Ashish Kumar

Apr 24, 2025 • 1 min read

Smart Pointers

Smart pointers in rust and c++

So let's say you wanted to use pointers , in a language that lacks garbage collector , for eg rust or C++. First let's understand what garbage collectors do. So you make some space in the memory using calloc or malloc and it is there and you forget about it (which you do) cuz of the big , really big codebase. Now everything you restart the program , it takes some space in your memory. But the garbage collector comes in , takes that amount of memory and frees it automatically.

So that garbage created , is now cleared by garbage collector in languages like java and python. But what bout languages that lack garbage collector ? That is why languages like Rust and C++ introduced smart pointers for this job.

But before that , what are pointers ? the above image explains exactly that , for exact definition ask my best friend ("google").

So smart pointers basically do what is help you solve this problem. It basically is a wrapper class around raw pointers. And it ensures that memory is deallocated when not needed. This prevents memory leaks (memory gets created again and again without being deallocated).

And it helps clear out the clutter when no more required , it automatically clears out the memory. But it has some cons , like slight performance issues and it takes more memory than raw pointers.

Rust Docs :=> https://doc.rust-lang.org/book/ch15-00-smart-pointers.html

C++ Docs :=> https://learn.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=msvc-170

Join Ashish on Peerlist!

Join amazing folks like Ashish and thousands of other people in tech.

Create Profile

Join with Ashish’s personal invite link.

2

12

0