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