Questions tagged [dangling-pointer]

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type.

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.

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data.

Source: http://en.wikipedia.org/wiki/Dangling_pointer

167 questions
0
votes
0 answers

question on properly managing reference members in a local class in C++

Is the reason why I got a garbage value because of the following or am I wrong? the Other object o passed as an argument to the Inner constructor, goes out of scope after the Inner object has been created, but before the end of the get_a_value…
Sami
  • 513
  • 4
  • 11
0
votes
1 answer

Does the following program contain a dangling reference?

I have the following program: #include #include using namespace std; using int_arr = int[3]; int& f(int_arr& arr, int index) { return arr[index]; } int main() { int arr[3] = {1, 2, 3}; int& g = f(arr, 0); g =…
ktqq99
  • 25
  • 5
0
votes
1 answer

How to look for dangling pointers?

I'm trying to build a class for a linked list in C++. My professor says that I have a dangling pointer in my code. I've spent hours looking for it but I just can't find it no matter what. I tried asking a friend and they couldn't find it either. Is…
Horsio
  • 3
  • 2
0
votes
1 answer

Why we initialize the next pointer of Linked List as NULL before deletion

Why we initialize the next pointer of Linked List as NULL before deletion we move our head to the next node during deletion and we free the memory of the first node, so why we need to initialize the next pointer of deleted node as NULL before…
user17804723
0
votes
0 answers

bugged const_iterator& operator++() for red-black tree

I'm new to c++ and I face some problems implementing the iterator on the template class Tree (structure of the code includes the canonical classes (Node,Tree,Iterator). Fwd iterator is not working properly when tested (with the following extract) in…
mpv
  • 1
0
votes
2 answers

Storing address of local var via passing a double ptr to function does not cause a seg fault but returning address of that var causes seg fault. Why?

I understand the concept of dangling pointers, func2 returns address of deallocated memory. In func1 instead of returning the address we store by passing a double pointer. Isn't this also a case of dangling pointer ? #include void…
0
votes
0 answers

What is the exact cause of undefined behaviour in this piece of code involving shared_ptr's and lambda ref capture?

I found this piece of code at https://alexgaynor.net/2019/apr/21/modern-c++-wont-save-us/. The author says that in main x goes out of scope, destroying the last reference to the data, and causing it to be freed. At this point y contains a dangling…
0
votes
3 answers

how to monitor dangling pointers that points to leakded memory?

lets think we have a code like this : int * ptr = new int; int *nptr = ptr int * ptr2 = new int; int *nptr2 = 2*nptr+ptr2; delete ptr; delete ptr2; ptr = NULL; ptr2 = NULL; So now the nptr and nptr2 is dangling pointer .I know that we can…
0
votes
1 answer

Why ptr not becomes Dangling pointer because when return pointer who store the address of local variable get destroy after return function?

#include int *func(int * ptr){ int a = 12; int *c = &a; return c; // here it returns the pointer by storing the address of local variable } int main() { int *ptr = NULL; ptr= func(ptr); // here the address is…
0
votes
4 answers

Why is the pointer not free and doesn't it bring me same value of a variable?

In the below code I free the pointer ptr but still *ptr retuns me the same value. If I free the variable then it should give me some garbage value but it didn't. #include #include int main() { int a=5; int *ptr=&a; …
user17804723
0
votes
1 answer

How to return Derived from a function as a reference to Base?

I want to implement a function which returns a reference to Base which actually comprises Derived (types are polymorphic). Something among the lines of the following (incorrect) code: struct Base { virtual ~Base() {} }; struct Derived: Base { int x…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
0
votes
2 answers

Why does the creation of a reference when assigning REQUIRE garbage collection?

Our lecturer in C++ made the following statement: Whenever a scope of some variable ends, the compiler checks whether this variable was an object itself or a reference to it, thus deciding whether to destroy it and make all references to it invalid…
0
votes
0 answers

why const ref of string in the program is still valid?

in below program a string cstis created inside the block scope and store its const reference in unique_ptr d(unique_ptr d is outside block scope). as per my understanding when the block scope ends string cst is destroyed, so…
CY5
  • 1,551
  • 17
  • 23
0
votes
2 answers

why is this dangling pointer in C?

why ptr is dangling pointer. I know "ch" is out of scope, but address of ch is still valid out of inner block. And when I print *ptr I get correct value i.e. 5. void main() { int *ptr; { int ch = 5; ptr = &ch; } …
Ravi
  • 173
  • 1
  • 10
0
votes
3 answers

Printing Dangling Pointers in C

#include int main() { int *ptr; { int x = 2; ptr = &x; } printf("%x %d", ptr, *ptr); return 0; } Output: address of x, value of x. Here, ptr should be a dangling pointer, right? Yet, it still stores…
INDHUJA G
  • 25
  • 4