🌐
GeeksforGeeks
geeksforgeeks.org › c++ › dangling-pointers-in-cpp
Dangling Pointers in C++ - GeeksforGeeks
July 20, 2026 - Dereferencing such a pointer results in undefined behavior, which may lead to crashes, incorrect output, or memory corruption. Commonly created after deleting dynamically allocated memory or when an object goes out of scope.
in computer programming: pointer that does not point to a valid object
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. More generally, … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Dangling_pointer
Dangling pointer - Wikipedia
August 8, 2026 - In every case, programmers using ... leads again to the problem. Also, this solution is limited to the scope of a single program or project, and should be properly documented. Among more structured solutions, a popular technique to avoid dangling pointers in C++ is to use smart ...
Discussions

c++ - What is a dangling pointer? - Stack Overflow
A dangling pointer is a (non-NULL) pointer which points to unallocated (already freed) memory area. The above example should be correct given that the string is not modified through new_foo. ... @Miklós Homolya its "const " .. what do you mean modify in new_foo?! 2013-08-01T15:14:22.923Z+00:00 ... That use of "const" is (compiler) front-end feature, which could be overcome by a const_cast. The problem ... More on stackoverflow.com
🌐 stackoverflow.com
Dangling Pointers
When you have a dangling pointer, you've freed the memory (or if it's a pointer to something on the stack the memory it points to is now being used by something else). In this case if you dereference it you might get an error if you free'd it and the page got returned to your OS, or you might get garbage data if something else is using the memory, or everything might just work fine if nothing else has started using that memory. You shouldn't dereference it because it's undefined behaviour and the compiler doesn't have to make any guarantees about what will or will not happen when you do it. This is why you'll get different behaviour depending on lots of factors like the compiler used and optimisation settings. More on reddit.com
🌐 r/C_Programming
22
15
April 25, 2025
[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
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
People also ask

What is a dangling pointer in C?
A dangling pointer in C is a pointer that refers to a memory location that has already been freed or gone out of scope. Using such a pointer can lead to undefined behavior, such as crashes or unexpected results.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › dangling-pointer
Dangling Pointer in C Language (Explained With Examples)
How does a dangling pointer in C occur?
It occurs when a pointer continues to reference memory after it has been freed, gone out of scope, or returned from a function.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › dangling-pointer
Dangling Pointer in C Language (Explained With Examples)
Is a dangling pointer in C same as a null pointer?
No. A null pointer is safely initialized to NULL (points to nothing), while a dangling pointer points to invalid or freed memory.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › dangling-pointer
Dangling Pointer in C Language (Explained With Examples)
🌐
Board Infinity
boardinfinity.com › blog › dangling-pointer-in-c
Dangling Pointer in C: Causes, Types, Fixes & Examples
June 16, 2026 - Safe alternatives are returning by value, using caller-provided storage, or allocating dynamically with a clear responsibility to free. A function returning the address of a non-static local variable is a classic dangling pointer bug.
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.

🌐
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.
🌐
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 methods to prevent them, helping you write safer and more reliable C++ code.
Find elsewhere
🌐
Unstop
unstop.com › home › blog › dangling pointer in c language explained (with code examples)
Dangling Pointer In C Language Explained (With Code Examples)
March 12, 2024 - Constant Pointer to Constant: A ... points to. Example: const int* const ptr; declares a constant pointer to a constant integer. A dangling pointer in C is not the same as a memory leak, but both issues are related to memory management and can cause problems in a ...
🌐
Scaler
scaler.com › home › topics › dangling pointer in c
Dangling Pointer in C - Scaler Topics
June 14, 2022 - We should assign NULL to the ptr pointer as soon as the memory block pointed by the ptr has been deallocated using the free() function to avoid creating the dangling pointer problem in our program.
🌐
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.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › dangling-pointer
Dangling Pointer in C Language (Explained With Examples)
July 27, 2026 - Learn in this tutorial about Dangling Pointer in C with examples. Understand how it is created, the issues it causes, and methods to avoid it in C programs.
🌐
Dot Net Tutorials
dotnettutorials.net › home › dangling pointer in c
Dangling Pointer in C Language with Examples - Dot Net Tutorials
November 17, 2023 - In C Language, the pointer pointing to the local variable becomes dangling when the local variable is not static. In the below example, the variable x is a local variable and goes out of scope once the execution of the fun() function is completed.
🌐
Developer Insider
developerinsider.co › what-is-dangling-pointer-with-cause-and-how-to-avoid-it
What is Dangling Pointer with Cause and How to avoid it?
March 29, 2018 - Dangling pointer bugs frequently become security holes. For example, if the pointer is used to make a virtual function call, a different address (possibly pointing at exploit code) may be called due to the vtable pointer being overwritten.
🌐
Medium
medium.com › @ryan_forrester_ › dangling-pointers-in-c-a-comprehensive-guide-04d55b0feb51
Dangling Pointers in C++: A Comprehensive Guide | by ryan | Medium
September 29, 2024 - In this code, we return the address of a local variable `x`. Once `create_dangling_pointer()` returns, `x` goes out of scope and is destroyed. The pointer `ptr` in `main()` is now dangling, pointing to invalid memory. As shown in the previous example, this is a common mistake, especially for ...
🌐
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.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › dangling pointers in c
Dangling Pointers in C | Learn How Dangling Pointers Works in C?
April 13, 2023 - Then pointer p1 is created to call the fun1(). Then after this, the pointer p will not point to a specific point, it points to the one which is not at all a valid one anymore. Then printf is used to print. But here there will be a warning when the c code runs in the c compiler. Check out the output so that you will know. Here in this example, the normal pointer doesn’t even become into a dangling pointer.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Educative
educative.io › home › courses › data structures preliminaries (refresher of fundamentals in c++) › problems with pointers
Common Problems with Pointers in C++ Programming
In the example discussed above, we addressed the issue of dangling pointers, but unfortunately, it resulted in a new problem: memory leakage. This occurred because when we copied the content of ptr to d, we allocated new memory for d without releasing the memory it was previously pointing to.
🌐
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.
🌐
Educative
educative.io › answers › what-is-the-dangling-pointer-problem-in-cpp
What is the dangling pointer problem in C++?
The value that ptr previously held was 5. Then inside another scope, it kept the value of 10. This makes the ptr pointer inside the main scope a dangling pointer pointing to a memory location that is out of its scope.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dangling-void-null-wild-pointers
Dangling, Void , Null and Wild Pointers in C - GeeksforGeeks
July 11, 2026 - Accessing such a pointer can lead to undefined behavior and program errors. It usually occurs after freeing dynamically allocated memory or when a local variable goes out of scope.