Once foo() returns, the automatic variables a (an int) and p (a pointer) are freed, but the heap variable created on line four still exists.

Remember, malloc() creates stuff that has no name. We only have its address (stored in the variable p). By dereferencing p we can access that unnamed value: printf( "%d\n", *p ); Every time you create something on the heap, you must also explicitly free() it.