Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Memory
  2. Pointers

Dangling Pointer

A dangling pointer is a pointer that points to a memory location that has been deallocated or freed. This means the memory it once pointed to no longer exists, and the pointer is left "dangling."

How Dangling Pointers Occur

There are primarily three ways a dangling pointer can arise:

  1. Deallocation of memory:

    • When you use delete or free to deallocate memory, the pointer still holds the address of the freed memory.

    • If you subsequently try to access or modify the memory through this pointer, you'll invoke undefined behavior, leading to crashes or corrupted data.

  2. Local variable going out of scope:

    • When a function returns, local variables within that function go out of scope.

    • If you return the address of a local variable, the pointer becomes dangling once the function ends.

    • Accessing this pointer outside the function's scope is invalid.

  3. Object deletion:

    • When an object is deleted using delete, the pointer to that object becomes dangling.

    • Continuing to use the pointer after deletion results in undefined behavior.

Example

#include <iostream>

int* dangerousFunction() {
    int x = 10;
    return &x; // Returning the address of a local variable
}

int main() {
    int* ptr = dangerousFunction();
    std::cout << *ptr << std::endl; // Undefined behavior!
    return 0;
}

Use code with caution.

In this example, ptr becomes a dangling pointer because it points to the address of x, which is a local variable. Once dangerousFunction returns, x goes out of scope, and accessing *ptr leads to undefined behavior.

Consequences of Dangling Pointers

  • Undefined behavior: The program's behavior is unpredictable, and it might crash or produce incorrect results.

  • Memory corruption: Trying to access or modify memory through a dangling pointer can corrupt other data.

  • Security vulnerabilities: Exploiting dangling pointers can lead to security vulnerabilities.

Preventing Dangling Pointers

  • Nulling the pointer: After deallocating memory, set the pointer to nullptr.

  • Avoiding returning addresses of local variables: Use references or output parameters instead.

  • Smart pointers: C++11 introduced smart pointers (like unique_ptr, shared_ptr) that automatically manage memory, reducing the risk of dangling pointers.

  • Careful memory management: Be mindful of when and how memory is allocated and deallocated.

PreviousPointersNext'this' Pointer

Last updated 9 months ago

Try it .

🎯
here