[ale] g++ and c++: freeing unallocated space with delete
Joseph A Knapka
jknapka at earthlink.net
Wed Nov 6 11:09:42 EST 2002
John Wells wrote:
> I have a program that manipulates various objects on the heap. I recently
> added a bug (unintentionally) in one of my modifications in which the
> program tries to free unallocated space. In other words, it calls delete
> on an string pointer that hasn't been pointed anywhere.
>
> The odd thing is, for a long time it worked just fine with no errors under
> gcc/g++ 2.96. Recently, I had to downgrade for other reasons to 2.95 and
> started getting seg faults, which led me to the culprit.
>
> Why did this work before? Do certain versions of gcc/g++ handle mistakes
> like this for you? And, if so, is there any way to turn this behavior
> off, so that no non-standard modifications are made? I'd like this code
> to be as standards compliant and possible, and if there are errors on my
> part, I'd like to know about them.
In general, heap corruption is a silent bug that may or may
not cause something to crash many millions of instructions later -
a program containing such a bug has undefined behavior, and the
compiler and/or runtime environment are not obligated to notify
you of the problem at the point where it occurs. If
you're getting a SEGV on an attempt to free unallocated memory, that
seems to indicate that the heap library you're using (presumably
the one that comes with libstdc++ in g++2.95) is doing some extra
checking for you that the 2.96 version doesn't do. But that's
just a Scientific Wild-Ass Guess.
Cheers,
-- Joe
---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.
More information about the Ale
mailing list