Reputation: 33
int** z= new int *[5];
for (int j = 0; j < 8; j ++)
{
z[j] = new int[5];
}
for (int n=0; n<8; ++n)
{
for(int m=0;m<5;++m)
{
int x=n%4;
int y=x*wB;
int p=(*(B+(y+m)));
z[n][m]=p;
}
}
return z;
throws a Bad_Acess_error
at n=6
, but
int** z= new int *[5];
for (int j = 0; j < 8; j ++)
{
z[j] = new int[5];
}
for (int n=0; n<8; ++n)
{
for(int m=0;m<5;++m)
{
int x=n%4;
int y=x*5;
int p=(*(B+(y+m)));
z[6][m]=p;
}
return z;
}
throws no error. Why? This is really weird and I can't seem to understand why this is happening. I am just typing in more text so it allows me to publish this question.
Edit: replaced variables with numbers. The numbers are just limits of the array in question. I know the second code works because the output is exactly what I expect it to be.
Upvotes: 1
Views: 71
Reputation: 57749
Simply put: Buffer Overrun.
int** z= new int *[5]; // Allocates space for 5 slots.
for (int j = 0;
j < 8; // <---- *** Assumes space for 8 slots!!!!!
j ++)
{
z[j] = new int[5]; // At j==6, access is outside the array.
}
Upvotes: 1