[ale] g++ and c++: freeing unallocated space with delete

Irv Mullins irvm at ellijay.com
Wed Nov 6 11:14:37 EST 2002


On Wednesday 06 November 2002 10:32 am, John 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.

The seg fault (trying to access unallocated space) won't always happen, 
because memory is not often allocated atomically (one byte at a time).
If the OS allocates memory to programs in blocks of ten, say, and you 
point to the first byte, then you might access 9 bytes past that point 
without a segfault, since those bytes "belong" to your process, even 
though they aren't initialized or used. On a different platform or different 
compiler, etc. etc..... ymmv.

At least I think it works that way.

Irv

---
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