Jump to content
  • Advertisement
Sign in to follow this  
Noobwaker

memcpy to arrays

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

If if have some arrays:
short Array1[101][101][2], x[202];
and I memcpy to Array1:
for(loopf1=0;loopf1<101;loopf1++){
	memcpy(&Array1[loopf1][0][0],x,(202*sizeof(short)));
}
Why would another variable get overwritten? Two other variables that I'm using get overwritten when loopf1==37.

Share this post


Link to post
Share on other sites
Advertisement
You should avoid using memcpy() generally, and rather use dynamic containers like std::list or std::vector, as these are much less error-prone than memcpy'ing things in arrays around yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Noobwaker
I've never used that notation before. Can you do that in C, or just C++? Or am I way off?


What lucem recommended is C++ only (It's a part of the SC++L). On this forum most people automatically assume you use C++.

EDIT: Fixed names

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
Quote:
Original post by Noobwaker
I've never used that notation before. Can you do that in C, or just C++? Or am I way off?


What Noobwaker recommended is ...


Hmm... was he talking to him/herself [grin].

Share this post


Link to post
Share on other sites
Basically, these are part of the C++ Standard template library.
As most compilers today are C++ compilers, you can use them from C, too, in most cases.
These containers, algortihms and iterators in the STL (and in other libs, too, like Boost:: for example) are really handy, as they base on templates and so a generic containers you can use with every data type.
They handle memory management for you and keep you away from producing segfaults ;), so all you have to do is use them, which simplifies development a lot a times.

Share this post


Link to post
Share on other sites
A quick test run with your code doesn't reveal any problems with the memory on either side of Array1 and x, and it looks OK to me [if not a little silly]. What exactly is getting overwritten?

CM

Share this post


Link to post
Share on other sites
it could be possible. if i remember correctly memmove is one function that handles overlapping boundaries. give memmove a try. though one question i have is (since it's been a while since i've used C...) are multidimensional arrays defined to be contiguous (is &Array[j] + 2 == &Array[j + 1]? i mean if done with new you'd have problems cuz your array size is technically 2, not 202.


short*** Array1 = new (short**)[101];
for(i = 0; i < 101; i++) {
Array1 = new (short*)[101];
for(j = 0; j < 101; j++) Array[j] = new short[2];
}



[Edited by - yadango on March 12, 2006 11:17:35 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by yadango
it could be possible. if i remember correctly memmove is one function that handles overlapping boundaries. give memmove a try. though one question i have is (since it's been a while since i've used C...) are multidimensional arrays defined to be contiguous (is &Array[j] + 2 == &Array[j + 1]?

Yes, they are defined to be contiguous, although your example is flawed since pointer arithmetic automatically acounts for type sizes. This assumes the array is statically defined...you can't use malloc for obvious reasons. I'm also not sure whether or not row-major or column-major forms are required. I'm not even sure which is used, since I always have to look up what those words mean. That, by the way, was my first guess. That the wrong ordering was being assumed, so bounds weren't adding up the way he thought they would.

CM

Share this post


Link to post
Share on other sites
The two variables, which are Map.SizeX and Map.SizeY, get overwritten when the loopf1 is 37. If I make x only 201, and memcpy only (201*sizeof(short)), then Array1[loopf1][100][0] gets written to, but Array1[loopf1][100][1] doesn't. So I don't think it's any overflow. Array1 and the Map variables are global, and x is local.

Is there something else I can do that would be just as fast or faster?

(I'm using C incase you don't know)

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!