delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/08/03:05:51

Message-ID: <20000608070434.10895.qmail@hotmail.com>
X-Originating-IP: [208.160.246.197]
From: "Nimrod Abing" <n_abing AT hotmail DOT com>
To: djgpp AT delorie DOT com
Cc: apsh AT ecr DOT mu DOT oz DOT au
Subject: Re: Strange behaviour of new/delete
Date: Thu, 08 Jun 2000 15:04:34 PHT
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

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

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

- Raw text -


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