Memory leaks can be a significant issue when working with pointers in C++. They occur when dynamically allocated memory is not properly deallocated, leading to wasted resources and potential program crashes. Here are some best practices to avoid memory leaks:
The most effective way to prevent memory leaks is to use smart pointers from the <memory>
header. These automatically manage memory deallocation:
#include <iostream>
#include <memory>
class Character {
public:
Character(std::string name)
: mName{std::move(name)} {}
std::string mName;
};
int main() {
// Using unique_ptr for exclusive ownership
std::unique_ptr<Character> player{
std::make_unique<Character>("Anna")};
std::cout << "Player name: "
<< player->mName << "\n";
// Using shared_ptr for shared ownership
std::shared_ptr<Character> enemy{
std::make_shared<Character>("Goblin")};
std::cout << "Enemy name: "
<< enemy->mName << "\n";
}
std::unique_ptr
and std::shared_ptr
automatically delete the object they own when they go out of scope, preventing memory leaks.
Follow the RAII principle: wrap resource management in classes whose constructors acquire the resource and destructors release it.
new
and delete
Minimize the use of raw new
and delete
. If you must use them, ensure every new
is matched with exactly one delete
:
#include <iostream>
#include <string>
class Weapon {
public:
Weapon(std::string name)
: mName{std::move(name)} {}
~Weapon() {
std::cout << mName << " destroyed\n";
}
std::string mName;
};
void badPractice() {
Weapon* sword = new Weapon("Excalibur");
// ... some code ...
// Oops! We forgot to delete sword. Memory leak!
}
void goodPractice() {
Weapon* sword = new Weapon("Excalibur");
// ... some code ...
delete sword; // Remember to delete!
}
int main() {
badPractice();
goodPractice();
}
Excalibur destroyed
std::vector
Instead of Raw ArraysPrefer std::vector
over raw arrays when you need a dynamic collection:
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{10, 20, 30};
scores.push_back(40);
for (int score : scores) {
std::cout << score << " ";
}
}
Circular references can cause memory leaks with std::shared_ptr
. Use std::weak_ptr
to break cycles:
#include <memory>
class Character;
class Weapon {
public:
// Use weak_ptr to avoid circular reference
std::weak_ptr<Character> mOwner;
};
class Character {
public:
std::shared_ptr<Weapon> mWeapon;
};
int main() {
auto character = std::make_shared<Character>();
character->mWeapon = std::make_shared<Weapon>();
character->mWeapon->mOwner = character;
}
By following these practices, you can significantly reduce the risk of memory leaks in your C++Â programs.
Answers to questions are automatically generated and may not have been reviewed.
This lesson provides a thorough introduction to pointers in C++, covering their definition, usage, and the distinction between pointers and references
Comprehensive course covering advanced concepts, and how to use them on large-scale projects.
View Course