 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave Guest
|
Posted: Sun Oct 19, 2003 4:02 pm Post subject: ECPP questions |
|
|
Hello all,
I have a couple of questions regarding memory allocation and Herb's first
book (page 146):
1.
When explaining why operator delete has two parameters, the answer is that
"It's just preference". What does this mean?
2.
There is a reference to "placement delete" just after the quote mentioned
above. What is placement delete??? If you create an object with placement
new, you've haven't allocated anything; you've only caused a constructor
invocation. Why would I ever want to call delete not having allocated
anything?
Thanks,
Dave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Aaron Bentley Guest
|
Posted: Sun Oct 19, 2003 9:41 pm Post subject: Re: ECPP questions |
|
|
Dave wrote:
| Quote: | Hello all,
I have a couple of questions regarding memory allocation and Herb's first
book (page 146):
[snip]
2.
There is a reference to "placement delete" just after the quote mentioned
above. What is placement delete???
|
When a constructor throws, the memory allocated with new must be
deallocated. For Placement new, there is a corresponding placement
delete to deallocate the memory. Otherwise, it will leak.
| Quote: | If you create an object with placement
new, you've haven't allocated anything; you've only caused a constructor
invocation.
|
Frequently, you *have* allocated something-- for example, you could use
placement new to allocate from shared memory.
--
Aaron Bentley
www.aaronbentley.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrea Griffini Guest
|
Posted: Sun Oct 19, 2003 9:46 pm Post subject: Re: ECPP questions |
|
|
On 19 Oct 2003 12:02:41 -0400, "Dave" <better_cs_now (AT) yahoo (DOT) com> wrote:
| Quote: | What is placement delete??? If you create an object with placement
new, you've haven't allocated anything; you've only caused a constructor
invocation. Why would I ever want to call delete not having allocated
anything?
|
With "placement new" and despite the name the concept is not
the one of creating an object at a specific memory address,
but to invoke a "special" allocator passing it additional
parameters to get the memory in which the object should be built.
There is also a standard provided "special" allocator that
takes a void* as a parameter that just returns that pointer
as the address in which the object should be created; but this
is just one of the possible "placement new" allocators: you
can have special allocators that accept other parameters
instead of a pointer and that do any computation you like to
obtain the address in which the object should be built into.
When you allocate an object using the standard (non-placement)
new, and the object being constructed throws an exception
during the execution of the constructor, the compiler will
call the standard operator delete to free the memory block
that was reserved for the object whose construction was aborted.
When an object is instead being allocated using a placement
new throws an exception during the constructor it would be
clearly a terrible idea to call the standard delete operator
(because the memory was *not* coming from the standard new
operator), so the C++ standard mandates that if there is a
placement delete operator whose signature matches the one of
the allocator used then *that* special delete will be called
passing the same parameters used to allocate the memory,
and if there is no such a compatible placement delete then
there will be no attempt to free the block.
Of course with the standard provided placement new operator
just ignoring the delete operation is perfectly meaningful
and generates no leak because the allocator didn't indeed
allocate any memory, but in the general case this is not true.
HTH
Andrea
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Wed Oct 22, 2003 1:24 am Post subject: Re: ECPP questions |
|
|
Hi,
| Quote: | 1.
When explaining why operator delete has two parameters, the answer is
that
"It's just preference". What does this mean?
|
The second (size_t) argument of operator delete is the size of the
object
allocated, similar to the size_t argument to new. Whereas this argument
is
clearly required for allocating objects, the size_t argument of delete
might be not required for most systems, especially those where the
operator
delete is implemented by means of the free() call of the C library which
keeps
the size of the memory in the memory itself and doesn't require a size
argument.
However, I *DO NOT* agree that this is "just a preference". While this
is
true for most systems, there are memory allocators out in the wilderness
which
do not keep the memory size of the object automatically, and where a
memory
release requires the size of the memory along with its address.
Unfortunately, I found quite some implementations where this second
argument
to operator delete was broken. (older versions of g++, and IIRC VS 6.0)
| Quote: | 2.
There is a reference to "placement delete" just after the quote
mentioned
above. What is placement delete??? If you create an object with
placement
new, you've haven't allocated anything; you've only caused a
constructor
invocation. Why would I ever want to call delete not having allocated
anything?
|
I would more or less call "placement new" a misnomer. "new with
arguments" would be much more apropriate. One common usage for this
construction is where you want to place an object into an already
allocated memory area; in that case the argument is the memory block,
and there is indeed rarely the need to delete the memory again thru
the operator delete. However, "placement new arguments" also has some
benefits besides this. For example, consider a system with main memory
and designated graphics memory. If you would like to allocate memory
from the pool of graphics memory, an operator new that takes a flag
value which memory pool to allocate the memory from would definitely
make sense.
However, it is a common misunderstanding that for objects allocated by
"operator new with arguments" the corresponding "operator delete with
arguments" gets called. This is only the case if you allocated the
object
within a constructor of another object, and (the other's object)
construction
failed afterwards, requiring to get rid of the already allocated
components.
Instead, for regular "deletion" of objects, the regular operator delete
will
be called. It is your job to figure out where the memory came from and
what
to do about it. See also my answer to your post below.
After all, I think that the C++ requires some reconsideration in this
field,
this is all a bit too messy for me... (-;
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|