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 - Alternatively, if the pointer is used for writing to memory, some other data structure may be corrupted. Even if the memory is only read once the pointer becomes dangling, it can lead to information leaks (if interesting data is put in the next structure allocated there) or to privilege escalation ...
๐ŸŒ
Number Analytics
numberanalytics.com โ€บ home โ€บ blog โ€บ engineering โ€บ dangling pointers in data structures
Dangling Pointers in Data Structures
June 23, 2025 - A dangling pointer is a pointer that points to a memory location that has been deallocated or reused. When a program allocates memory using malloc() or new, it receives a pointer to the beginning of the allocated memory block.
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
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
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.

๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ dangling-pointer-in-c
Dangling Pointer in C: Causes, Types, Fixes & Examples
June 16, 2026 - A dangling pointer in C is a pointer that still stores the address of an object whose lifetime has ended. It matters because dereferencing such an address can corrupt data, crash a program, or expose security bugs such as use-after-free in payment, ...
๐ŸŒ
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. 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.
๐ŸŒ
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 - They are De-allocation of memory, Function Call, and Variable goes out of the scope. These dangling pointers are used when the memory management and pointers is having most of the common bugs.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_dangling_pointers.htm
Dangling Pointers in C
Dangling pointers in C is used to describe the behavior of a pointer when its target (the variable it is pointing to) has been deallocated or is no longer accessible. In other words, a dangling pointer in C is a pointer that doesn't point to a valid
Find elsewhere
๐ŸŒ
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 - Attackers can exploit dangling pointers to execute malicious code by injecting their payload into the memory the pointer references. ... When a pointer references deallocated memory, that memory might later be allocated to another part of the program or an external source. Attackers can manipulate this process to gain control over the programโ€™s behavior. Imagine a system where sensitive data (like passwords) is stored in memory.
๐ŸŒ
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 pointers in computer programming are pointers that pointing to a memory location that has been deleted (or freed). Dangling pointers arise during object destruction, when an object that has an incoming reference is deleted or deallocated, ...
๐ŸŒ
Quora
quora.com โ€บ What-is-dangling-pointer-in-c
What is dangling pointer in c? - Quora
Answer (1 of 18): Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.In short pointer pointing to non-existing memory location is called dangling pointer....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ dangling-void-null-wild-pointers
Dangling, Void , Null and Wild Pointers in C - GeeksforGeeks
July 11, 2026 - Understanding these pointer types helps prevent common programming errors like invalid memory access, crashes, and memory leaks. A dangling pointer is a pointer that points to a memory location that has already been deallocated or deleted.
๐ŸŒ
Rookienerd
rookienerd.com โ€บ tutorial โ€บ c โ€บ c-dangling-pointers
C Dangling Pointers - Rookie Nerd
While programming, pointers are used that contain memory addresses of data objects. Dangling pointer is a pointer that points to the memory location even after its deallocation. Or we can say that it is pointer that does not point to a valid data object of the appropriate type.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ dangling, void , null and wild pointer in c
Dangling, Void , Null and Wild Pointer in C - Aticleworld
May 25, 2021 - In simple words, you can say that โ€œa dangling pointer is a pointer that points to invalid memory or to memory that is not valid anymore and can be the cause of the undefined behaviorโ€. Letโ€™s see the below image for a better understanding.
๐ŸŒ
LearnMandu
learnmandu.com โ€บ home โ€บ blog โ€บ memc blog โ€บ dangling pointers in c - a comprehensive guide to prevention
Dangling Pointers in C - A Comprehensive Guide to Prevention
March 29, 2026 - A dangling pointer is an active threat because it looks and acts like a valid pointer. If you dereference it (e.g., *ptr = 42;), you initiate a use-after-free operation. If the operating system has unmapped the page, this causes an instant ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ dangling-pointer-in-programming
Dangling Pointer in programming - GeeksforGeeks
May 14, 2024 - Dangling pointers occur when a pointer points to a memory location that has been deallocated or freed. If a program attempts to access or modify the memory through a dangling pointer, it can lead to unpredictable behavior, including crashes ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ dangling pointer in c
Dangling Pointer in C - Scaler Topics
June 14, 2022 - So as the name suggests, Dangling Pointers are the pointers that point to some freed/deleted location from the program's memory (memory that is currently not in the use of the program). When we talk about allocation and deallocation of memory blocks, we see Dynamic Memory Allocation concepts.
๐ŸŒ
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 - Instead, the pointer inaccurately refers to the memory location of the deallocated memory. The pointer is considered "dangling" since it points to memory that might no longer hold a valid object.
๐ŸŒ
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 - A dangling pointer in C is a pointer that points to a memory location that has been freed or is invalid. They lead to undefined behavior, memory leak issues, etc.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ dangling-void-null-and-wild-pointers-in-cplusplus
Dangling, Void, Null and Wild Pointers in C++
July 30, 2019 - A dangling pointer is a variable that points to invalid or freed memory, causing errors if accessed. It is like calling a disconnected phone number.