raw_pointers


Concept: raw pointers

A raw pointer in C++ is a basic pointer type that holds the memory address of another variable or object. Raw pointers are fundamental to C++ and provide direct access to memory, but they come with significant responsibilities and potential risks. Unlike smart pointers (such as std::unique_ptr or std::shared_ptr), raw pointers do not manage the lifetime of the object they point to, meaning that the programmer is responsible for ensuring proper memory management.

Key Characteristics of Raw Pointers

Example 1: Simple Raw Pointer

#include <iostream>

int main() {
    int x = 42;
    int* ptr = &x;  // 'ptr' is a raw pointer to 'x'

    std::cout << "Value of x: " << x << std::endl;
    std::cout << "Value pointed to by ptr: " << *ptr << std::endl;

    *ptr = 100;  // Modify the value of 'x' through the pointer
    std::cout << "New value of x: " << x << std::endl;

    return 0;
}

Explanation:

Example 2: Dynamic Memory Allocation with Raw Pointers

Raw pointers are commonly used for dynamic memory allocation with new and delete.

#include <iostream>

int main() {
    int* ptr = new int(42);  // Dynamically allocate an integer and initialize it to 42

    std::cout << "Value pointed to by ptr: " << *ptr << std::endl;

    delete ptr;  // Free the dynamically allocated memory

    // ptr now becomes a dangling pointer; it points to a location that has been freed.
    ptr = nullptr;  // It's good practice to set ptr to nullptr after deletion

    return 0;
}

Explanation:

Example 3: Using a Null Pointer

A null pointer is a pointer that does not point to any object or memory location. In modern C++, the nullptr keyword is used to represent a null pointer.

#include <iostream>

int main() {
    int* ptr = nullptr;  // 'ptr' is a null pointer

    if (ptr == nullptr) {
        std::cout << "ptr is null." << std::endl;
    }

    return 0;
}

Explanation:

Example 4: Pointer Arithmetic with Arrays

Raw pointers support pointer arithmetic, which is often used in array manipulation and traversal.

#include <iostream>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int* ptr = arr;  // 'ptr' points to the first element of the array

    for (int i = 0; i < 5; ++i) {
        std::cout << "Value at index " << i << ": " << *(ptr + i) << std::endl;
    }

    return 0;
}

Explanation:

Example 5: Dangling Pointer

A dangling pointer is a pointer that refers to memory that has been freed or is otherwise invalid. Dereferencing a dangling pointer can lead to undefined behavior.

#include <iostream>

int main() {
    int* ptr = new int(42);  // Dynamically allocate memory

    delete ptr;  // Free the memory

    // ptr is now a dangling pointer
    // std::cout << *ptr << std::endl;  // Dereferencing ptr here would be unsafe

    ptr = nullptr;  // Prevent dangling by setting ptr to nullptr

    return 0;
}

Explanation:

Common Pitfalls with Raw Pointers

Summary

Raw pointers are a fundamental feature of C++, offering powerful control over memory and performance, but they come with significant responsibilities and risks. Modern C++ often prefers smart pointers for managing dynamically allocated memory due to their automatic memory management features.

Previous Page | Course Schedule | Course Content