If there is a pointer member in a class. Eg.:

Class XYZ
{
            Int *x;
            Char *c;
}

Then in such a case what all precautations do we need to take while handling such a member and the class?

For myself, defining a pointer means careful handling of memory to prevent leakage. Memory allocation and memory releasing have to be handled manually in C++, unlike managed code. When you have a pointer, remember to free up the meory when they are no longer needed. However, do keep in mind that once you free up the memory, do not try to retrieve its value. You will get unexpected behaviour.

For myself, defining a pointer means careful handling of memory to prevent leakage. Memory allocation and memory releasing have to be handled manually in C++, unlike managed code. When you have a pointer, remember to free up the meory when they are no longer needed. However, do keep in mind that once you free up the memory, do not try to retrieve its value. You will get unexpected behaviour.

Thanks for the reply.
Even I too think so.
We can allocate memory in the constructor and in the destrucor we must ensure that we are releasing the memory and setting the pointer to null.

This is a interview question asked to me.
I had replied the same as mentioned here but that did not satisfied the interviewer
So I want some more points on this.
Any more ideas?

How about when copying one object of XYZ to another? By default objects are copied member to member. You might need to add something to change this.

This article has been dead for over six months. Start a new discussion instead.