Mail Archives: djgpp/2000/06/09/01:16:39
Nimrod Abing (n_abing AT hotmail DOT com) wrote:
: >From: apsh AT ecr DOT mu DOT oz DOT au (Alistair_P SHILTON)
: >Reply-To: djgpp AT delorie DOT com
: >To: djgpp AT delorie DOT com
: >Subject: Re: Strange behaviour of new/delete
: >Date: 7 Jun 2000 07:27:55 GMT
: >
: >Is that a no :) ?  Thanks anyway - its just that I've been trying to work 
: >this
: >out for ~4 weeks now with no success, so I thought, just maybe...  oh well,
: >back to the drawing board.  I'll try your suggestion of C++ing it up a bit
: >more and see if it helps.
: >
: >Sorry for not posting the full code, but the program is quite large (~6000
: >
: >over 4 files), and the new and delete statements are quite distant from one
: >another.
: >
: >ps - how do you ckeck for new error?  Sorry if that's a dumb question, but
: >I'm relatively new to C++ (I usually do C).
: >
: >Thanks again.
: >
: [SNIP]
: What you did was use the operator `new' allocate memory for an array of
: objects (in this case doubles). When you use `new' like that, you need to
: `delete' like this:
:      delete [] object_array;
:      //     ^^----- you can use any value here, but it's okay to leave it
:      //             empty.
I was under the impression that the statements:
delete removal[0];
delete removal[1];
delete removal[2];
delete removal;
and
delete [] removal;
would be synonyms in this case?  Perhaps not.  Anyhow, I tried your your
suggestion, and a some other changes, and the error moved to another equally
inexplicable spot.  So I guess that I must have made an error elsewhere and
it just doesn't happen to turn up until this point - back to the drawing
board I guess.
Anyway, thanks for your help.
: And yes, allocating memory using `obj_ptr = new obj_class' is very
: different from obj_ptr = new obj_class[elms]'.
: Also `new' does not return a NULL pointer when it fails to find enough
: memory, it throws a `bad_alloc' exception instead. So checking the result
: of `obj_ptr = new obj_class' for a NULL value is _not_ necessary.
: Instead you should use a `try' block and `catch' any `bad_alloc' that
: `new' might throw.
:      try {
:        obj_ptr = new obj_class;
:      }
:      catch (bad_alloc) {
:      // ... do something in case of bad alloc.
:      }
: ----------------
: _nimrod_a_abing_
: ------------------------------------------
: Homepage: http://www.geocities.com/n_abing
: ________________________________________________________________________
: Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
--
----------------------------------------------------
Alistair Shilton
http://www.ee.mu.oz.au/pgrad/apsh
- Raw text -