Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
4.80/5 (2 votes)
See more:
Hi people,
I have following question on delete [] operator.
Imagine I have two classes.
C++
Class A
{
....

char * data;

~A() // destructor
{
if (data) delete [] data;
}

}

 

class B
{
A * objectA;

...

~B() // destructor
{
delete objectA;
}

void function()
{
// now imagine here I create a pointer to A object
objectA = new A;
objectA->data = new char [100];
}
}

My question is. Imagine
funtion()
was called. And afterwards class B destructor was called - the one that deletes object A -- my question is, will this imply that pointer->data variable will also be deleted? (i.e., won't I need to separately
delete [] data 
-- later on ? -- I seem to get a run time error in class A destructor once
delete [] data
is being called in this code)

thanks
Posted
Updated 29-Oct-12 7:10am
v5
Comments
Eugen Podsypalnikov 29-Oct-12 12:35pm    
// Will this imply that pointer->data variable will also be deleted?
- Yes, the pointer will be deleted, in its uninitialized case as well :)

// I seem to get a run time error in class A destructor once is being called in this code
- There is no call of ~A() in this (posted above) code :)

Set your data pointer to 0 in the constructor of A and you will be fine. Same for your class B and objectA.

In your function, there is no object of type B being constructed. But let's assume you added a line

C++
B b;
b.objectA = new A;


Then the constructor of B would delete the A object, which in turn would call A's destructor.

By the way, you seem not have compiled your code ever. The keyword class in C++ is spelled with a lower-case c. And you would need to a public: line at the beginning, or your member variables will not be accessible from outside code.
 
Share this answer
 
Comments
[no name] 29-Oct-12 13:13pm    
Hi thanks I had some error in the code which I corrected now above.

My question was following exactly.

nv3>>"Then the constructor of B would delete the A object, which in turn would call A's destructor."

When the destructor would delete A object, would this imply that 'data' variable of class A was automatically deleted too??
nv3 29-Oct-12 15:03pm    
Yes, A's destructor will be called and hence, if data is non-zero, a "delete [] data" will be executed.
[no name] 29-Oct-12 15:52pm    
I will rephrase my question a bit. Imagine there is no 'delete[] data;' in the destructor of A. Now, I have the same question, would calling destructor of B (as in above example), automatically: delete [] objectA->data?
nv3 29-Oct-12 17:28pm    
No, it would not. When the destructor of B is called, class B has no idea of what to do when an object of type A is to be destroyed. That knowledge sits excusively in A's destructor. So all B does is call A's destructor. And if that destructor leaves a memory leak behind, B can nothing do about it.
CPallini 29-Oct-12 16:11pm    
5.
You code doesn't compile. The following one does and reproduces the runtime error:
C++
class A
{
    //....
public:
    char * data;

    ~A() // destructor
    {
        if (data) delete [] data;
    }

};

class B
{
    A* objectA;

public:

    ~B() // destructor
    {
        delete objectA;
    }
public:
    void function()
    {
        // now imagine here I create a pointer to A object
        A * pointer = new A;
        pointer->data = new char [100];
    }
};
int main()
{
    B b;
    b.function();
}



You get an error because:
  1. You never allocate memory for objectA but you delete it inside B's ctor.
  2. You never initialize the objectA's member data

(note: memory associated to pointer temporary variable leaks).
A fix would be
C++
class A
{
public:
  //..
  A():data(NULL){}
  //..
};

class B()
{
public:
//..
B():objectA(NULL){}
};


(you may use nullptr instead of NULL if you have an updated compiler)

On the overall your design doesn't look terribly robust.
 
Share this answer
 
Comments
[no name] 29-Oct-12 13:16pm    
Hi Cpallini thanks I had error in my code above which I changed. Maybe you can look at it now.
Doesn't this: A():data(NULL){} mean same as data = NULL? But then do I have the right to invoke delete [] data?? thanks

In general if I have never called 'new' operator for some char *p variable, can I ever invoke delete[] on it?? thanks
CPallini 29-Oct-12 13:57pm    
Yes it is practically the same, but you'll better use the first one.
Yes, tt is safe to delete a NULL pointer (delete perform the check itself).
Sergey Alexandrovich Kryukov 29-Oct-12 15:44pm    
5ed.
--SA
CPallini 29-Oct-12 16:10pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900