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
1154
votes
20 answers

Can a local variable's memory be accessed outside its scope?

I have the following code. #include int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The…
Avi Shukron
  • 6,088
  • 8
  • 50
  • 84
264
votes
7 answers

What is the difference between a weak reference and an unowned reference?

Swift has: Strong References Weak References Unowned References How is an unowned reference different from a weak reference? When is it safe to use an unowned reference? Are unowned references a security risk like dangling pointers in C/C++?
73
votes
8 answers

What is a dangling pointer?

I know this is pretty common question, but still new for me! I don't understand concept of dangling pointer, was googling around, and writing test methods to find one. I just wonder is this a dangling pointer? As whatever example I found was…
code muncher
  • 1,592
  • 2
  • 27
  • 46
71
votes
3 answers

Is it legal to compare dangling pointers?

Is it legal to compare dangling pointers? int *p, *q; { int a; p = &a; } { int b; q = &b; } std::cout << (p == q) << '\n'; Note how both p and q point to objects that have already vanished. Is this legal?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
38
votes
0 answers

Can we return string literal in C

Is this code valid? const char* foo() { return "Hello World"; } That is, return "Hello World" from a C function. const char* str = foo(); Will str be a dangling pointer? PS: The above function is abstracted from some real code. I see someone…
Ryan
  • 509
  • 4
  • 7
30
votes
6 answers

Safer way to expose a C-allocated memory buffer using numpy/ctypes?

I'm writing Python bindings for a C library that uses shared memory buffers to store its internal state. The allocation and freeing of these buffers is done outside of Python by the library itself, but I can indirectly control when this happens by…
ali_m
  • 71,714
  • 23
  • 223
  • 298
21
votes
3 answers

Detect dangling references to temporary

Clang 3.9 extremely reuses memory used by temporaries. This code is UB (simplified code): template class my_optional { public: bool has{ false }; T value; const T& get_or_default(const T& def) { return has ? value…
vladon
  • 8,158
  • 2
  • 47
  • 91
20
votes
1 answer

Why does std::string_view create a dangling view in a ternary expression?

Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling string view: const std::string&…
gexicide
  • 38,535
  • 21
  • 92
  • 152
17
votes
6 answers

Why is there no safe alternative to unique_ptr::operator*()?

std::vector has the member function at() as a safe alternative to operator[], so that bound checking is applied and no dangling references are created: void foo(std::vector const&x) { const auto&a=x[0]; // What if x.empty()? Undefined…
Walter
  • 44,150
  • 20
  • 113
  • 196
15
votes
4 answers

Is there any way to check if pointer is dangling?

I have a code where I use a pointer to access some datablock. In some rare cases, a few members of the datablock are empty, and as a result the pointer becomes dangling. In fact, I get the correct pointer but the program crashes when trying to do…
Nikhil
  • 299
  • 3
  • 6
  • 17
15
votes
1 answer

Is this constructor initializer causing a dangling reference?

I'm studying the C++ Primer 4th edition by Stanley B. Lippman. In section 12.4.1, when the author talks about constructor initializers, he gives this example: class ConstRef { public: ConstRef(int ii); private: int i; const int ci; …
chanp
  • 675
  • 7
  • 16
14
votes
3 answers

Dangling reference in inner lambda

I have an inner lambda that uses one of the referenced variables of the outer lambda like this: int x=0; auto outer=[&](){ return [&](){ x=5; }; }; auto inner= outer(); inner(); std::cout << x; I tried it. It worked well. However, I…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
13
votes
2 answers

Will this async trick work or the state will be dangling when I access it?

I am facing a situation where it would be nice to launch an std::async operation totally asynchronously. future MyClass::MyAsyncFunc() { std::future f = std::async(...); return f; } // The future goes out of scope, will…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
11
votes
3 answers

Is checking the value of a dangling pointer safe or Undefined Behavior?

We can only de-reference a valid pointer and we can only check the address that a dangling built-in pointer points to. We cannot access its value (the value in the address of object it is pointing to). int* ptr = nullptr; if(ptr) // != 0x00000000 …
Itachi Uchiwa
  • 3,044
  • 12
  • 26
11
votes
4 answers

Writing a function to free a pointer and the assigning it NULL

I was asked this in a recent interview, basically writing a function to combine the functionality of free and assigning null. I answered in the following manner: void main() { int *ptr; ptr = new int; ptr =…
Harshad Kshirsagar
  • 491
  • 2
  • 7
  • 11
1
2 3
11 12