Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new operator doesn't work?

Reply
Thread Tools

new operator doesn't work?

 
 
Stanley Rice
Guest
Posts: n/a
 
      02-17-2012
The global new operator function has three overloaded prototype, and one of it
is:
void * operator new(size_t s, void *p) throw();
The document says that the above function doesn't allocate memory, instead, it
just call the constructor of the given type and place the address of the
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 message printed.

************************************************** *********************
What's going on there? Am I misunderstand something?

Thanks in advance.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-17-2012
On 17.02.2012 07:06, Stanley Rice wrote:
> The global new operator function has three overloaded prototype, and one of it
> is:
> void * operator new(size_t s, void *p) throw();
> The document says that the above function doesn't allocate memory, instead, it
> just call the constructor of the given type and place the address of the
> 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 message 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.

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

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 called.
- the exception is propagated.

5. Otherwise, a pointer to the object is the expression result.

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.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Stanley Rice
Guest
Posts: n/a
 
      02-17-2012
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 one of it
> > is:
> > * * *void * operator new(size_t s, void *p) throw();
> > The document says that the above function doesn't allocate memory, instead, it
> > just call the constructor of the given type and place the address of the
> > 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 message 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 called.
> * * * - 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the key differences between operator new and operator new[]? xmllmx C++ 6 02-03-2010 04:11 PM
Allocation with new operator and Destroying with free operator saikishore.vanga@wipro.com C++ 8 12-22-2005 01:35 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57