Access violation in pointer arithmetic
With code:
int nsize;
int * buffer;
char TargetBuffer[4096];
const SIZE_T buffersize = (320*240) * sizeof(int);
buffer = (int *) malloc(bufferSize);
// fill buffer with data
nsize = 0;
while(nsize < buffersize)
{
// HERE after some loops i get Access Violation
memcpy(TargetBuffer, buffer + nsize, 4096);
// do stuff with TargetBuffer
nsize += 4096;
}
Why am I getting an access violation? What should I change?
+3
source to share
3 answers
I would do something like this:
SIZE_T left = buffersize;
nsize = 0;
while(left)
{
SIZE_T block = (left >= 4096)?4096:left;
// HERE after some loops i get Access Violation
memcpy(TargetBuffer, buffer, block);
buffer += (left) / sizeof(*buffer);
// do stuff with TargetBuffer
left -= block;
}
I'm sure the problem you are seeing is that you are going over the edge because your code doesn't care about dimensions that are not multiples of 4K.
+3
source to share