Jump to content
  • Advertisement
Sign in to follow this  

[C++] copying arrays.

This topic is 3414 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

square * amount = new square[x]; square * amount1= new square[x]; amount1 = amount; i cant figure out how to copy the data of amount to amount1; also later, when i try delete[] amount1, i get a whole buncha errors, is that because im deleting 1 block of memory referenced by two objects?

Share this post


Link to post
Share on other sites
Advertisement
You might want to consider using std::vector instead of pointers. They avoid memory leaks in ways you don't even imagine yet, support the correct assignment and copy construction semantics, store their own length, and can even be resized.

std::vector<square> amount(x);
std::vector<square> amount1(x);
amount1 = amount;


Otherwise, you have to rely on a copy operator, such as std::copy:

square * amount = new square[x];
square * amount1= new square[x];
std::copy(amount, amount+x, amount1);[:code]

Or, of course, implement it yourself with a for-loop, although I wonder why you'd want to do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by insertz
memcpy(amount1, amount , (sizeof(square) * x));
memcpy only works if square is a POD type—if the object being copied around is not a POD, the compiler can mess things up and will mess things up on certain occasions.

Share this post


Link to post
Share on other sites
the reason im doing is because im going to have objects shown on the screen but it will not be a fixed number, so i want to keep track of these objects through an array.

Share this post


Link to post
Share on other sites
Quote:
Original post by pavel989
the reason im doing is because im going to have objects shown on the screen but it will not be a fixed number, so i want to keep track of these objects through an array.


That is exactly the kind of scenario std::vector was designed for.

Share this post


Link to post
Share on other sites
when copying memory, u can't just pass it in that way, it's like asking an existing pointer to point to another block of memory, it is why it crashes.
copy the elements.

square * amount = new square[x];
square * amount1 = new square[y];

if(amount1 != NULL)
delete [] amount1;

amount1 = new square[x];

for(int i = 0; i != x; i++)
amount1 = amount;

Share this post


Link to post
Share on other sites
i ended up trying that, but it still copied the pointer value instead of values. i just decided to stick with vectors. it seems to be the new kool thing to do, and seems to work quite well.

But ima still try waht you said, danong, just out of interest.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!