new operator doesn't work?

Discussion in 'C++' started by Stanley Rice, Feb 17, 2012.

  1. Stanley Rice

    Stanley Rice Guest

    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.
     
    Stanley Rice, Feb 17, 2012
    #1
    1. Advertising

  2. 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
     
    Alf P. Steinbach, Feb 17, 2012
    #2
    1. Advertising

  3. Stanley Rice

    Stanley Rice Guest

    On Feb 17, 2:22 pm, "Alf P. Steinbach" <alf.p.steinbach
    > 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
     
    Stanley Rice, Feb 17, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. balasubramaniam
    Replies:
    2
    Views:
    435
    Andrey Tarasevich
    Nov 30, 2003
  2. Replies:
    4
    Views:
    369
    Victor Bazarov
    Nov 9, 2005
  3. heng
    Replies:
    8
    Views:
    335
    red floyd
    Dec 5, 2006
  4. xmllmx
    Replies:
    6
    Views:
    416
    xmllmx
    Feb 3, 2010
  5. Replies:
    2
    Views:
    100
Loading...

Share This Page