Garbage Collection In C++

Garbage Collection In C++

·

3 min read

C++ is a very popular general-purpose programming language that's generally used in high-performance applications, operating systems, embedded systems and many more low-level areas. It's a very fast language, it's well known for its performance and reliability but its also well known for its huge learning curve and lack of garbage collection (automatic memory allocation and deallocation). C++ doesn't have garbage collection by default but with the introduction of smart pointers in C++11, it makes it easier to manage memory without worrying about memory leaks.

What Is Garbage Collection?

Garbage Collection is a feature that is baked into most modern-day languages. It helps in automatically deallocating and allocating memory without the intervention of the programmer. It prevents memory leaks from happening.

Unfortunately, since languages like C and C++ are very old, they do not have this feature so you have to manually allocate and deallocate memory but smart pointers in C++ solve this issue.

What Are Smart Pointers?

A pointer in C++ is a variable that points to the memory address of a value. They're essential to use data structures in C++. They also allow programs to emulate call-by-reference.

A pointer is denoted with the * symbol.

For example:

    datatype *var_name;   
    int *ptr;   // ptr can point to an address which holds int data

With the use of regular pointers, you have to manually allocate and deallocate memory by using delete keyword to free up memory. This can be a bit tiring when you're dealing with large scale applications and can be time consuming and you may also be prone to causing memory leaks.

To solve this issue, Smart pointers were introduced in C++11.

Pointer vs Smart Pointer

In a regular program using regular pointers, we have to use the delete keyword to deallocate the memory.

For Example:

void someFunc(unsigned int size)
{
    Box * boxArr = new Box[size]; // initating a pointer
       delete[] boxArr;//deleting the memory

As you can see, you have to manually delete the memory. This can be tedious and confusing when you're dealing with lengthy code.

Now let's see how its done with smart pointers

void someFunc(unsigned int size)
{
    unique_ptr<Box> box(new Box); // allocates one Box object
    unique_ptr<Box[]> boxArr(new Box[size]); // allocates an array    
}

As you can see, we didn't have to use the delete keyword this time. We used a smart pointer here which automatically handles the memory management. Once the object is out of scope, it gets deleted automatically. This will not only save time but also make it harder to write unsafe code.

Types Of Smart Pointers

There are 3 types of smart pointers:

  • unique_ptr

  • shared_ptr

  • weak_ptr

unique_ptr

This is the go-to smart pointer if you're not sure what kind of pointer to use. It holds a unique pointer and the memory cannot be shared with any other pointers. You cannot create another copy of this pointer.

Usage:

//unique_ptr usage
std::unique_ptr<int> ptr(newint(24));

shared_ptr

This is the second type of smart pointer in C++. It allows you to make a copy of a pointer. It holds the memory until the pointer holding the memory goes out of scope. It maintains a reference counter.

Usage:

//shared_ptr usage
std::shared_ptr<int> p1(new(24));

weak_ptr

This is the third type of smart pointer in C++. It's very similar to shared_ptr, only difference is that the reference counter won't increase.

Usage:

//weak_ptr usage
std::weak_ptr<Foo> p1(new Foo);

.

Limitations of Smart Pointers

Everything has its weakness so do smart pointers.

  • They don't help against loops in graph like structures.

  • Strong smart pointers in loops will cause memory leaks

  • Performance limitations

  • Cannot be used in embedded systems due to STL limitations