-2

In C++, suppose I called a constructor two times for the same object. Then does the compiler allocate memory two times or simply once? Is it even legal?

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 12
    Can you give a concrete example of how you'd do this? – tadman Mar 20 '18 at 18:47
  • To do so, click the [edit](https://stackoverflow.com/posts/49392014/edit) link. – Ron Mar 20 '18 at 18:48
  • Possible duplicate of [Calling a constructor to re-initialize object](https://stackoverflow.com/questions/2166099/calling-a-constructor-to-re-initialize-object) – 001 Mar 20 '18 at 18:50
  • you cannot call the constructor more than once for a one object but you can call it recursively. – Raindrop7 Mar 20 '18 at 18:50
  • Calling the constructor each time will cause it to allocate memory each time. Although you can use 'placement new' to place the new memory allocation on top of where the previous one was for that object. – Thomas Phan Mar 20 '18 at 18:51
  • 1
    The answer depends entirely on how you managed to trick the compiler into generating code that you claim calls the constructor on the same object twice. Since you haven't shown that code, it's impossible to answer this question. – Pete Becker Mar 20 '18 at 19:09
  • @tadman I'm in a similar situation. My code (I'd like to know if it is allowed): `myClass * someFunction(int arg) { static myClass theObject(arg); return &theObject; }` – Martin Rosenau Apr 13 '22 at 16:15
  • @MartinRosenau It's my understanding that `static` variables like this are constructed once, like a singleton. Depending on the implementation by the compiler, when the initialization actually occurs, this may or may not be thread safe. – tadman Apr 13 '22 at 16:17

1 Answers1

3

You can call a constructor multiple time on the same allocated storage, as long as you also call a destructor. But don't do it. I'll assume this is a purely theoretical question:

struct Object { virtual ~Object() {} };
{                                 // some scope
    Object obj;                   // constructed on automatic storage location
    obj.~Object();                // destructed
    new (&obj) Object;            // new object constructed on same storage
}                                 // destructor automatically called

Skipping the manual destruction might lead to undefined behaviour for objects with non trivial destructor1. Note this is calling for troubles; as an exercise: what happens if an exception is raised after obj has been manually destructed and obj has been manually reconstructed2?


1) [basic.life]/5

For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.

2) Answer: [basic.life]/9.

YSC
  • 38,212
  • 9
  • 96
  • 149