http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

in this example the keyword "new" can be replaced with "malloc" or others ..

I like to think of the stack as privately assigned/managed memory per function , and the heap as global managed/assigned memory ..

if you create something within a function scope that
does not get stored globally (it cannot be accessed outside of this function scope) then it should be taken care of when the function scope runs out ,the local (stack) would be taken care of .

if you can globally access memory created within another function its not on the stack, you can take
care of removing it , however there is no harm in destroying a local created object within the end scope of that function if you feel like it I think.

However using malloc seems to be mentioned by the manual as automaticly taken care of upon exit , but this also does not mean you should ignore such objects unless you need them to exist throughout runtime ..

you should create a function to release memory you
created globally ,so that you may call it whenever you
need to ,sometimes you need to destroy a bunch
of created objects before the application exit e.g when
going to a new level you most likely need to get rid
of some stored data.

I am coming off as confusing most likely but I tried.

what becomes confusing here is the word "create"
because , depending on how the created object gets created makes a difference ,if the called function creates the object on the heap you take care of it , if it is on the stack it wil be taken care of as soon as the function that called the "create function" runs out ,I mention this because not all functions that create objects are written the same eg.

you may find a function that is written to create on the heap but called localy within another function ..

Last edited by Wjbender; 08/25/14 09:59.

Compulsive compiler