|
|
This thread is locked; no one can reply to it.
|
1
2
|
| How to swap static array pointers in c++? |
|
HoHo
Member #4,534
April 2004
|
Don't laugh at the easy looking question before you try out your solution yourself. A friend of mine made a program that used two char arrays in turns(something like doublebuffering). To swap them he used memcpy. I suggested him to use pointer swapping to speed things up considerably but he told it didn't work. Well, it's true. Code like this doesn't work: char state[256]; char lastState[256]; char *tmp; lastState=state;//error: ISO C++ forbids assignment of arrays state=tmp;///usr/katsepolygon/cpp/coop/tehas/src/tehas.cpp:134: error: incompatible types in assignment of `char*' to `char[256]' It looks allmost perfect(tmp is not of type char[256]) but it doesn't work. My guess is that it's practically impossible to swap two arrays of this kind. What do you think? __________ |
|
ReyBrujo
Moderator
January 2001
|
You cannot pass one array to another array just like that. What you can do is point the pointer to one buffer, and when needed point to another, and use the pointer instead of the buffers. But you won't be changing the information in the buffers, just the current buffer. -- |
|
HoHo
Member #4,534
April 2004
|
some code that does this would be helpful. Everything I have tried haven't worked __________ |
|
Marco Radaelli
Member #3,028
December 2002
|
In C (and C++) you can't directly assign arrays or structures. Hence, you can use memcpy() or manually swap each char (which is what memcpy() basically does, but probably it swaps as many bytes as it can per cycle, to speed things up [edit] Beaten
|
|
Rampage
Member #3,035
December 2002
|
This won't work (and it's illegal): char state[size]; char lastState[size]; char *temp; temp = state; state = lastState; lastState = temp; You want this: char buf1[size]; char buf2[size]; char *handle; if (some_condition) handle = buf1; /* buf1 is the active buffer */ else handle = buf2; /* buf2 is the active buffer */ process_data(handle);
-R |
|
lucaz
Member #4,194
January 2004
|
The sentence 'lastState=state' fails because lastState and state are const pointers (not just simple pointers). |
|
HoHo
Member #4,534
April 2004
|
Jorram, that's pretty much the same way I have used things before(the second example). I assumed that it works the same on statically defined arrays too. If these pointers are const pointers then that makes perfect sense. I wonder why I haven't seen it written in any books or tutorials. I use statically defined arrays very rearely and almost always use dynamic ones or stl containers. That's why I haven't had such problems before. Thanks everybody. __________ |
|
Erkle
Member #3,493
May 2003
|
This is the fastest way I know of doing that:
Of course I'm going to get a few replies suggesting that it is "Dangerous Casting".;D
If the writing above has offended you, you've read it wrong.....fool. |
|
Thomas Fjellstrom
Member #476
June 2000
|
Quote: If these pointers are const pointers then that makes perfect sense. They arn't pointers. They are arrays, that pretend to look like pointers. -- |
|
Erkle
Member #3,493
May 2003
|
In C an array is implemented as a const pointer and will act the same in all cases.
If the writing above has offended you, you've read it wrong.....fool. |
|
Kitty Cat
Member #2,815
October 2002
|
An array is a block of memory. Using the array name without fully derefencing it will implicitly give you a pointer. Check it: int array[23][25][24]; array == array[0] == array[0][0] == &array[0][0][0] == &array[0][0] == &array[0] == &array;
-- |
|
lucaz
Member #4,194
January 2004
|
Quote: Using the array name without fully derefencing it will implicitly give you a pointer.
A pointer or a const pointer? |
|
Thomas Fjellstrom
Member #476
June 2000
|
Quote: In C an array is implemented as a const pointer and will act the same in all cases. Its still an array. and is NOT a pointer Its best not to assume an array is a pointer, it'll just give you problems like these. -- |
|
ReyBrujo
Moderator
January 2001
|
I would say a pointer, because you can change their address if using ISO C99 flexible arrays. -- |
|
lucaz
Member #4,194
January 2004
|
the array name is a const pointer to the block of memory. |
|
Thomas Fjellstrom
Member #476
June 2000
|
the compiler may give you a pointer, but an array is an array, now, the address of an array can be used in a pointer sure -- |
|
Kitty Cat
Member #2,815
October 2002
|
Quote: A pointer or a const pointer? An implicit pointer, just like: int var; &var; // this is implicit int array[3]; array; // so is this It's a hard-coded value in the program code. -- |
|
ReyBrujo
Moderator
January 2001
|
Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories. -- |
|
Kitty Cat
Member #2,815
October 2002
|
Quote: Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories. No. Allegro's line pointer points to the end of the allocated structure. The struct itself doesn't allocate any memory.. Allegro does that itself. -- |
|
lucaz
Member #4,194
January 2004
|
Quote: Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories.
As far as I know, the BITMAP line is pointer to a char pointer (char**) or an EDIT: beaten |
|
ReyBrujo
Moderator
January 2001
|
I could have swear that allocating a 0 bytes array was valid as allocating a 1 byte array, based on the fact that malloc(0) returns a valid pointer. Seemly I am wrong, sorry -- |
|
Rampage
Member #3,035
December 2002
|
Now I'm confused -R |
|
Kitty Cat
Member #2,815
October 2002
|
Jorram, yes. BITMAP *ptr; &ptr give you the hard-coded address of the BITMAP pointer, which is a variable pointer that points to a BITMAP struct. Quote: I could have swear that allocating a 0 bytes array was valid as allocating a 1 byte array, based on the fact that malloc(0) returns a valid pointer. Seemly I am wrong, sorry Actually, that is right. However, you can't dereference anything returned by malloc(0) because the first byte you try to dereference is out of bounds. Allegro's line pointer is a static array of char* pointers. EDIT: clarrification -- |
|
lucaz
Member #4,194
January 2004
|
As far as I know: char c = 3; char* pc = &c; Memory Name Address Data ------ ------ ------ c 0x004A -> 0x0003 ------ ------ ------ pc 0x0058 -> 0x004A ------ ------ ------ char* p; &c = p; // error, you are trying to change the memory address of a non-pointer variable.
EDIT: beaten again!!! |
|
Erkle
Member #3,493
May 2003
|
Quote: Its still an array. and is NOT a pointer I didn't say "pointer" I said "const pointer":P. If you look at all the cases where arrays aren't like pointers you'll find they do act like const pointers.
If the writing above has offended you, you've read it wrong.....fool. |
|
|
1
2
|