1

I am trying to understand the memory used in a particular type of initialization. Here is an example for the question:

#include <iostream>
using namespace std;

class Example {
    int n;
public:
    Example(int number = 0)
    {
        n = number;
    }
};

int main() {
    Example *obj1 = new Example(1); //dynamic allocation - 1 instance is created and the object pointer points to the address
    Example obj2(2); // automatic allocation - a single instance of the object is created with the given value
    Example obj3 = Example(3); //automatic allocation - with copy constructor from an object initialized with a the class constructor

    delete obj1; //de-allocating dynamically allocate memory
}

I am interested in knowing how obj3 is being instantiated. Does the usage of the equal to sign mean that we are using a copy constructor here? If so, is Example(3) a separate instance (what is its scope)?

If they are separate instances, then it seems the initialization of obj2 more optimized, isn't it?

Edit: Fixed usage of incorrect terminology - static to automatic

Aditya
  • 439
  • 4
  • 14
  • It is called http://en.cppreference.com/w/cpp/language/copy_initialization – Slava Jan 23 '18 at 16:41
  • 3
    What you called "static" allocation is, in fact, automatic allocation. Static allocation is another one (for global and static variables). – Quentin Jan 23 '18 at 16:41
  • 1
    Copy constructor if on the declaration line, assignment operator (`=`) otherwise. – goodvibration Jan 23 '18 at 16:43
  • No it isn't. This stuff is very complicated in all the detail but in this case compilers are *required* to optimize away the 'separate instance'. Cases 2 and 3 are the same. – john Jan 23 '18 at 16:43
  • @john So, there is no call to the copy constructor of `Example`? – Aditya Jan 23 '18 at 16:51
  • 2
    @john that is only true in C++17. Since C++11 and until C++17, the compiler is allowed, but not required to elide the copy/move. – patatahooligan Jan 23 '18 at 16:56

1 Answers1

4

I am interest in knowing how obj3 is being instantiated. Does the usage of the equals to sign mean that we are using a copy constructor here? If so, is Example(3) a separate instance (what is its scope)?

This expression is known as copy initialization and your case with obj3 dsescribed here:

First, if T is a class type and the initializer is a prvalue expression whose cv-unqualified type is the same class as T, the initializer expression itself, rather than a temporary materialized from it, is used to initialize the destination object: see copy elision (since C++17)

So no copy ctor involved and obj3 is initialized directly (passing 3 to its ctor) for the current standard. Before C++17 temporary could be created but most probably it would be eliminated by optimizer.

Note: you incorrectly stated in comment for automatic variables to use static memory allocation. Please see details here Difference between static memory allocation and dynamic memory allocation

Slava
  • 43,454
  • 1
  • 47
  • 90