Avoiding Dangling Pointers and Memory Leaks

Tutorial 4 of 5

Introduction

In this tutorial, we will cover two significant issues in C++ programming: dangling pointers and memory leaks. Both issues are related to dynamic memory management, a vital but tricky part of C++.

You will learn:
- What dangling pointers and memory leaks are
- How to avoid them in your C++ code

Prerequisites:
- Basic knowledge of C++ syntax
- Familiarity with pointers

Step-by-Step Guide

Dangling Pointers

A dangling pointer is a pointer that doesn’t point to a valid memory location. This usually happens when memory is deallocated before all pointers to it have been properly reassigned.

Avoiding Dangling Pointers:
- Always set your pointers to NULL after you delete them.
- Be careful when returning pointers from functions. Make sure the memory they point to is still valid.

Memory Leaks

A memory leak occurs when a program allocates memory on the heap but forgets to deallocate it. This can lead to the program using up more and more memory, eventually causing it to slow down or crash.

Avoiding Memory Leaks:
- Always deallocate memory with delete (or delete[] for arrays) after you're done using it.
- Use smart pointers (std::unique_ptr, std::shared_ptr) that automatically manage memory for you.

Code Examples

Dangling Pointer Example

int* ptr = new int(5); // Allocate memory for an integer, and assign its address to ptr
delete ptr; // Deallocate memory
// ptr is now a dangling pointer, because it's pointing to memory that has been deallocated.

To avoid creating a dangling pointer, set ptr to NULL after deleting it.

int* ptr = new int(5); 
delete ptr; 
ptr = NULL; // ptr is no longer dangling

Memory Leak Example

for(int i = 0; i < 1000000; i++) {
    int* ptr = new int(i); // Allocate memory for an integer
    // No delete here: memory is leaked on every iteration
}

To avoid the memory leak, delete the memory after you're done using it.

for(int i = 0; i < 1000000; i++) {
    int* ptr = new int(i); 
    delete ptr; // No memory leak
}

Summary

In this tutorial, we learned what dangling pointers and memory leaks are, and how to avoid them in C++. Always remember to set pointers to NULL after delete, and to always delete memory when you're done using it. For further learning, you can look into smart pointers in C++, which automatically handle memory management for you.

Practice Exercises

  1. Write a function that creates a memory leak. Then, modify the function to prevent the memory leak.
  2. Create a dangling pointer, then modify your code to avoid creating the dangling pointer.

Solutions:

  1. Memory Leak
void createMemoryLeak() {
    int* ptr = new int(5); // Allocate memory for an integer
    // Memory is leaked because we didn't delete ptr
}
// Fixed version
void noMemoryLeak() {
    int* ptr = new int(5);
    delete ptr; // No memory leak
}
  1. Dangling Pointer
void createDanglingPointer() {
    int* ptr = new int(5); 
    delete ptr; // ptr is now dangling
    // To avoid the dangling pointer, set ptr to NULL
    ptr = NULL;
}