www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/10/15/09:49:34

Xref: news-dnh.mv.net comp.os.msdos.djgpp:2670
Path: news-dnh.mv.net!mv!news.sprintlink.net!newsfeed.internetmci.com!tank.news.pipex.net!pipex!dispatch.news.demon.net!demon!mail2news.demon.co.uk!jjavp.demon.co.uk
From: Justin Johnson <Justin AT jjavp DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C++ delete []
Date: Sat, 14 Oct 95 23:12:32 GMT
Organization: Myorganisation
Lines: 49
References: <45ptmi$ehg AT ds2 DOT acs DOT ucalgary DOT ca>
Reply-To: Justin AT jjavp DOT demon DOT co DOT uk
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

In article <45ptmi$ehg AT ds2 DOT acs DOT ucalgary DOT ca>
           mewesolo AT freenet DOT calgary DOT ab DOT ca  writes:

> I'm using djgpp 1.12 M4. As part of a learning exercise, I've defined a
> class which allocates memory in the constructor, and deletes it in the
> destructor. I've then defined a function which takes one parameter of this
> class, and returns an object of the same class. The intent is to
> deliberately break the code (I want to see what happens when I later free
> which should already be freed memory, not to mention accesing this
> memory).
> 
> Anyway, what I see is that the memory is freed twice with no problem, and
> that the contents (a simple string) are still accessible. In other
> words, it appears that delete [] is not doing its job (would we normally
> expect a NULL pointer?). 

Freeing an allocated block of memory does not turn all the pointers that
were pointing to it to NULL. Chances are that your second free is actually
doing nothing at all - I think that maybe this crashes under certain
circumstances. For C to change all pointers of a free block to NULL would mean
that it has to store references to *all* pointers to a block, which IMHO is a
pointless overhead.

You can still access the string because:

a) The pointer still points to the recently free memory.
b) C/C++ does not null memory that you free.

The next few memory allocations that may occur would probably result in this
memory being used once more, so you cant take for granted that freed memory
will be what you expect.

BTW, here's a related question for any knee-deep techie C/C++ bods out there,
I've just written my own memory allocation library allowing me to have pools
of memory within pools of memory and the ability to instantly free a whole
pool of memory just by resetting a few pointers. Its so much more flexible
that the C/C++ equivilent. Thing is, I realise that there is a linked list
containing free blocks whose entries join together if they can, but what about
the data structures for allocated blocks? I dont think this is a linked list
because freeing memory is extrememly fast, even with thousands of blocks.
Anybody know? I just want to compare it with what I've done.

Regards,
--------------------------------------------------------------------------------
Justin Johnson
E-Mail: justin AT jjavp DOT demon DOT co DOT uk
BOOM! BABY ... Not bad for a Humanoid.
================================================================================

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019