Risks of Using Raw Pointers in C++
Last Updated : 18 Jun, 2025
In C++, raw pointers are variables that hold memory addresses of other variables or dynamically allocated memory. While raw pointers provide powerful low-level control over memory, they also introduce several risks that can lead to bugs, security vulnerabilities, and crashes.
What is a Raw Pointer?
A raw pointer is a basic pointer type that directly stores the address of a memory location.
Example:
C++ int x = 10; // ptr holds address of x int* ptr = &x;
Common Risks of Using Raw Pointers
Let’s look at the key dangers of raw pointers in C++:
Memory Leaks
Raw pointers require manual deallocation of dynamically allocated memory using delete or delete[]. If you forget to free memory, it leads to memory leaks — memory that is allocated but never released.
C++ #include <iostream> using namespace std; int main() { // Allocate memory for an integer int* p = new int(42); // Use the pointer cout << "Value: " << *p; // Forgot to delete p — memory leak happens // delete p; // This is missing return 0; }
Over time, this leaks memory and can exhaust system resources, especially in long-running programs.
Dangling Pointers
A dangling pointer points to memory that has been deallocated or gone out of scope.
C++ #include <iostream> using namespace std; int main() { int* p = new int(99); // Memory freed delete p; // p still points to the freed memory cout << "Dangling value (undefined): " << *p; return 0; }
OutputDangling value (undefined): 0
Using a dangling pointer may result in crashes, data corruption, or unpredictable behaviour.
Double Deletion
If you delete the same pointer twice, the program’s behaviour is undefined.
C++ #include <iostream> int main() { int* p = new int(7); // First delete — OK delete p; // Second delete — undefined behavior delete p; return 0; }
Output
bort signal from abort(3) (SIGABRT)
*** Error in `./Solution': double free or corruption (fasttop): 0x0000000006382c20 ***
timeout: the monitored command dumped core
/bin/bash: line 1: 31 Aborted
Wild Pointers
A wild pointer is a pointer that is not initialized but is used anyway.
C++ #include <iostream> using namespace std; int main() { // Uninitialized pointer (wild) int* p; // Dangerous: writing to an unknown address *p = 10; cout << "Written to wild pointer"; return 0; }
OutputWritten to wild pointer
Ownership Confusion / Aliasing Example
When multiple raw pointers point to the same dynamically allocated memory, it's unclear who is responsible for deleting it.
C++ #include <iostream> using namespace std; int main() { int* p1 = new int(100); // Both point to same memory int* p2 = p1; // Memory freed delete p1; // p2 still points to freed memory — dangling cout << "p2 value (undefined): " << *p2; // If we delete p2, we attempt to free // same memory again undefined behavior delete p2; return 0; }
Output
Abort signal from abort(3) (SIGABRT)
*** Error in `./Solution': double free or corruption (fasttop): 0x0000000025d57c20 ***
timeout: the monitored command dumped core
/bin/bash: line 1: 32 Aborted
This makes memory management error-prone and invites bugs like double deletion or dangling pointers.
No Bounds Checking
Raw pointers do not provide array bounds checking when used for dynamic arrays.
C++ #include <iostream> using namespace std; int main() { int* arr = new int[3]{1, 2, 3}; // Correct access cout << "arr[0]: " << arr[0] << endl; // Undefined behavior — writing beyond // array size arr[5] = 42; cout << "Out-of-bounds write done"; delete[] arr; return 0; }
Outputarr[0]: 1 Out-of-bounds write done
Overwriting adjacent memory may corrupt data or crash the program.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read