A dangling pointer is a pointer that points to invalid data or to data which is not valid anymore, for example:

Class *object = new Class();
Class *object2 = object;

delete object;
object = nullptr;
// now object2 points to something which is not valid anymore

This can occur even in stack allocated objects:

Object *method() {
  Object object;
  return &object;
}

Object *object2 = method();
// object2 points to an object which has been removed from stack after exiting the function

The pointer returned by c_str may become invalid if the string is modified afterwards or destroyed. In your example you don't seem to modify it, but since it's not clear what you are going to do with const char *name it's impossible to know it your code is inherently safe or not.

For example, if you store the pointer somewhere and then the corresponding string is destroyed, the pointer becomes invalid. If you use const char *name just in the scope of new_foo (for example, for printing purposes) then the pointer will remain valid.

Answer from Jack on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › dangling-pointers-in-cpp
Dangling Pointers in C++ - GeeksforGeeks
July 20, 2026 - Can cause unpredictable program behavior if accessed after the referenced memory becomes invalid. ... Example: The following program shows a dangling pointer created after deleting dynamically allocated memory.
Top answer
1 of 8
98

A dangling pointer is a pointer that points to invalid data or to data which is not valid anymore, for example:

Class *object = new Class();
Class *object2 = object;

delete object;
object = nullptr;
// now object2 points to something which is not valid anymore

This can occur even in stack allocated objects:

Object *method() {
  Object object;
  return &object;
}

Object *object2 = method();
// object2 points to an object which has been removed from stack after exiting the function

The pointer returned by c_str may become invalid if the string is modified afterwards or destroyed. In your example you don't seem to modify it, but since it's not clear what you are going to do with const char *name it's impossible to know it your code is inherently safe or not.

For example, if you store the pointer somewhere and then the corresponding string is destroyed, the pointer becomes invalid. If you use const char *name just in the scope of new_foo (for example, for printing purposes) then the pointer will remain valid.

2 of 8
18

Taken from here. Although, even if this is for C, it is the same for C++.

Dangling Pointer

When a pointer is pointing at the memory address of a variable but after some time that variable is deleted from that memory location while the pointer is still pointing to it, then such a pointer is known as a dangling pointer and this problem is known as the dangling pointer problem.

Initially

Later

Example

#include<stdio.h>

int *call();
int main() {

  int *ptr;
  ptr = call();

  fflush(stdin);
  printf("%d", *ptr);
  return 0;
}

int * call() {
  int x=25;
  ++x;

  return &x;
}

Its output will be garbage because the variable x is a local variable. Its scope and lifetime are within the function call hence after returning the address of x variable x becomes dead and the pointer is still pointing to that location.

Discussions

[C++] Wikipedia dangling pointer example
I'll try to explain a bit more visually and concrete. First let me change the code just a tiny bit. int* a = new int; int* b = a; delete b; /* a and b are now dangling pointers */ *a = 4; /*Memory error*/ This is to show you what the star actually means. It's essentially a different variable type. A pointertype of the regular type. So a variable of the type int* points to a memory location that contains an int. Now: In the first line of code you allocate memory for an int with the code 'new int'. Let's call the adress of this memory location XXX. Then you point the pointer 'a' to this location 'int* a ='. a now points to location XXX. In the second line of code you tell the pointer b to point to the same location as a. a and b now both point to location XXX. In the third line of code you delete the memory b is pointing to. The location XXX is empty and no longer contains an int and can be reused, possibly by processes that you do not control. So you shouldn't overwrite it! This is why a and b are now dangling pointers, they no longer point to accessible memory. In the fifth line you try to overwrite the location XXX, which you had already freeed. This causes an error as you do not have permission to do this. The star operator here is different from the star used to make a pointer. The star here means you dereference the pointer, which essentially means, you get the variable it points to (which usually does not have a name). You get the content of adress XXX. Here's an extra example on the use of the astrix. int anInt = 5; /*the value of anInt is 5, this is stored at a memory location*/ int* aPointer; /*the pointer does not point to anywhere*/ *aPointer = 10; /* the pointer points to a memory location that contains 10*/ int* anOtherPointer = &anInt; /*the &-operator returns the location of a variable, anOtherPointer now points to anInt*/ aPointer = anOtherPointer /*aPointer now also points to anInt*/ I hope this clears things up a bit. EDIT: What work__account said. More on reddit.com
🌐 r/learnprogramming
17
2
January 25, 2013
Dangling pointers in C - Stack Overflow
When a pointer is allocated memory using malloc, pointer (say x)will now point to memory address. Later I free this(x) memory pointer,but pointer is still pointing to it's old memory. This would now create dangling pointer. More on stackoverflow.com
🌐 stackoverflow.com
unique_ptr and dangling references
Here's a shorter demonstration: template Copyable& foot_gun(const Copyable& src) { return *(std::make_unique(src).get()); } It's the difference between syntax and semantics. That's what static analysis tools try to help us figure out. part of me was expecting the reference to have been magically nulled somehow. It might! If the referenced object's destructor cleans everything up to some ground state, it's entirely possible that calls to the object's methods will all return some sensible nothing-value. It's also entirely possible that the compiler will decide that there's no reason to do the cleanup because those values are legally inaccessible after the destructor exits--so the calls will work as if the object weren't destroyed (until something stomps on that heap space). It's possible that the behavior on the same compiler will differ depending on whether you're running a "debug" build or a "release" build (because of different compiler flags). You can waste days chasing ghosts! Static analysis tools and -fsanitize are your friends. More on reddit.com
🌐 r/cpp_questions
10
1
August 18, 2021
Is this an example of a dangling pointer?
It tried to read a field from a pointer to a struct but the pointer was null. Not a dangling pointer seeing as it probably wasn't deallocated (it would most likely be a different random address) but rather a null pointer. More on reddit.com
🌐 r/rust
3
0
April 15, 2025

A dangling pointer is a pointer that points to invalid data or to data which is not valid anymore, for example:

Class *object = new Class();
Class *object2 = object;

delete object;
object = nullptr;
// now object2 points to something which is not valid anymore

This can occur even in stack allocated objects:

Object *method() {
  Object object;
  return &object;
}

Object *object2 = method();
// object2 points to an object which has been removed from stack after exiting the function

The pointer returned by c_str may become invalid if the string is modified afterwards or destroyed. In your example you don't seem to modify it, but since it's not clear what you are going to do with const char *name it's impossible to know it your code is inherently safe or not.

For example, if you store the pointer somewhere and then the corresponding string is destroyed, the pointer becomes invalid. If you use const char *name just in the scope of new_foo (for example, for printing purposes) then the pointer will remain valid.

Answer from Jack on Stack Overflow
🌐
Wikipedia
en.wikipedia.org › wiki › Dangling_pointer
Dangling pointer - Wikipedia
August 8, 2026 - Another frequent source of dangling pointers is a jumbled combination of malloc() and free() library calls: a pointer becomes dangling when the block of memory it points to is freed. As with the previous example one way to avoid this is to make sure to reset the pointer to null after freeing ...
🌐
Medium
huutamnguyen.medium.com › dangling-pointer-and-memory-leak-in-c-when-using-pointer-random-programming-problems-part-2-1d30c66d67c4
Dangling Pointer and Memory Leak in C++ When Using Pointer (Random Programming Problems Part 2) | by Tam Nguyen | Medium
September 7, 2021 - In this example below, the memory is reallocated using the pointer ptr . What happens is that the first allocation is lost irretrievably, and so are the 30 bytes that it pointed to. Now they’re impossible to free, and you have a memory leak. A dangling pointer points to memory that has already been freed.
🌐
Board Infinity
boardinfinity.com › blog › dangling-pointer-in-c
Dangling Pointer in C: Causes, Types, Fixes & Examples
June 16, 2026 - A familiar Indian-context example is a UPI transaction object allocated while processing a payment. If the transaction record is freed after sending a response, but a later logging function still reads txn->amount, the pointer is dangling.
🌐
Reddit
reddit.com › r/learnprogramming › [c++] wikipedia dangling pointer example
r/learnprogramming on Reddit: [C++] Wikipedia dangling pointer example
January 25, 2013 -
int *a = new int;
int *b = a;
delete b;
/* a and b are now dangling pointers */
*a = 4; /* Memory error: we may be overwriting another pointer's data */

Can someone explain this a bit further? I still don't quite understand what a dangling pointer is or how this is an example of it. What does new do, in this case, to the value of a?

Here's the Wiki article: http://en.wikipedia.org/wiki/Memory_safety#Dangling_pointer

Thanks!

Top answer
1 of 4
3
I'll try to explain a bit more visually and concrete. First let me change the code just a tiny bit. int* a = new int; int* b = a; delete b; /* a and b are now dangling pointers */ *a = 4; /*Memory error*/ This is to show you what the star actually means. It's essentially a different variable type. A pointertype of the regular type. So a variable of the type int* points to a memory location that contains an int. Now: In the first line of code you allocate memory for an int with the code 'new int'. Let's call the adress of this memory location XXX. Then you point the pointer 'a' to this location 'int* a ='. a now points to location XXX. In the second line of code you tell the pointer b to point to the same location as a. a and b now both point to location XXX. In the third line of code you delete the memory b is pointing to. The location XXX is empty and no longer contains an int and can be reused, possibly by processes that you do not control. So you shouldn't overwrite it! This is why a and b are now dangling pointers, they no longer point to accessible memory. In the fifth line you try to overwrite the location XXX, which you had already freeed. This causes an error as you do not have permission to do this. The star operator here is different from the star used to make a pointer. The star here means you dereference the pointer, which essentially means, you get the variable it points to (which usually does not have a name). You get the content of adress XXX. Here's an extra example on the use of the astrix. int anInt = 5; /*the value of anInt is 5, this is stored at a memory location*/ int* aPointer; /*the pointer does not point to anywhere*/ *aPointer = 10; /* the pointer points to a memory location that contains 10*/ int* anOtherPointer = &anInt; /*the &-operator returns the location of a variable, anOtherPointer now points to anInt*/ aPointer = anOtherPointer /*aPointer now also points to anInt*/ I hope this clears things up a bit. EDIT: What work__account said.
2 of 4
2
Enough memory to store an int is allocated to the program on line 1, and a is set to point to that block of memory. Then, that block of memory is deallocated on line 3. As soon as the delete statement completes, that memory address is no longer part of the program's allocated memory, and reading or writing it can produce surprising results. For example, another thread in the program might just have called new int and happened to get that same block of memory - line 5 will now cause action at a distance.
Find elsewhere
🌐
Black Hat
blackhat.com › presentations › bh-usa-07 › Afek › Whitepaper › bh-usa-07-afek-WP.pdf pdf
DANGLING POINTER SMASHING THE POINTER FOR FUN AND PROFIT JONATHAN AFEK
This whitepaper will present a complete instruction manual to researching and exploiting Dangling · Pointer vulnerabilities using the real case IIS vulnerability.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › dangling-pointer-in-programming
Dangling Pointer in programming - GeeksforGeeks
May 14, 2024 - Dangling Pointer in programming refers to a pointer that doesn’t point to a valid memory location. This usually happens when an object is deleted or deallocated, without modifying the value of the pointer, so it still points to the memory location of the deallocated memory. ... The below example demonstrates a simple program that creates a dangling pointer in C.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › difference-between-dangling-pointer-and-void-pointer
Difference between Dangling pointer and Void pointer - GeeksforGeeks
July 23, 2025 - Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.
🌐
Tutorial and Example
tutorialandexample.com › dangling-pointers-in-c
Dangling pointers in C - TAE
June 1, 2021 - When the memory in the program is deallocated, then the pointer points to a freed or a deleted space which eventually leads to dangling pointer ... The dangling pointer gives out bugs and errors in the C programming language and it becomes difficult for a programmer to find one.
🌐
UNKRIS
p2k.unkris.ac.id › IT › 3065-2962 › dangling-pointer_18199_p2k-unkris.html
Dangling pointer - Komputer - 3065 - p2k.unkris.ac.id
Dangling pointer Komputer 3065 p2k.unkris.ac.id Dangling pointer Dangling Pointer Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations.
🌐
StudyMite
studymite.com › blog › dangling-pointer-in-c
Dangling pointer in C | StudyMite
June 7, 2026 - The pointer ptr is still pointing to the same memory location in outer block. However, int type variable num is not available at that memory location anymore. Hence, ptr becomes a dangling pointer in the outer block.
🌐
Aticleworld
aticleworld.com › home › dangling, void , null and wild pointer in c
Dangling, Void , Null and Wild Pointer in C - Aticleworld
May 25, 2021 - Let’s see an example program, ... would no longer work correctly. So in the below code piData is a dangling pointer that is pointing to a memory that is not available....
🌐
Wordpress
coherence0815.wordpress.com › 2014 › 08 › 14 › dangling-pointer
Dangling pointer | 海明威
December 6, 2022 - #include <iostream> using namespace std; void main() { int* ptr = NULL; if(1) { int n = 20; ptr = &n; cout << "Original value:" << endl; cout << "*ptr = " << *ptr << endl; } //n out of scope //ptr is now a dangling pointer cout << "After out of scope:" << endl; cout << "*ptr = " << *ptr << endl; } ========== Example 3 ========= 指標所指向function回傳local variable的address。其實Example 3也是Example 2的一種;指標所指的function內的local variable out of scope。
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dangling-void-null-wild-pointers
Dangling, Void , Null and Wild Pointers in C - GeeksforGeeks
July 11, 2026 - When the local variable is not static and the function returns a pointer to that local variable. The pointer pointing to the local variable becomes dangling pointer.
🌐
Medium
medium.com › @sofiasondh › what-is-a-dangling-pointer-how-can-it-be-avoided-e72321e1fdf3
What is a Dangling Pointer? How Can It Be Avoided? | by Sofia Sondh | Medium
December 21, 2024 - A dangling pointer occurs when a pointer continues to reference memory that has already been deallocated or is no longer valid. This can cause unpredictable behavior, crashes, and security risks. In this article, we’ll explain what dangling pointers are, provide examples, and discuss effective ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_dangling_pointers.htm
Dangling Pointers in C
The same reason applies when a variable declared in an inner block is accessed outside it. In the following example, we have a variable inside a block and its address is stored in a pointer variable. However, outside the block, the pointer becomes a dangling pointer as its target is out of bounds.
🌐
TechTarget
techtarget.com › searchsecurity › tip › How-to-avoid-dangling-pointers-Tiny-programming-errors-leave-serious-security-vulnerabilities
What dangling pointers are and how to avoid them | TechTarget
July 18, 2024 - Decades ago, dangling pointers were considered quality control problems, not security issues. In 2005, for example, Microsoft was alerted to a dangling pointer in Internet Information Services 5.1, but it remained unpatched for two years.