Doubt in dynamic memory allocation

3

In the code below, releasing memory from B also frees from A?

int* A = new int[4]; 
int *B = A; 
delete[] B;
    
asked by anonymous 09.06.2014 / 16:47

1 answer

2

Yes, because both pointers point to the same memory region.

Keep in mind that the pointer only indicates where your data is, delete B does not delete A, delete the content also pointed to by A.

After allocation: Afterthememoryisreleased:

Also note that A continues to point to the memory region, while B after delete points to an explicitly invalid region.

    
09.06.2014 / 16:57