Deleting Global allocated memory in C++ -
today tried create global pointer:
int *map = new int[m2*n2];
then tried delete in main() function:
delete[] map; map = null;
but after executing program gives error:
free(): invalid next size (fast)
so, possible create such pointer , delete anywhere else?
edit
its fixed.
m2 m+1 (m limit array), after using m2=m+10 worked.
thanks all...
the error seems saying that, when trying clean memory block, size stored next heap block invalid. information stored in small header blocks, before pointer new
.
...--+----------------+--------+----------+------------+------------+--... | previous block | block | block | next block | next block | | data | header | data | header | data | ...--+----------------+--------+----------+------------+------------+--... ^ ^ | | new[] returns pointer here -----+ | wrote here --------------+ meaning os can no longer find blocks after there.
i bet writing memory outside bounds of array. in other words, you're accessing index less 0
or greater or equal m2*n2
. (since it's complaining next heap block header being corrupt, it's latter case.)
turn off parts of code until free()
stops complaining. turn on parts turned off until starts again. narrow down few, or one, lines causes problem, fix it.
edit: you've determined larger array works around problem--it not fix it--you should initialize "out of bounds" parts of array known constant (pick number, preferably 1 unlikely in data) , @ end of program, can see out-of-bounds elements changed.
in end, if code not work int *map = new int[m*n];
, faulty code. if 1 of subordinates told me m2=m+10
final fix code, i'd fire them. that's nothing more latent bug waiting come , bite you, right when deadline hits.
Comments
Post a Comment