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
Raw String Literal in C++ A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \" ' of C++ are not processed. Hence, a raw string literal that starts with R"( and ends in )". The syntax for R
2 min read
Pointers vs References in C++ Prerequisite: Pointers, References C and C++ support pointers, which is different from most other programming languages such as Java, Python, Ruby, Perl and PHP as they only support references. But interestingly, C++, along with pointers, also supports references. On the surface, both references and
5 min read
Smart Pointers in C++ In C++, pointers are the variables that stores the memory addresses. They are extensively used in dynamic memory location to store the address of allocated memory. But they bring a lot of issues.Problems with Normal PointersMemory Leaks: This occurs when memory is repeatedly allocated by a program b
4 min read
C++ Vector of Pointers Prerequisites Pointers in C++Vectors in C++ Vector of pointers are vectors that can hold multiple pointers. Each pointer within a vector of pointers points to an address storing a value. We can use the vector of pointers to manage values that are not stored in continuous memory. How to Create Vector
6 min read
Features and Use of Pointers in C/C++ Pointers store the address of variables or a memory location. Syntax: datatype *var_name; Example: pointer "ptr" holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through "ptr" int *ptr; Features of Pointers: Pointers save memory
2 min read