Jump to content
  • Advertisement
Sign in to follow this  
Uphoreum

Copying Arrays

This topic is 3753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to copy an array, but the array is just in the form of a pointer, "Uint8*" to be exact. I've come to the conclusion that memcpy is the way to go, and this is how I'm trying to do it:
Uint8 *array2 = new Uint8[sizeof(array1)];
memcpy(array2, array1, sizeof(array1));

I must be doing this wrong, because the values in 'array2' are not the same as those in array1. All the examples I could find were situations where the exact size of the source array was known, like it was defined as array1[5]. Anyone know what I'm doing wrong here?

Share this post


Link to post
Share on other sites
Advertisement
Assuming array1 is also a Uint8*, sizeof(array1) is sizeof(Uint8*), probably 4. It is not the number of elements in array1.

Share this post


Link to post
Share on other sites
Think about it. A pointer is just that - a pointer to a memory location. How could that possibly be enough to determine the size of an arbitrary collection of instances of an arbitrary type?

There are only two (reasonable) ways to determine how long an array is: store that information somewhere, or end the array with a terminating character (such as NULL for NULL-terminated C strings). The latter method is typically not a good idea to use.

So basically, if you want to copy an array, you need to know how big it is. To achieve that, do one of the following (from best to worst option):
-use std::vector
-use regular array declarations instead of pointers
-store the size of the array in a variable and encapsulate the whole thing into your own array-class.

Share this post


Link to post
Share on other sites
You shouldn't use memcpy in C++. It's a throwback to C. use std::copy instead.

std::copy(array1, array1+size, array2);

Share this post


Link to post
Share on other sites
Quote:
Original post by Uphoreum
So what should I do to copy this type of an array?


One way or another you need to know the number of elements in the array, so you'll either need to keep track of this when you create array1, or ask the client to pass in this information along with array1 if this is a library function.

Of course, if this is C++ and not C, your whole problem goes away if you switch from raw arrays to std::vectors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hnefi
So basically, if you want to copy an array, you need to know how big it is. To achieve that, do one of the following (from best to worst option):
-use std::vector
-use regular array declarations instead of pointers
-store the size of the array in a variable and encapsulate the whole thing into your own array-class.


The problem is, the source array that I'm trying to copy is coming from SDL_GetKeyState. SDL stores this array internally, and all I'm getting is a pointer to it, but I want to save the state so that I can test whether the state of a key has changed. Basically, I want to have the current keyboard state and the previous keyboard state before the state was updated. If I don't copy it, the state will change and I can't look back on previous states to find the differences.

Maybe I'm just going about this wrong. How can I achieve this sort of thing?

Share this post


Link to post
Share on other sites
Quote:
Original post by Uphoreum
The problem is, the source array that I'm trying to copy is coming from SDL_GetKeyState. SDL stores this array internally, and all I'm getting is a pointer to it


SDL_GetKeyState takes a pointer to an integer, which will contain the length of the array it returns (according to this). So,


int len = 0;
Uint8 *keystates = SDL_GetKeyState(&len);
Uint8 *array2 = new Uint8[len];
std::copy(array1, array1 + len, array2);

Share this post


Link to post
Share on other sites
Oh wow... I thought that parameter was to specify the size of the array you wanted returned. I know that doesn't make any sense, but I didn't think too much about it because the example just passed in NULL.

Well, that solves my problem. Thanks for pointing that out!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!