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

Reassigning a pointer when the object it is pointing to is deleted by an instance of another class

I am creating a Tower Defence game mockup where enemies attempt to cross the screen before they are destroyed. Pointers to dynamically allocated Enemy objects are stored in a vector inside an EnemyManager class that controls how they spawn and…
0
votes
2 answers

What happens after you delete a ptr C++

I try to delete a ptr using the following code example: int* data = new int(1); int* p = NULL; p = data; *p = 3; delete p; // Prints 3 cout << *p << endl; //Prints 3 cout << *data << endl; The last two lines of code prints 3, and my…
0
votes
9 answers

Dynamically allocated list in C++

I made a cute generic (i.e. template) List class to handle lists in C++. The reason for that is that I found the std::list class terribly ugly for everyday use and since I constantly use lists, I needed a new one. The major improvement is that with…
cantrem
  • 616
  • 2
  • 8
  • 20
0
votes
2 answers

Understanding dangling pointer behaviour in this case

I have a pointer and by default it carries NULL then it waits for some event and gets a value if the event happens, later I am freeing the pointer somewhere else but even after freeing the pointer I am not making it NULL so it still keeps…
Sumit Singh
  • 182
  • 3
  • 14
0
votes
0 answers

Const reference as a member of a class insconsistency

If one creates a const reference to a temporary, its life is extended as if the reference where in the stack. It is a good feature of the language, although it is presented sometimes like an exception to other rules.…
alfC
  • 14,261
  • 4
  • 67
  • 118
0
votes
0 answers

Is there an idiomatic way to drop early in Rust?

In Chapter 4 of The Rust Programming Language, the drop function is introduced as being implicitly called at the end of any scope on all the variables that were allocated within that scope. This solves the usual alloc/dealloc problems. I can imagine…
John Doucette
  • 4,370
  • 5
  • 37
  • 61
0
votes
2 answers

Memory allocation of returned object in c++

What happens to the object returned from the last line of following code class weight { int kilogram; int gram; public: void getdata (); void putdata (); void sum_weight (weight,weight) ; weight sum_weight (weight) ; }; weight…
Abhay
  • 110
  • 2
  • 9
0
votes
1 answer

Reliably Ensure Memory Safety in C++ 14

I'm converting some old C++ code to use shared_ptr, unique_ptr and weak_ptr, and I keep running into design problems. I have "generator" methods that return new objects, and accessor methods that return pointers to existing objects. At first glance…
Ebonair
  • 221
  • 1
  • 11
0
votes
1 answer

Idiomatic way to determine if an object has been destroyed

I've been trying to find a better way to accomplish determining if a particular object has been destroyed (destroy(...)). The way I've been doing it is like so: class C { bool valid = false; this(){ valid = true; } } Then you do: C c…
t123
  • 23
  • 3
0
votes
3 answers

Can't explain why pointers become dangling when creating a vector of vectors of pointers

Consider the following code: #include #include using namespace std; class SomeClass { public: SomeClass(int num) : val_(num) {} int val_; int val() const { return val_; } }; // Given a vector of vector of numbers,…
Gowtham
  • 101
  • 1
0
votes
1 answer

Prevent coder error - return temporary value by reference (dangling pointer)

How to prevent coder from returning local variable as reference? Example 1 I sometimes make a mistake like this :- int& getStaticCache(){ int cache = 0; return cache; //<--- dangling pointer } The correct code is :- int& getStaticCache(){ …
javaLover
  • 6,347
  • 2
  • 22
  • 67
0
votes
1 answer

CreateFile2, WriteFile, and ReadFile: how can I enforce 16 byte alignment?

I'm creating and writing a file with CreateFile2 and WriteFile, then later using readfile with the to read 16 bytes at a time into an __m128i and then performing simd operations on it. Works fine in debug mode, but throws the access denied…
MNagy
  • 423
  • 7
  • 20
0
votes
3 answers

C++ Add derived object of abstract class into another class without dangling pointers?

I have a simple container class that points to an abstract class and I have functions to get/set the pointer in the container class. More concretely, the class looks like this: class Container { Abstract* thing; public: void set(Abstract…
silentwf
  • 153
  • 2
  • 8
0
votes
1 answer

Catching a dangling pointer

I wrote the following code which outputs 45: #include int main() { int *p; { int n = 45; p = &n; } std::cout << *p; } Because the lifetime of n ends at the scope I expected this code to emit an error or…
0
votes
1 answer

How to intentionally preserve and harvest a dangling pointer in C++

I am writing an LLVM plugin. In it, I create a global array of pointers that take data along the flow of different functions in the program. When pointers take an address in a function scope, it is logical to assume that, once outside their scope,…