C++ Dangling Pointers and Memory Leaks

  • A+
Category:Languages

I'm having a hard time understanding how to tell between dangling pointers and memory leaks. I have a few questions on a recent assignment that are puzzling me, and after reading into it, I am still puzzled. I don't want someone to do my homework for me, I want to be able to understand why something is what it is, if that makes sense.

So, the homework:


Given the declarations:

int *ptrA, *ptrB; 

Tell whether each code segment below results in a memory leak, a dangling pointer, or neither. Draw pictures to help.

  1. I'm guessing that this is fine, as ptrA is already pointing to something in memory, so this one is neither a dangling pointer or a memory leak.
ptrA = new int; ptrB = new int; *ptrA = 345; ptrB = ptrA; 
  1. I'm guessing that this is a dangling pointer because 345 was deallocated from memory, so ptrB points to nothing.
ptrA = new int; *ptrA = 345; ptrB = ptrA; delete ptrA; 
  1. This is where I'm complete lost. Does the last line mean that the pointer is pointing to another pointer? Not sure what the implications of this would be.
ptrA = new int; ptrB = new int; *ptrA = 345; *ptrB = *ptrA; 
  1. Like the previous question, I'm not sure what pointing to a pointer means, or if I even understand what this is achieving.
ptrA = new int; ptrB = new int; *ptrA = 345; ptrB = new int; *ptrB = *ptrA; 
  1. I know this is a dangling pointer, but I don't know why. Is it because the pointer is pointing to a local variable that went out of scope when the function finished?
ptrA = LocationOfAge(); 

where function LocationOfAge is defined as:

int *LocationOfAge() {   int age = 21;   return &age; } 

Thanks for anyone willing to help.

 


The rules of the game:

  • For every new Type draw a box. Put a question in the box (you don't know what is in there).
  • For every delete p cross out the box pointed to by p.
  • For every a = b (where there are no stars) draw a line from variable a to a box b.
  • For every *x = y write y inside the box pointed at by x.
  • For every *x = *y read the content of the box y and put a copy in x

The result:

  • When you have a box with no variables pointing at it you have a leak.
  • If you have a pointer that does not point at a box (or a crossed out box) you have a dangling pointer.

The first problem:

ptrA = new int; ptrB = new int; *ptrA = 345; ptrB = ptrA; 

Lets do this line by line:

ptrA = new int;

// Part 1 has a new so draw a box               *********               *   ?   *               *********  // Part 2 assignment to variable add a line ptrA -------> *********               *   ?   *               ********* 

ptrB = new int;

// Part 3 has a new so draw another box ptrA -------> *********               *   ?   *               *********                *********               *   ?   *               *********  // Part 4 assignment to variable add a line  ptrA -------> *********               *   ?   *               *********  ptrB -------> *********               *   ?   *               ********* 

*ptrA = 345;

ptrA -------> *********               *  345  *               *********  ptrB -------> *********               *   ?   *               ********* 

ptrB = ptrA;

ptrA -------> *********         |     *  345  *         |     *********         | ptrB ----     *********               *   ?   *               ********* 

Seems like you have a leaked box. i.e. There is a box with no variable pointing at it.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: