Re: new operator doesn't work?
On Feb 17, 2:22 pm, "Alf P. Steinbach" <alf.p.steinbach
+use...@gmail.com> wrote:
On 17.02.2012 07:06, Stanley Rice wrote:
The global new operator function has three overloaded prototype, and on=
e of it
is:
void * operator new(size_t s, void *p) throw();
The document says that the above function doesn't allocate memory, inst=
ead, it
just call the constructor of the given type and place the address of th=
e
object on the given allocated memory pointed by p. and it is "equal to"
new (type) p;
I tried it, but failed. I have the following code snippet:
**********************************************************************
struct myclass {
myclass() { cout<< "constructor"<< endl; }
};
myclass *p = operator new(sizeof *p)); // just allocate memory
new (p) myclass; // works, =
the constructor is called.
// operator new(sizeof *p, p); // doesn't work, no mess=
age printed.
***********************************************************************
What's going on there? Am I misunderstand something?
Yes. `operator new` is just an allocation function, and I think it would
have been better for all if it had been named e.g. `alloc` or some such
instead of the very misleading `operator new`. A *`new`-expression`,
with the keyword `new`, like
new YourClass()
is something else entirely.
First, a `new`-expression has TWO SETS OF ARGUMENTS, so it's unlike any
ordinary function call. The first set of arguments, if specified, is
passed to the allocation function, and to some degree determines which
allocation function is used. The second set of arguments is passed to
the constructor of the relevant type, and determines which constructor:
new (allocArg1, allocArg2, allocArg3) Type (constrArg1, constrArg2=
....)
So the form that you used above,
new (p) myclass
is a special case where you're passing one pointer argument to the
allocation function, and no arguments to the constructor.
My intention here is to call the PLACEMENT NEW allocation.
With one pointer argument, if you have included the <new> header, then
ordinarily the allocation function selected by that is the standard
PLACEMENT NEW allocation function that does nothing but return the
specified pointer.
The PLACEMENT NEW allocation function will return the specified
pointer. But,
will it call the default contructor of SomeType?
However, this is not guaranteed unless you restrict
the search for an overload to the global namespace, by writing
::new (p) myclass
When you write
operator new(sizeof *p, p)
you're just calling the allocation function, which in this case is the
standard placement new allocation function (operator new), which does
nothing.
Same question. Will the placement new allocation function invoke the
SomeType's constructor?
When you write
new (a1, a2, a3) SomeType( c1, c2, c3 )
then roughly the following happens:
1. An allocation function that takes the specified arguments, here
(a1, a2, a3), is selected and called.
2. If the allocation function throws an exception then that's that=
..
3. Otherwise, the SomeType constructor that takes the specified
arguments, here (c1, c2, c3), is called.
4. If the constructor throws an exception then
- the allocated memory is deallocated via the CORRESPONDING
deallocation function (operator delete), and this is the =
only
case where a deallocation function with custom args is ca=
lled.
- the exception is propagated.
5. Otherwise, a pointer to the object is the expression result.
Nice conclusion!! It assets to me.
Due to use of the do-nothing allocation function to construct an object
in some existing storage, it's called PLACEMENT NEW.
As a general rule, don't use.
As a general rule, don't even use the ordinary `new`, but preferably use
standard library containers and e.g. `std::string` and so on.
Why? Creat the object from the heap and store the pointer into the
container,
such as, vector, is more efficient than store the object itself into
the container directly.
Cheers & hth.,
- Alf