memcpy to arrays

Started by
18 comments, last by Noobwaker 18 years, 1 month ago
*sadness*
Advertisement
Quote:Original post by Noobwaker
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.


Your code looks correct to me (like it did to others). The suggestion is now that you copy and paste (don't type, you may make typos) an actual code block to exhibits the problem. Preferably, the code block should be a complete program that we can compile.

Here's an example of what such a block could look like (even though it doesn't appear to exhibit your problem):
#include <stdio.h>#include <string.h>int main(void) {    short Array1[101][101][2], x[202];    size_t loopf1;    for (loopf1 = 0; loopf1 < 202; loopf1++) {        x[loopf1] = loopf1;    }    for (loopf1 = 0; loopf1 < 101; loopf1++) {	  memcpy(Array1[loopf1], x, 202 * sizeof(short));    }    for (loopf1 = 0; loopf1 < 101; loopf1++) {        size_t loopf2;        for (loopf2 = 0; loopf2 < 101; loopf2++) {            printf("%d %d ", (int)Array1[loopf1][loopf2][0], (int)Array1[loopf1][loopf2][1]);        }        printf("\n");    }    return 0;}


Here's one that uses "sizeof var" instead of "sizeof(type)" and magic numbers to help avoid errors (in particular for the memcpy):
#include <stdio.h>#include <string.h>#define MIN(a_, b_) ((a_) < (b_) ? (a_) : (b_))#define N(a_)       (sizeof a_ / sizeof *a_)int main(void) {    short Array1[101][101][2], x[202];    size_t loopf1;    for (loopf1 = 0; loopf1 < N(x); loopf1++) {        x[loopf1] = loopf1;    }    for (loopf1 = 0; loopf1 < N(Array1); loopf1++) {	  memcpy(Array1[loopf1], x, MIN(sizeof Array1[0], sizeof x));    }    for (loopf1 = 0; loopf1 < N(Array1); loopf1++) {        size_t loopf2;        for (loopf2 = 0; loopf2 < N(*Array1); loopf2++) {            printf("%d %d ", (int)Array1[loopf1][loopf2][0], (int)Array1[loopf1][loopf2][1]);        }        printf("\n");    }    return 0;}
Wow, I just found out something really messed up.
	path[37][84][0]=9;	path[37][84][1]=14;

sets Map.SizeX to 9, and Map.SizeY to 14.

What's going on?
Quote:Original post by Noobwaker
Wow, I just found out something really messed up.
	path[37][84][0]=9;	path[37][84][1]=14;

sets Map.SizeX to 9, and Map.SizeY to 14.

What's going on?


You have a bug. To be more specific, you either have an overflow or an aliasing problem. Without actual code (*hint* *hint*), it's hard to say exactly what's wrong.
So... what is aliasing?
Quote:Original post by Noobwaker
Wow, I just found out something really messed up.
	path[37][84][0]=9;	path[37][84][1]=14;

sets Map.SizeX to 9, and Map.SizeY to 14.

What's going on?


To understand what's going on, it's better that you back up a bit and do this with a 2D array. static arrays are contigous. Meaning, if you went out of bounds in a 2D array with the first row, you would land in the second row (assuming you're still within range). This works because of the contigousness, but, it's not recommended that you program this way =)

For example:

char twod[2][10]; // ROW and COLUMEtwod[0][10] = 'k';


would set the character value 'k' at indexe's 1 (row) and 0 (colume).



- xeddiex
one..
path[37][84][0] should still be in range though since the declaration is
short path[101][101][2];
But my problem is that somehow Map.SizeX is connected with path[37][84][0]. How does something like that happen, and how can I fix it?
Aliasing is when you have two names for the same data. The most trivial example is:
int a = 1;int *b = &aint *c = &a

A less trivial example is:
int a[2] = {1, 2};int *b = a;int *c = a + 1;

Because here b[1] and *c are the same. Overflow is very similar:
int a[1] = {1};int b = 2;

Here, a[1] might give you b.

Quote:Original post by Noobwaker
How does something like that happen, and how can I fix it?


I don't have enough information to answer that question. Take off the tinfoil hat and give us some code to work with.
Quote:Original post by Noobwaker
path[37][84][0] should still be in range though since the declaration is
short path[101][101][2];
But my problem is that somehow Map.SizeX is connected with path[37][84][0]. How does something like that happen, and how can I fix it?

We can't tell you what's going on, because, as you noticed, it shouldn't be happening in the first place. This means that something elsewhere must be the problem. Until we see that something elsewhere, you aren't going to get any help.

CM
I think it's just my debugger.

It will say that Map.SizeX = -1, but then I put in
loopf1=Map.SizeX;path[37][84][0]++;
and it says that loopf1 = 30 while still saying that Map.SizeX = -1, then it says that Map.SizeX = 0 once it reaches "path[37][84][0]++;"

This topic is closed to new replies.

Advertisement