[CSI] Question about power point...

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Nov 17 09:09:04 EST 2016


> 
> I have a question from slide 5 of the Dynamic Memory Allocation power point.
> The code directly under the text "is this a memory leak?" .... Is it a
> memory leak?

it is not not. A memory leak happens when no pointer stores the
address of a dynamic variable. In the example on the slide: 

   int *ptr1,*ptr2 = new int; // first dynamic variable
   ptr1=ptr2;  // ptr1 holds the address of the fist dynamic variable
   ptr2 = new int; // second dynamic variable

Even though "ptr2" got reassigned to hold an address of the second
dynamic variable, "ptr1" still holds the address of the first. That
is, the first dynamic variable is not leaked.

Thanks,
--
Mikhail


More information about the cs13001 mailing list