Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Violation in pointer arithmetic

With the 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 get the Access Violation? What should I change?

like image 682
Hanan N. Avatar asked Jul 09 '26 08:07

Hanan N.


1 Answers

When you add buffer + nsize you have to realize that you are actually adding buffer + (nsize * (sizeof(int)) since it's a int * when you are doing pointer arithmetic.

So it probably has something to do with it. Try incrementing nsize by nsize += 4096/sizeof(int) or something more clever.

like image 129
Pablo Santa Cruz Avatar answered Jul 12 '26 02:07

Pablo Santa Cruz