dynamic memory woes

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Ok first of all i would just like to say that dynamic memory is evil!
This program that i am making wont work and I have written a simpler one to demonstrate my problem.
It looks like when I try to use the copy constructor in the manner:
object1 = object2;
I get errors concerning dynamic memory yet when i do this:
class object2 = object1;

it works just fine??

my code is below:
Rich (BB code):
#include <iostream>
#include <cstring>

using namespace std;

class header
{
public:
    header();
    header (char *);
    header (const header &);
    ~header();
    char * reader();
    void input(char *);


private:
    char *ptr;
};

int main()
{
    header obj1("hello");
    cout << obj1.reader() << endl;
    obj1.input("goodbye");
    cout << obj1.reader() << endl;
    header obj2;
    obj2 = obj1;


    return 0;

}

header::header()
{
    ptr = new char[40];
    strcpy(ptr,"empty");
}
header::header(char *input)
{
    ptr = new char[40];
    strcpy(ptr,"empty");
}
header::~header()
{
    delete [] ptr;
}
header::header(const header &obj)
{
    ptr = new char[40];
    strcpy(ptr,obj.ptr);
}
char * header::reader()
{
    return ptr;
}

void header::input(char * in)
{
    strcpy(ptr,in);
}
 
Last edited:

Mark44

Joined Nov 26, 2007
628
Presumably the line that's causing you problems is obj2 = obj1;
I believe that the reason for this is that your header class does not have an overloaded assignment operator. You mentioned copy constructor, but the overloaded assignment operator has nothing to do with this.

It's important to overload = when you have a class with members that are pointers to memory. If you don't overload =, the compiler will generate one for you, but it probably won't do what you want it to do.

The default = operator (that the compiler generates for your class since you didn't define one) is probably doing this:
  1. Create a new instance of your header class. This instance will have a ptr member.
  2. Copy the value of obj1.ptr to obj2.ptr. This means that both header objects now point to the same dynamically allocated memory. This will definitely cause problems when you free() the memory -- the other pointer will now be pointing to memory that has been freed.

As far as why class object2 = object1; works, I have no idea.
Mark
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Presumably the line that's causing you problems is obj2 = obj1;
I believe that the reason for this is that your header class does not have an overloaded assignment operator. You mentioned copy constructor, but the overloaded assignment operator has nothing to do with this.

It's important to overload = when you have a class with members that are pointers to memory. If you don't overload =, the compiler will generate one for you, but it probably won't do what you want it to do.

The default = operator (that the compiler generates for your class since you didn't define one) is probably doing this:
  1. Create a new instance of your header class. This instance will have a ptr member.
  2. Copy the value of obj1.ptr to obj2.ptr. This means that both header objects now point to the same dynamically allocated memory. This will definitely cause problems when you free() the memory -- the other pointer will now be pointing to memory that has been freed.

As far as why class object2 = object1; works, I have no idea.
Mark
You got it in one Mark!
Thanks for the help. In case your wondering why "class object2 = object1;" works

according to my lecturer and notes this invokes the copy constructor which I have defined. this is the header(const header &) function

Again thanks Mark
 

Mark44

Joined Nov 26, 2007
628
Ok, that makes sense about the copy constructor. It's been a bit over 10 years since I was immersed in this stuff, so I have to think about it to figure out why things happen.
 
Top