Shortening the lifecycle of heap objects

Shortening the lifecycle of heap objects lets the allocator reclaim memory faster, and allows it to be quickly reused for new heap objects, which reduces the amount of heap memory required.

To optimize the heap in this manner, always try to free objects in the same function as they are allocated, unless it is an allocation function. (An allocation function returns or stores a heap object to be used after the function exits.) A good pattern of local memory allocation looks like this:
p=(type *)malloc(sizeof(type));
do_something(p);
free(p);
p=NULL;
do_something_else();

After the pointer is used, it is freed, then nullified. The memory can then be used by other processes. In addition, try to avoid creating aliases for heap variables because it usually makes code less readable, more error prone, and difficult to analyze.