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
1
vote
2 answers

How char[] and char* are different in this case?

When we run this piece of code, it works normally and prints string constant on the screen: char *someFun(){ char *temp = "string constant"; return temp; } int main(){ puts(someFun()); } But when we run the following similar code, it…
buch11
  • 872
  • 3
  • 13
  • 29
1
vote
1 answer

Return a shared pointer from a function vs Capturing a shared pointer in a Lambda

I am constructing a shared pointer in a function_1 and giving it as a capture to a lambda. I think this is an issue, could you please confirm if this safe or I am right and I shoudn't be doing this? #include #include #include…
Vero
  • 313
  • 2
  • 9
1
vote
1 answer

Dangling reference solution

T&& operator[](std::size_t n) && noexcept {std::cout<<"move"<
1
vote
1 answer

C++ Dangling pointer issue

I am using the Raylib GUI framework for a project that displays all the nodes within an iteration of the Collatz Conjecture. In my program, I have a class for a Node object that acts simply as a circle with a labelled number. The variable text in my…
1
vote
1 answer

dangling reference in nested vector when parent container reallocates

thing contains 2 vectors, one of foo and one of bar. The bar instances contain references to the foos - the potentially dangling ones. The foo vector is filled precisely once, in things's constructor initializer list, and the bar vector is filled…
Oliver Schönrock
  • 1,038
  • 6
  • 11
1
vote
1 answer

C++ How to debug a dangling pointer in production

Basically the situation is we have a C++ program that occasionally crashes when it attempts to access an already-freed object (in Debug build we notice that the memory being pointed to is full of the usual "cdcdcdcd" pattern). We tried to trace…
Dan
  • 261
  • 2
  • 8
1
vote
1 answer

Is it possible to extract a struct containing fields that are unique/shared pointers from a set

so essentially I have a set of instances of struct A. I want to extract an instance, modify the fields. One of the fields is a unique ptr. I'm not that great at reading c++ errors, it looks like the field is deleted on extraction. I.e. the…
1
vote
0 answers

Does this C++ code acutally have a dangling pointer?

I have class B, which contains a pointer for object of class A. I want them to share their position, so I used std::shared_ptr for it. Below is a short version of this code: struct A { std::shared_ptr pos; A(const A& right,…
Kitsune
  • 11
  • 2
1
vote
1 answer

Failed implementation of a Linked List in C

I do not understand why this program is not working and the elements were not inserted into the List as intended. Every time, when I am debugging, I see that when I go to the main method after the 'insert' method, the Linked List is still empty and…
ib2000
  • 43
  • 7
1
vote
1 answer

How to avoid pointing to deallocated memory (dangling pointers) in classes with pointer members

When an object has a pointer member, the pointer may point to the part of memory that is not owned by the object. Thus, it may be deallocated without informing the object and cause a dangling pointer (and abundant headaches to debug). For example,…
nmnm
  • 47
  • 5
1
vote
1 answer

Relocating intermediate pointers during realloc

Suppose if I have an allocated chunk of memory and an intermediate pointer points to some location in the allocated chunk. If I change the size of the memory by using realloc(), which may relocate the whole chunk with the new size. Is there…
Vishal
  • 59
  • 2
  • 9
1
vote
2 answers

Why returning pointer to locally declated variable is null in place of pointer to location in stack?

In the following code snippet shouldn't str_s should point to some location in stack. #include #include #include char* fun_s(){ char str[8]="vikash"; printf("s :%p\n", str); return str; } char*…
chandola
  • 23
  • 4
1
vote
1 answer

Null dangling pointers c++

I have a pointer to an abstract class object ptr1. I need to create another pointer with the same adress ptr2 so that when you free and zero the ptr1 pointer, the ptr2 also becomes zero. SomeObject* ptr1 = new SomeObject(); SomeObject* ptr2 =…
Egor
  • 107
  • 1
  • 8
1
vote
0 answers

VHDL runtime: invalid memory access (dangling accesses or stack size too small)

Weird error when running the test bench, I have never seen this before. I am attempting to simulate an 8-bit calculator with 4 registers. The calculator has 8 bit instructions for add,subtract, branches on equal, load immediate, and print to the…
Mike Rawding
  • 121
  • 4
1
vote
2 answers

Dangling pointers/references to int and char* constants

I'm reading a book on C++ templates by Vandevoorde, Josuttis and Gregor and do not understand the warning they make about dangling reference. Here is the code: #include // maximum of two values of any type…
Student4K
  • 916
  • 2
  • 9
  • 20