Managing Memory Manually

Avoiding Raw Pointers

Why should we avoid using raw pointers when possible in modern C++ programming?

Abstract art representing computer programming

In modern C++ programming, it's generally recommended to avoid raw pointers for several reasons related to safety, clarity, and maintainability.

Safety

Raw pointers do not manage memory automatically. This puts the onus on us as developers to ensure the lifecycles of our objects are managed correctly. If we don’t accomplish thise, our program can have several problems.

Memory Leaks

If you forget to call delete on a dynamically allocated object, you'll leak memory. For example:

void LeakyFunction() {
  int* p = new int(42); 
  // Missing delete
}

Here, the allocated memory is not freed, leading to a memory leak.

Dangling Pointers

After delete is called, the pointer still holds the address of the freed memory, which can lead to undefined behavior if accessed.

void DanglingFunction() {
  int* p = new int(42);
  delete p;
  *p = 10; 
}

Clarity and Maintenance

Raw pointers can make code harder to understand and maintain:

  • Manual Management: You must manually manage the allocation and deallocation, increasing the likelihood of bugs.
  • Ownership Semantics: It's not always clear who owns the memory or who is responsible for deleting it, making it difficult to manage lifetimes and ownership.

Smart Pointers

Modern C++ provides smart pointers like std::unique_ptr and std::shared_ptr that handle memory management automatically.

std::unique_ptr

This manages a resource with exclusive ownership, ensuring the resource is deleted when the unique_ptr goes out of scope.

void SafeFunction() {
  std::unique_ptr<int> p{
    std::make_unique<int>(42)};
}

Here, the memory is automatically freed when p goes out of scope.

std::shared_ptr

This manages a resource with shared ownership, deleting the resource only when the last shared_ptr to it is destroyed.

void SharedFunction() {
  std::shared_ptr<int> p1{
    std::make_shared<int>(42)};

  // Both share ownership
  std::shared_ptr<int> p2{p1};
}

Using smart pointers helps avoid common pitfalls with raw pointers and makes your code safer and easier to manage.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved