0

if constructor is used for allocating memory.

In the following program it does not work like that. See

#include <iostream>

using namespace std;

class Demo
{
    int i;
public:
    Demo()
    {
        cout<<"\nDefault contructor called";
    }
    Demo(int x)
    {
        i = x;
        cout<<"\nParameter contructor called";
    }
    void Display()
    {
        cout<<endl<<"i = "<<i<<endl;
    }
};

int main()
{
    Demo *demo = new Demo[5]; // = {1,2,3,4,5};

    int i;
    cout<<endl<<endl<<"First time";
    cout<<endl<<"Addresses are "<<endl;
    for( i=0;i<5; i++)
    {
        cout<<endl<< &demo[i];
    }
    cout<<endl<<endl<<"first time assigning values";    
    for( i=0;i<5; i++)
    {
        demo[i]= i;
    }

    cout<<endl<<endl<<"\nAfter first assignment";
    cout<<endl<<"Addresses are "<<endl;
    for( i=0;i<5; i++)
    {
        cout<<endl<< &demo[i];
    }
    cout<<endl<<endl<<"Second time assigning values";    
    for( i=0;i<5; i++)
    {
        demo[i]= i+5;
    }
    cout<<endl<<endl<<" After Second assignment ";
    cout<<endl<<"Addresses are "<<endl;
    for( i=0;i<5; i++)
    {
        cout<<endl<< &demo[i];
    }

    for( i=0;i<5; i++)
    {
        demo[i].Display();
    }
    return 0;
}

Output:

Default contructor called
Default contructor called
Default contructor called
Default contructor called
Default contructor called

First time
Addresses are 

0x8281008
0x828100c
0x8281010
0x8281014
0x8281018

first time assigning values
Parameter contructor called
Parameter contructor called
Parameter contructor called
Parameter contructor called
Parameter contructor called


After first assignment
Addresses are 

0x8281008
0x828100c
0x8281010
0x8281014
0x8281018

Second time assigning values
Parameter contructor called
Parameter contructor called
Parameter contructor called
Parameter contructor called
Parameter contructor called

 After Second assignment
Addresses are 

0x8281008
0x828100c
0x8281010
0x8281014
0x8281018
i = 5

i = 6

i = 7

i = 8

i = 9

Here the constructor is called three time and the memory address are same, means it is not allocating new memory and uses the same address. Why?

Why is the constructor called more than one time?

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53

2 Answers2

1

When you do this:

demo[i]= i;

i is used to construct a temporary object using the constructor which takes an int. This temporary is then assigned to demo[i]. The assignment does not result in demo[i] being reconstructed as a new object with a different address (objects can't be reconstructed, and can never change their address), it simply results in a memberwise assignment from the members of the temporary to the members of demo[i] (unless you provide an assignment operator which does something different, which you didn't).

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

Thinking the constructor allocates the memory for your object is a fundamental misunderstanding. When the constructor is called you already have the memory allocated, either on the stack or on the heap. The constructor only prepares this memory for using it afterwards.

However, the constructor is often responsible for allocating further memory for resources used by your object, i.e.

class Container
{
public:
    Container()
        : ptr_(new int[5])
    {}
    // ...
private:
    int* ptr_;
}
filmor
  • 30,840
  • 6
  • 50
  • 48
  • It means you are saying that constructor can be called more than one time. first call will allocte memory and other call will use that memory.. am I right ?? – user2814127 Sep 25 '13 at 08:41
  • Nope. It means that allocating memory and calling the constructor are two independent actions the runtime executes after each other when you create an object. You can in fact decouple this yourself by using placement new: `void* ptr = new char[sizeof(Demo)]; new (ptr) Demo;` (see for example http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new). – filmor Sep 25 '13 at 08:49