Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to swap static array pointers in c++?

Credits go to lucaz, Marco Radaelli, Rampage, and ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
How to swap static array pointers in c++?
HoHo
Member #4,534
April 2004
avatar

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?

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

ReyBrujo
Moderator
January 2001
avatar

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.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

HoHo
Member #4,534
April 2004
avatar

some code that does this would be helpful. Everything I have tried haven't worked

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Marco Radaelli
Member #3,028
December 2002
avatar

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 :D
[edit2] I should correct myself: in C you can directly assign structures ::)

Rampage
Member #3,035
December 2002
avatar

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).
You are trying to copy const pointers not the array data.
Take a look to std::vector<>.

HoHo
Member #4,534
April 2004
avatar

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.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Erkle
Member #3,493
May 2003
avatar

This is the fastest way I know of doing that:

1#include <stdio.h>
2#include <conio.h>
3 
4int main()
5{
6 char szBuff1[256] = "Hello\n";
7 char szBuff2[256] = "World\n";
8 char* szCurrent = szBuff1;
9 int dwSum = (int)szBuff1+(int)szBuff2;
10 
11 printf("%s", szCurrent); // Hello
12 szCurrent = (char*)(dwSum-(int)szCurrent);
13 
14 printf("%s", szCurrent); // World
15 szCurrent = (char*)(dwSum-(int)szCurrent);
16 
17 printf("%s", szCurrent); // Hello
18 szCurrent = (char*)(dwSum-(int)szCurrent);
19 
20 printf("%s", szCurrent); // World
21 szCurrent = (char*)(dwSum-(int)szCurrent);
22 
23 printf("%s", szCurrent); // Hello
24 getch();
25 
26 return 0;
27}

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.
And if you read that wrong it means 'All of the above is opinion and is only to be treated as such.'

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Erkle
Member #3,493
May 2003
avatar

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.
And if you read that wrong it means 'All of the above is opinion and is only to be treated as such.'

Kitty Cat
Member #2,815
October 2002
avatar

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;

:)

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

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? :P

Thomas Fjellstrom
Member #476
June 2000
avatar

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 :P You can treat it like a pointer in MOST cases, but not all.

Its best not to assume an array is a pointer, it'll just give you problems like these.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ReyBrujo
Moderator
January 2001
avatar

I would say a pointer, because you can change their address if using ISO C99 flexible arrays.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

lucaz
Member #4,194
January 2004

the array name is a const pointer to the block of memory.

Thomas Fjellstrom
Member #476
June 2000
avatar

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 :P

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

A pointer or a const pointer? :P

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

ReyBrujo
Moderator
January 2001
avatar

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.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Kitty Cat
Member #2,815
October 2002
avatar

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

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 empty array of pointers to char (char* line[]) not a char* line[number].
In both cases, it is allocated dynamically.

EDIT: beaten :(

ReyBrujo
Moderator
January 2001
avatar

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 :)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Rampage
Member #3,035
December 2002
avatar

Now I'm confused ???. I thought that the pointers are variables that contain the address of memory locations. A BITMAP* ptr has the address of a BITMAP, but the address of the BITMAP* ptr (&ptr) is itself constant.

-R

Kitty Cat
Member #2,815
October 2002
avatar

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

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

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!!! :-X

Erkle
Member #3,493
May 2003
avatar

Quote:

Its still an array. and is NOT a pointer :P You can treat it like a pointer in MOST cases, but not all.

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.
And if you read that wrong it means 'All of the above is opinion and is only to be treated as such.'

 1   2 


Go to: