Delete [] and memory leak

I'm curious about the delete [] operator in C ++. (I am using Visual Studio 2005).

I have an unmanaged DLL called by a managed DLL. When I close this program after performing several tasks during debugging, I get many (thousands?) Memory leaks, mostly 24 bytes - 44 bytes. I suspect this may be due to some unmanaged dll that I have.

In any case, from what I understand, if I have the following code:

char* pointer = new char[500]
/* some operations... */
delete[] pointer;

Then all the memory for him is freed correctly, am I right?

What happens when I have the following code:

char* pointer = new char[500];
char* pointerIt = pointer;
/* some code perhaps to iterate over the whole memory block, like so */
for (int i = 0; i < 250; i++){ // only iterate halfway
    *pointerIt = 0;
    pointerIt++;
}

delete[] pointer;

Is the memory pointed to by the pointer deleted correctly? Thus, this means that pointerIt is no longer pointing to actual memory. But this is normal, because I can set both pointers to NULL, right?

, , :

char* pointerFirstPosition = new char[500];
char* pointerIt = pointerFirstPosition;

for (int i = 0; i < 250; i++){ // only iterate halfway
    *pointerIt = 0;
    pointerIt++;
}

delete[] pointerIt; // delete the pointer iterator...

, pointerIt pointerIt +500? , pointerFirstPos, FirstPos +500?

?

, .

,

+3
2

:

char* pointer = new char[500] 
/* some operations... */ 
delete[] pointer;

, ?

.

:

char* pointer = new char[500];
char* pointerIt = pointer;
/* some code perhaps to iterate over the whole memory block, like so */
for (int i = 0; i < 250; i++){ // only iterate halfway
    *pointerIt = 0;
    pointerIt++;
}

delete[] pointer;

, , ? , , pointerIt . , NULL, ?

pointer , . pointer pointerIt . - , . , . * . - , .

:

, , . undefined , . ? , ? , ...? .

, . , delete[], , , delete.

, :

char* pointer = new char[500];
char* pointerIt = pointer;
//This is fine because you are deleting the same address:
delete[] pointerIt;
//The memory that both variables point to is freed, do not try to free again
+4
0

Source: https://habr.com/ru/post/1715039/


All Articles