c - Freeing int pointer to pointer array works fine alone, but crashes in main program -


this code snippet works fine on own , once integrate complete main() function, issues free(0ing, failing @ first iteration, failing @ random , when try free int pointer pointer.

int main() {     int** gprilistedcoords = calloc(sizeof(int*), gprimaxval);     int i;     for(i = 0; < gprimaxval; i++)         gprilistedcoords[i] = calloc(sizeof(int) , 2);     for( = 0; < gprimaxval; i++ )         free(gprilistedcoords[i]);     free(gprilistedcoords);     return 0; } 

i create / free char pointers ( pointers ) in same way no issues.

i tried catching null pointers, i'm not getting any.

gdb stack info: http://i.snag.gy/z3zqv.jpg

this code sample looks fine. think problem in other case, do allocated pointers. per man page of free() (emphasis mine)

void free(void *ptr);

the free() function frees memory space pointed ptr, which must have been returned previous call malloc(), calloc() or realloc(). otherwise, or if free(ptr) has been called before, undefined behavior occurs. if ptr null, no operation performed.

so, in case, if calloc()-ed pointer changed, you'll face undefined behaviour.

you can use debugger (like, gdb on linux) inspect allocated memory locations , see if pointer changed or not.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -