POV-Ray : Newsgroups : povray.off-topic : Adventures with C++ : Re: Adventures with C++ Server Time
29 Jul 2024 00:36:31 EDT (-0400)
  Re: Adventures with C++  
From: Warp
Date: 31 May 2013 16:41:35
Message: <51a90aff@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> To be clear... I only need to do explicit deallocation if I've done 
> explicit allocation in the first place, right?

Every 'new' must have a correspondent 'delete'. If you don't write 'new'
then you don't have to worry about any deletes either.

Of course the exception to this is if you are using a smart pointer like
std::shared_ptr or std::unique_ptr, in which case you do write 'new',
but let the smart pointer do the 'delete' for you.

If you really need to explicitly allocate memory, then it's usually a
good idea to use such a smart pointer, because they will make sure that
they will delete the object when the last smart pointer to it dies, and
delete it only once.

The thing is, even if you have written one 'new' and one 'delete' in your
code, the object might be being deleted more than once. This is because of
the wonderful thing in C++ that is copying and assigning of classes.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.