Memory Allocation
Every variable we have seen so far is a local variable. And local variables go out of scope when the function ends. But sometimes, this is not what we want. That’s where the free store comes in. The free store is memory for longer-lived variables.
Manual Memory Management
To avoid wastage of memory, you can dynamically allocate any memory required during runtime using the new
and delete
operators in C++.
int main() {// memory allocationptr = new int[num];...// memory is releaseddelete ptr;}
The new
operator:
ptr = new int[num];
The delete
operator:
delete ptr;
If you create something with new
, at some point you must delete
it. When something is not deleted, it will cause a memory leak.
The Rule of Three
The rule of three is a rule of thumb in C++ that claims that if a class defines one of the following special member functions, it should define all three:
- Destructor
- Copy constructor
- Copy assignment operator
The Rule of Five
With C++11, a new rule emerged: the rule of five. This adds two more special functions to the rule of three list:
- Destructor
- Copy constructor
- Copy assignment operator
- Move constructor
- Move assignment operator
As you can see, manual memory management is difficult and old-fashioned. Instead of using new
and delete
, there’s something in the standard library that will make your life a lot easier.
Learn C: Pointers and Memory
Learn how to use C pointers and proper memory management to make your programs more efficient.Try it for freeStandard Library Smart Pointers
A smart pointer is an abstract data type that was popularized by C++ during the early 1990’s. It simulates a pointer while providing additional features, such as automatic memory management.
In the standard library, we have the following:
unique_ptr
: a smart pointer that owns and manages another object through and disposes of that object when theunique_ptr
goes out of scope.shared_ptr
: a smart pointer that retains shared ownership of an object through a pointer. Severalshared_ptr
objects may own the same object.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Binary Application Attacks
Learn about attacks that target binary applications, often via memory manipulation. - Article
Implementing Memory in LLM Applications Using LangChain
This article discusses how to implement memory in LLM applications using the LangChain framework in Python. - Article
How to Concatenate List in Python
Learn how to concatenate lists in Python using `+`, `*`, `for` loop, list comprehension, `extend()`, and `itertools.chain()`. Compare the methods for their efficiency.
Learn more on Codecademy
- Free course
Learn C: Pointers and Memory
Learn how to use C pointers and proper memory management to make your programs more efficient.Beginner Friendly1 hour - Free course
Learn C++: References and Pointers
References and pointers are some of the most powerful features in C++; they allow programmers to directly manipulate memory.Beginner Friendly1 hour - Free course
Operating Systems: Process Synchronization and Memory Management
Learn about Operating Systems and how to synchronize processes and handle memory management.Beginner Friendly1 hour