dynamic memory woes

Discussion in 'Programmer's Corner' started by mentaaal, Feb 9, 2009.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4. #include <iostream>
    5. #include <cstring>
    6.  
    7. using namespace std;
    8.  
    9. class header
    10. {
    11. public:
    12.     header();
    13.     header (char *);
    14.     header (const header &);
    15.     ~header();
    16.     char * reader();
    17.     void input(char *);
    18.  
    19.  
    20. private:
    21.     char *ptr;
    22. };
    23.  
    24. int main()
    25. {
    26.     header obj1("hello");
    27.     cout << obj1.reader() << endl;
    28.     obj1.input("goodbye");
    29.     cout << obj1.reader() << endl;
    30.     header obj2;
    31.     obj2 = obj1;
    32.  
    33.  
    34.     return 0;
    35.  
    36. }
    37.  
    38. header::header()
    39. {
    40.     ptr = new char[40];
    41.     strcpy(ptr,"empty");
    42. }
    43. header::header(char *input)
    44. {
    45.     ptr = new char[40];
    46.     strcpy(ptr,"empty");
    47. }
    48. header::~header()
    49. {
    50.     delete [] ptr;
    51. }
    52. header::header(const header &obj)
    53. {
    54.     ptr = new char[40];
    55.     strcpy(ptr,obj.ptr);
    56. }
    57. char * header::reader()
    58. {
    59.     return ptr;
    60. }
    61.  
    62. void header::input(char * in)
    63. {
    64.     strcpy(ptr,in);
    65. }
    66.  
     
    Last edited: Feb 9, 2009
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    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
     
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    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
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    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.
     
Loading...