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 - A dangling pointer is a pointer that refers to a memory location that is no longer valid.
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ dangling-pointer-in-c
Dangling Pointer in C: Causes, Types, Fixes & Examples
June 16, 2026 - Dangling pointers sit at the intersection of memory lifetime, ownership, and pointer aliasing. If you already know pointer basics, the next step is recognising when an address becomes invalid; for a related foundation, see Function Pointers in C when reviewing pointer syntax and indirection.

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
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.

๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Dangling_pointer
Dangling pointer - Wikipedia
August 8, 2026 - Dangling pointers arise during object destruction, when an object that is pointed to by a given pointer is deleted or deallocated, without modifying the value of that said pointer, so that the pointer still points to the memory location of the deallocated memory.
๐ŸŒ
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
๐ŸŒ
Javatpoint
javatpoint.com โ€บ dangling-pointers-in-c
Dangling Pointers in C - javatpoint
September 4, 2014 - Dangling Pointers in C with Tutorial or what is c programming, C language with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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. It usually appears/occurs at the object destruction time whenever the object deleted or de-allocated from memory without modifying pointer value.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ dangling pointer in c
Dangling Pointer in C - Scaler Topics
June 14, 2022 - So, When we deallocate a memory block using free() function and do not modify the pointer value, it will cause the pointer to act as a Dangling Pointer. The free() function takes a single parameter, i.e. a pointer pointing to the memory to be deallocated. The diagram below shows how a dangling pointer is created in case of the deallocation of 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 - As str character pointer is non-visible in Outer Block , then character pointer to pointer strPtr is still pointing to same invalid memory location in Outer block , then character pointer to pointer strPtr becomes "Dangling Pointer".
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ dangling-pointer-in-programming
Dangling Pointer in programming - GeeksforGeeks
May 14, 2024 - 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.
๐ŸŒ
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 - However, if the allocator has reused that memory block for a different variable, you will silently corrupt the active data, causing unpredictable logic errors that are notoriously difficult to debug. There is no special syntax for a dangling pointer...
๐ŸŒ
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 - A dangling pointer is a pointer that references memory that is no longer valid or has been deallocated.
๐ŸŒ
Tutorialforgeeks
tutorialforgeeks.com โ€บ dangling-pointer-in-c
Dangling Pointer in C | TutorialforGeeks
A dangling pointer is a pointer ... the memory at that address is no longer available for use. Syntax: There is no special syntax for a dangling pointer....
๐ŸŒ
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 the image Pointer1, Pointer2 is pointing to a valid memory object but Pointer3 is pointing to a memory object that has been already deallocated. So Pointer3 becomes a dangling pointer when you will try to access the Pointer3 then you will get the undefined result or segmentation fault.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ dangling-void-null-and-wild-pointers-in-cplusplus
Dangling, Void, Null and Wild Pointers in C++
July 30, 2019 - It is like calling a disconnected phone number. When the local variable is not static, the pointer pointing to it becomes dangling. Following is the syntax to the dangling pointer: int *show(void) { int n = 76; /* ...
๐ŸŒ
Dot Net Tutorials
dotnettutorials.net โ€บ home โ€บ dangling pointer in c
Dangling Pointer in C Language with Examples - Dot Net Tutorials
November 17, 2023 - The pointer variable that points to an inactive or dead memory location is called the Dangling Pointer. In other words, we can say that a pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer in the C language.
๐ŸŒ
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 - Key Takeaway: After deallocating memory with delete, always set the pointer to nullptr to ensure it no longer points to invalid memory. ... A dangling pointer occurs when a function returns the address of a local variable.
๐ŸŒ
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.
๐ŸŒ
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 - They allow us to access and manipulate the variable stored in that memory addresses directly. This enables programmers to perform efficient memory management and low-level programming tasks. When a pointer points to a deallocated memory block (or invalid memory location), it is called a dangling pointer in C programming.