Hi,

As per below code snippet it is clear that in below class a if we don't introduce assignment operator
despite of having pointer values it works fine as memory allocation is done explicitly by declaring s2 object on heap .

can anybody suggest, if same canbe achieved without declaring copy constructor explicitly despite of having pointer fields .

class A
{ 
int *m_p;
string str;
public:
A (string );
void setVal(int);
~a (){ 
delete m_p;
       }
}
};


A  s1("s1") ;
A * s2 = new A ("s2");
s2=&s1;
delete s2;

~Mohan

Recommended Answers

All 6 Replies

This doesn't work like you think it does. You never initialise the pointer m_p to anything. Also, on line 17 you leak the memory that you new on line 16. And I think that line 18 will the cause a stack corruption or segfault or something.

Here's what your code does:

  • Line 15: Creates a new A on the stack. s1.m_p doesn't point at anything in particular
  • Line 16: Creates a new A on the heap. s2.m_p doesn't point at anything in particular
  • Line 17: Aim s2 at the adress of s1. This leaks the memory that you assigned in the previous line, since there is now nothing in your program that points to that space in the heap.
  • Line 18: Calling delete on a pointer calls the destructor of the thing that the pointer points to (s1 in this case). It then tries to make that memory available again. However, the memory in question is on the stack. I don't know what happens in this case, but it can't be good!

The default copying of a struct is always an element-by-element value copy. This means that if you have pointer members, the values of those pointers are copied, not the data that the pointers point to. If you want to do this, then you have to make your own copy constructor. Allocating a struct using new doesn't change this.

Hi
Below is the complete code snippet, I never faced the memory leak issue except Line18.
which i commented now.

can any body suggest if same way can we perform the copy constructor as well.

#include<iostream.h>
#include<conio.h>

using namespace std;

class Derived1 
{ 
int *m_p;
string str;
public:
Derived1 ( string );

//Derived1& operator =( const Derived1 &obj);
void show()const  {

cout<<"integer="<< *m_p<<endl;
cout<<"str"<<str<<endl;
}
void setVal(int);
~Derived1(){ 

delete m_p;

}
};

void Derived1::setVal(int val)
{
*m_p = val;

}


 Derived1::Derived1(string str)
 {    
     this->str= str;
      m_p=new int(0);
 }

/*Derived1& Derived1::operator=(const Derived1 &obj)
{
int *ptr= new int (*obj.m_p);
this->str=obj.str;
delete m_p;
m_p=ptr;
return *this;
}*/


int main()
{

  Derived1 s1("s1") ;

  s1.setVal(20);
  s1.show();
  Derived1* s2 = new Derived1("s2");
  s2->setVal(100);
  s2->show();
  s2=&s1;

  s1.show();
  s2->show();
  getch();


}

Line 60 does not achieve a copy of s1. The calls to show() display the same thing for s1 and s2, but that's because they're accessing the same object, not two objects that have the same values. Try this addition to your main:

int main()
{
  Derived1 s1("s1");

  s1.setVal(20);
  s1.show();

  Derived1* s2 = new Derived1("s2");
  s2->setVal(100);
  s2->show();

  s2=&s1;   // this is aiming s2 at the memory for s1 (and leaking the old memory for s2 that you newed)

  s1.show();     // Shows s1
  s2->show();    // Also shows s1!

  // Now try to change s2
  s2->SetValue( 5 );

  s1.show();    // Now s1 has the same value that you just put into s2!

  getch();
}

Hi
Below is the complete code snippet, I never faced the memory leak issue except Line18.
which i commented now.

can any body suggest if same way can we perform the copy constructor as well.

#include<iostream.h>
#include<conio.h>

using namespace std;

class Derived1 
{ 
int *m_p;
string str;
public:
Derived1 ( string );

//Derived1& operator =( const Derived1 &obj);
void show()const  {

cout<<"integer="<< *m_p<<endl;
cout<<"str"<<str<<endl;
}
void setVal(int);
~Derived1(){ 

delete m_p;

}
};

void Derived1::setVal(int val)
{
*m_p = val;

}


 Derived1::Derived1(string str)
 {    
     this->str= str;
      m_p=new int(0);
 }

/*Derived1& Derived1::operator=(const Derived1 &obj)
{
int *ptr= new int (*obj.m_p);
this->str=obj.str;
delete m_p;
m_p=ptr;
return *this;
}*/


int main()
{

  Derived1 s1("s1") ;

  s1.setVal(20);
  s1.show();
  Derived1* s2 = new Derived1("s2");
  s2->setVal(100);
  s2->show();
  s2=&s1;

  s1.show();
  s2->show();
  getch();


}

Hi,
Thanks for your explanation but still i really confused about memory leak thing as i never ever
faced memory leak issue even while trying with compiler generated assignment operator versions.
Could you help me out to reproduce the same.
Note:I am using Dev-c++4.9.9.2 compiler .

can any body suggest if same way can we perform the copy constructor as well.

Written here, in your other thread:
Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.