c - how to deallocate the memory allocated to the malloc in following code? -
int n; int **arr; arr = (int**)malloc(n*sizeof(int)); (i=0; i<n; ++i){ arr[i] = malloc(2*sizeof(int)); }
[edit]
*** glibc detected *** ./abc: double free or corruption (out): 0x0000000002693370 ======= backtrace: ========= /lib/libc.so.6(+0x775b6)[0x7f42719465b6] /lib/libc.so.6(cfree+0x73)[0x7f427194ce53] ./abc[0x405f01] /lib/libc.so.6(__libc_start_main+0xfd)[0x7f42718edc4d] ./abc[0x400ee9] ======= memory map: ======== 00400000-00416000 r-xp 00000000 08:07 392882 00616000-00617000 r--p 00016000 08:07 392882
i tried following mentioned answers, got following above mentioned error.what can reason it?
[edit]
the above mentioned problem solved. since there bug in code. now,what not getting improvement in freed memory.what can reason it?
[edit] using modules print memory.following code
memory_print(); #ifdef check memory_printleaks(); #endif
here memory_printleaks()
prints demanded memory , freed memory.i getting same value after making changes.
one more remark add is, can call free() anywhere program or required call particular locations place malloc() has been called or @ end of program?
with free
. must first free memory pointed @ arr[i]
entries, , free memory pointed @ arr
(which holds entries). can't other way around, because freeing memory means may no longer use values in memory.
thus:
for (i = 0; < n; ++i) { free(arr[i]); } free(arr);
Comments
Post a Comment