Questions on Pointers to Objects

In the video about pointers to objects at 0:45 he states that you use these if you want objects to remain after the end of the function.
Then goes on to show the “old” way to create objects with a pointer.
Then shows the “new” way. About halfway through the video he mentions that with the new way make_unique<> the object will be released at the “end of the main() function.”

There are a few points of confusion here.
Does the make_unique version get deleted at the end of main() all the time, or is it destroyed at the end of the function it was created in?
If it’s deleted at the end of the function it was created in, then how is it any more useful than having it on the stack?
Will the “old” way work as described and keep it longer than the current function?
Is it true that when the main() ends all memory, even undestroyed heap memory is returned back to the OS?

Thanks!

First of all, you should understand that static memory allocation and dynamic memory allocation are different.

In static memory allocation, memory is allocated for all identifiers declared without using new operator before execution of program (after compilation). Here, data is stored using a stack, which uses a contiguous block of memory.

Static memory data is deleted when objects go out of scope.

In dynamic memory allocation, memory is allocated in heap at run time for all identifiers declared using new operator. Data here is stored using a heap, which use trees or linking between memory blocks.

Pointers are specifically designed to store (or point to) memory locations. This makes them useful for storing data on heap, passing huge data over functions or iterating data structures.

Memory in heap is not automatically deleted.

Memory created in heap should be deleted manually, which is why the “old pointer’s way” was generalized by smart pointers using templates.

Smart pointers contain two types of pointers:

  1. Unique pointers std::unique_ptr
  2. Shared pointers std::shared_ptr

Unique pointers delete object they point to when they go out of scope.

What we created in SmartPointer class was a unique pointer.

Shared pointers use reference count to count current number of references to an object. If the count is not zero, then data is not deleted even when pointer goes out of scope. When the count is zero, data is deleted.

Now.

make_unique member function creates a unique pointer. There is no reference count in unique pointers. They are deleted when they are out of scope from where they were declared, a function or main (which is also a function).

how is it any more useful than having it on the stack

You probably mean to say old pointer’s way.

Memory on heap allows you to control data during execution of your program, that is, it is more flexible. You can change, update or delete it from main memory whenever you want to. Of course we are talking about complex data here, as static memory is also smaller in comparison to heap memory to support operation on these data. Also, threads share heap data. So it allows better multithreading operations.

Static memory creates data blocks based on data types. They are removed only after complete execution of your program.

To make it work

longer than the current function,

you will need to implement your own object reference counter which is already done in shared pointer class.

It depends on the OS being used. When heap memory is not deleted by the user, it creates memory leak. Most modern OS clean up memory space used by the program after execution. But it is not done in all OS.

It is advised to clean up heap memory in a program and not doing it is a bad practice. It creates integrating operations into bigger applications or when switching deployment platforms an issue.

I hope that answered all your questions.

I replied to this post late, as I have recently started the advanced part of the series (and started using the forum).

This forum has less activity. Maybe it is dependent on the topic?