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.
delete
or `delete[].#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;
}
int* ptr = &x;
: ptr
is a raw pointer that holds the address of x
.*ptr
): Dereferencing the pointer ptr gives access to the value stored at the memory address it points to, which is x
.x
can be modified by dereferencing ptr
.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;
}
int* ptr = new int(42);
dynamically allocates memory for an integer and initializes it to 42
. ptr
holds the address of this memory.delete ptr;
deallocates the memory pointed to by ptr
. Failing to deallocate this memory can result in a memory leak.ptr
to nullptr
is a good practice to avoid accidentally accessing a dangling 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;
}
nullptr
: ptr
is initialized to nullptr
, indicating that it does not point to any valid memory.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;
}
int* ptr = arr
; points to the first element of the array arr
.ptr + i
moves the pointer to the i-th element in the array. Dereferencing this pointer with *(ptr + i)
gives the value at that index.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;
}
delete ptr;
, the pointer ptr still holds the address of the freed memory, making it a dangling pointer.nullptr
after deletion ensures that it no longer refers to invalid memory.nullptr
) represents a pointer that does not point to any valid memory.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