A very very quick question

If I delete a pointer (it can be a pointer to any kinds of data type, intrinsic or user-defined class type), will the whole data/class object pointed by this pointer be deleted/deallocated?

For example, if I have defined a class CC and constructed an object of CC, which is pointed by a pointer COBJ
 
COBJ = new CC();


Will
 
delete COBJ

delete/deallocate all the space allocated for this CC object?

Thank you for your inputs!
closed account (zb0S216C)
Once delete is invoked, the destructor of the class is invoked. It's the destructor's responsibility to de-allocate any resources allocated by the members of the class, not deletes.

So, the answer to your question is yes. delete will release the allocated resources pointed to by COBJ when invoked.

Wazzak
Last edited on
Wazzak, thank you for your prompt reply.

1. what if this is pointer to an C-style array? The whole array will be freed, right?

2. Let me be more aggressive. What if the author of class CC forgot to define a destructor? I suppose that I am get to run out of memory if the constructor is called many times. So is there any way in which we can detect this kind of memory leaking, suppose that I don't want to go into class CC and take my precious time to check if the destructor is defined or not.

Thank you!
The compiler will automatically use a default destructor. However, if inside your CC class you allocate memory from heap this will not be freed, only the COBJ object itself is guaranteed to be deleted by C++ Runtime.
1. what if this is pointer to an C-style array? The whole array will be freed, right?
In this case, you have to use delete[], but only if the array was allocated with new[].

What if the author of class CC forgot to define a destructor? I suppose that I am get to run out of memory if the constructor is called many times. So is there any way in which we can detect this kind of memory leaking, suppose that I don't want to go into class CC and take my precious time to check if the destructor is defined or not.
If CC::~CC() wasn't defined by the developer and the object owns pointers to dynamic memory, then the object will leak memory when it's destructed. There's no way to detect or fix memory leaks from inside a program.
Note that not every class needs to have a destructor manually defined.
closed account (zb0S216C)
subjugater wrote:
1. what if this is pointer to an C-style array? The whole array will be freed, right? (sic)

A C-Style array that has normal, non-pointer elements would require a single delete[] invocation. For example:

1
2
3
int *Array( new int[5] );

delete [] Array;

In this segment, Array is constructed with 5 int elements, then destroyed straight after. When delete[] was invoked, the region of memory (region size: 5 x sizeof( int )) pointed to by Array was released, allowing the previously allocated bytes to be used by another application. However, if the elements were pointers to allocated memory, that's a different story. For example:

1
2
3
4
5
6
int **Array = nullptr;

Array = ( new int* [5] );
Array[0] = ( new int[4] );

delete [] Array;

This code segment is different from the last. First, Array allocates and array of 5 int pointers, each of which are uninitialized. Immediately following the first DMA, the first pointer of Array is assigned a newly allocated region of ( 4 x sizeof( int )). When delete[] is invoked, the memory allocated by Array0 is never released, because that's the programmers responsibility.

subjugater wrote:
What if the author of class CC forgot to define a destructor? (sic)

If any of the members of CC allocated memory dynamically, then it's the authors responsibility, not the clients. If the author does in fact forget to release the memory within the destructor, then that marks them as an amateur, since any experienced programmer would remember. Regardless, delete will not check if members of a class point to DAM.

subjugater wrote:
So is there any way in which we can detect this kind of memory leaking (sic)

There's software out there that detects leaks within the OS, but I know of none. I rely on Windows 7's integrated memory leak detection abilities.

subjugater wrote:
I don't want to go into class CC and take my precious time to check if the destructor is defined or not. (sic)

You shouldn't have to.


Wazzak
Last edited on
There are tons of memory tools available for debugging memory issues. Valgrind is one of the more popular ones. Some static analysis tools will even catch missing deletes/frees. Memory leaked from a process is reclaimed by the OS after the process exits, so your programs shouldn't have ill-effects on the rest of the system.
Topic archived. No new replies allowed.