0

I'm trying to understand Deep Copy and write this program.

#include <iostream>
#include <string.h>
using namespace std;
char a[10];
class String
{
    public:
    char *ptr;
        String()
        {
            ptr = a;
        }
        void setString(const char *str)
        {
            strcpy(ptr,str);
        }   
        void operator=(String k){
            ptr = new char[10];
            strcpy(ptr, k.ptr);
        }
        char* getString()
        {
            return ptr;
        }
};
class student
{
    public:
    String name;
        student(){  }
        student (const student &O){
            name=O.name;
        }
};
int main(){
    student obj;
    obj.name.setString("ABC");
    student obj1=obj;
    obj1.name.setString("NEW");
    cout << obj.name.getString() << endl;
    cout << obj1.name.getString() << endl;
}

It's working fine. But I'm trying to call Destructor to free the memory and when I write Destructor then program not run properly.

~String(){
            delete ptr;
        }

I know it's could be due to ptr = a; I already tested with other short examples and ptr=a caused the problem when Destructor called.

  1. Is everything fine instead of Destructor?
  2. How I can free the memory?

Note: This Program is just to understand the Deep copy.

  • 1
    Think a little bit about your default constructor for a while. It makes `ptr` point to the global array `a` (though I don't see the reason for this) which you haven't allocated. What do you think would happen when you try to delete that memory? Also, you `delete` what you `new`, and `delete[]` what you `new[]`. – Some programmer dude Aug 01 '15 at 20:20
  • @JoachimPileborg Thanks, I already write that there could be problem. But I want to ask, is there any memory leakage? if yes then How I can solve that problem. –  Aug 01 '15 at 20:23
  • 1
    You need to [**read this**](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) and do a little more research afterward. – WhozCraig Aug 01 '15 at 20:25
  • 1
    Every time the assignment operator is called you have a memory leak (unless you `delete[]` the previously allocated memory). And possible *undefined behavior* is the source string is longer than nine characters. – Some programmer dude Aug 01 '15 at 20:28
  • @JoachimPileborg So after `strcpy(ptr, k.ptr);` this line (in = operator) I'm thinking about to add `delete ptr;` that could solve the problem? I already tried and program worked, But just confirming about memory leakage. –  Aug 01 '15 at 20:30
  • Once again, think about what `ptr` is initially assigned to point to. And `delete` is not the same as `delete[]`. And deleting the memory *after* you just allocated it, what's the point of that? – Some programmer dude Aug 01 '15 at 20:31
  • @JoachimPileborg Oh sorry by mistake.. `delete[] ptr;` would be ok ( in = operator) ? that could solve the memory leakage problem? –  Aug 01 '15 at 20:33
  • @JoachimPileborg Sorry may be I'm bothering you, But it's urgent.. `delete[] ptr;` would be ok ( in = operator) ? that could solve the memory leakage problem in this program? –  Aug 01 '15 at 20:40
  • Yes, ***iff*** you have allocated the memory in the first place, and remember that there's one case where you haven't. – Some programmer dude Aug 01 '15 at 20:48

1 Answers1

2

having a destructor would help - but as was pointed out in comments you have inconsistent handling of the internal storage (sometimes pointing to global sometimes pointing to dynamic mem)

so some of the things you really want to consider are:

  • use consistent allocation scheme
  • use NULL when not allocated
  • add a destructor
nhed
  • 5,774
  • 3
  • 30
  • 44