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(0
ing, 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 pointedptr
, which must have been returned previous callmalloc()
,calloc()
orrealloc()
. otherwise, or iffree(ptr)
has been called before, undefined behavior occurs. if ptrnull
, 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
Post a Comment