Here are my thoughts of C++ Memeory management, please feel free to comment.
Memory can be allocated in stack or heap.
Rule 1:
If two nested stacks need to share data, use RAII allocating memory in stack, like below:
func1() {
Resource res; // res will be destructed once func1 returns
func2( &res );
}
Rule 2:
If two parallel stacks need to share data (not class member fields), memory must be allocated in heap, use Smart Points or GC. For example:
func() {
shared_ptr<Resource> res = func1(); // returned a shared ptr to a memory allocated in func1
func2( res );
}
Am I correct?