Search code examples
cpointersmemory-managementmemory-leaksrealloc

realloc memory Access violation reading location


Probably my brain is not working properly now... I wonder why I receive mentioned error in my code:

int ** zm;
zm = (int**)calloc(1, sizeof(int*));
*zm = (int*)calloc(1, sizeof(int));
*zm[0] = 5;
*zm = (int*)realloc(*zm, 2*sizeof(int));
*zm[1] = 10; // Access violation reading location 0xFFFFFFFFFFFFFFFF

Could someone explain what happend in that code?


Solution

  • Wrong indexing, try (*zm)[1] instead. And check for errors from library calls.