Explicit ctor/dtor calls

Discussion in 'C++' started by Jacques Labuschagne, Jul 20, 2003.

  1. Hi all,
    Is it legal to kill an object and build a new one of the same type in its
    memory?

    class A{
    int i;
    public:
    explicit A(int value): i(value){}
    };

    int main(){
    A a(3);
    a.~A();
    new (&a)A(2);
    }

    --
    There is this special biologist word we
    use for 'stable.' It is 'dead.' -- Jack Cohen
    Jacques Labuschagne, Jul 20, 2003
    #1
    1. Advertising

  2. "Jacques Labuschagne" <> wrote in message
    news:1840039.ZJxocY03RF@klesk...
    > Hi all,
    > Is it legal to kill an object and build a new one of the same type in its
    > memory?
    >
    > class A{
    > int i;
    > public:
    > explicit A(int value): i(value){}
    > };
    >
    > int main(){
    > A a(3);
    > a.~A();
    > new (&a)A(2);
    > }
    >


    Seems OK to me. There are a few issues here but none that you've raised in
    your example code.

    john
    John Harrison, Jul 20, 2003
    #2
    1. Advertising

  3. Jacques Labuschagne wrote:
    > Hi all,
    > Is it legal to kill an object and build a new one of the same type in its
    > memory?
    >
    > class A{
    > int i;
    > public:
    > explicit A(int value): i(value){}
    > };
    >
    > int main(){
    > A a(3);
    > a.~A();
    > new (&a)A(2);
    > }
    >


    Yes, it is legal. Such a case is described in the C++ standard (3.8/7).
    The name of the object and any pointers or references to it still remain
    valid after such an operation.
    Janusz Szpilewski, Jul 20, 2003
    #3
  4. "Jacques Labuschagne" <> wrote in message
    news:1840039.ZJxocY03RF@klesk...
    > Hi all,
    > Is it legal to kill an object and build a new one of the same type in its
    > memory?
    >
    > class A{
    > int i;
    > public:
    > explicit A(int value): i(value){}
    > };
    >
    > int main(){
    > A a(3);
    > a.~A();
    > new (&a)A(2);
    > }

    Let us suppose the first constructor succeeds and the second fails with the
    exception (it is not your case indeed but let us debate in general). A
    compiler must call the destructor for the successfully created a(3) local
    variable when it leaves the scope and he knows that the construction was
    successful. But your unsuccessful placement new really confuses it and, I
    believe, you may expect crashes, because if the second construction throws
    no destructor shall be called but it would be called for a(3).
    So, IMO, your solution may appear to be dangerous in general and I would
    recommend you to use the traditional approach of using a buffer of proper
    amount of chars with the subsequent placement new if you are interested in
    the final result.

    --
    With regards,
    Michael Kochetkov.
    Michael Kochetkov, Jul 20, 2003
    #4
    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. Karl Heinz Buchegger

    Ctor/Dtor order

    Karl Heinz Buchegger, Jul 30, 2003, in forum: C++
    Replies:
    3
    Views:
    1,232
    Greg Comeau
    Jul 30, 2003
  2. pmastroianni

    Does a ctor/dtor occupy memory

    pmastroianni, Apr 1, 2005, in forum: C++
    Replies:
    1
    Views:
    350
    Howard
    Apr 1, 2005
  3. Grizlyk
    Replies:
    8
    Views:
    480
    Grizlyk
    Nov 29, 2006
  4. Replies:
    5
    Views:
    387
    Kai-Uwe Bux
    Nov 29, 2006
  5. , India
    Replies:
    2
    Views:
    311
    Stefan Ram
    Mar 30, 2010
Loading...

Share This Page