As soon as the scope of the function ends i.e after the closing brace } of function, memory allocated(on stack) for all the local variables will be released automatically by compiler. So, returning pointer to some memory which is no longer valid invokes undefined behavior.

Whereas dynamic memory allocation is on heap/free store and memory allocated from it won't be released by the compiler. If you have allocated something dynamically then you have to deallocate it yourself when memory is no longer required otherwise there will be a memory leak.
As far as statics are concerned, memory for them is allocated from a special static/global area as soon as the program starts. This memory is released as soon as the programs gets over.

So, returning pointers on both the latter cases makes sense.

Last edited by Aztec; 7Nov2006 at 22:50..