user2814127
user2814127

Reputation: 1

Constructor in called for allocate memory?

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?

Upvotes: 0

Views: 186

Answers (2)

filmor
filmor

Reputation: 32182

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_;
}

Upvotes: 1

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

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).

Upvotes: 1

Related Questions