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
votes
1 answer

Returning the address of a local variable and dynamically created variable

Code 1: int *g(){ int *p; p=(int *)malloc(sizeof(int)); *p=10; return p; } Code 2: int *g(){ int p=10; return &p; } OUTPUTS: Code 2 ----> [Warning] function returns address of local variable [-Wreturn-local-addr] Still…
-1
votes
2 answers

Can I create a struct that can only be dynamically allocated?

In C++, can I create a struct that can only be dynamically allocated? I want to enforce a way to make sure that the programmer cannot statically allocate this struct because doing so would lead to a dangling pointer since I need to use the contents…
Anonymous
  • 295
  • 2
  • 13
-1
votes
2 answers

Declaring temporary variables from vector> in a for-loop

I am trying to declare temporary variables in a sequence of for-loops. To do that, I use a range-based loop for each "vector container" (i.e. vector > containerXYZ) and declare temporary variables within each for-loop. Here's part of…
-1
votes
1 answer

create a linked list of items containing pointers in c++

I have a little problem with my code. In fact, I can not understand why when inserting in the list in mind, the last element to insert is in the whole list which is impossible according to my algorithm. So I created 04 classes: STUDENTS, NOTES,…
-1
votes
2 answers

Dangling pointer in case of integer pointer vs char pointers

I am trying to understand how dangling pointers work in c++. int* get_int_ptr() { int i=10; int* ptr; ptr = &i; return ptr; } char* get_char_ptr() { char str[10]; strcpy(str,"Hello!"); return(str); } int main() { …
sarthak0415
  • 83
  • 1
  • 6
-1
votes
2 answers

Prevent call of function on object being deleted

I'm trying to fix an issue caused by call of method on object that is being destructed which results in my application crash. I have following classes: // Forward declarations class A; class B { public: B(A* aPtr) : m_pA(aPtr) { Schedule(); } …
-1
votes
1 answer

Why is below two codes provide different results?

The first piece of code is: #include char *getString() { char *str = "Will I be printed?"; return str; } int main() { printf("%s", getString()); } And the second piece of code is: #include char *getString() { …
-1
votes
3 answers

What happens when data is written into a memory location which is pointed by a dangling pointer of a different data type

If pointer data type is same as the newly entered data,i guess it wouldn't give an error,but if the pointer has a different data type ,we'll have a type mismatch. I was wondering whether the compiler would do something about it(say delete the…
-1
votes
2 answers

C freeing check doesn't work

I have written a method to free my struct. Now I have a problem. When I call this method twice, it gives me an error. But I do check if there is something in my struct so I don't know how it is possible that it gives me the error. My struct: typedef…
Lennart
  • 383
  • 4
  • 16
-1
votes
6 answers

Why I don't get a dangling pointer?

Why I is the output of the following code 10 and not an error? Isn't object2 a dangling pointer? #include using namespace std; int *method() { int object = 10; return &object; } int main() { int *object2 = method(); cout…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
-2
votes
2 answers

Dangling pointer issue C++

I'm making a game engine and I have run into a problem with destroying elements. For example I have some cameras that follow and look at an target (a gameobject or an actor). Later in the game the camera target is destroyed and the camera code will…
Fewnity
  • 11
  • 2
-2
votes
1 answer

dangling pointer is still accessing the memory value

I am pretty new to this concept and I am confused that if a dangling pointer is a pointer which points to a memory location which points to memory which has been freed or deleted then in this case why it is still able to call the function…
StealthTrails
  • 2,281
  • 8
  • 43
  • 67
-2
votes
1 answer

Does referencing solves the pointer problems?

Hi I'm very confused using references. If we use reference instead of pointer does it solve the dangling or even memory leakage problem?
mellodi
  • 15
  • 1
  • 1
  • 9
-2
votes
2 answers

Why the dangling pointer is giving the size of the previously pointed variable?

class B { public: B():a(0), b(0) { } B(int x):a(x), b(0) { } private: int a; int b; }; class A { public: A(B* ptr):pB(ptr) { } void modifypB() { delete pB; pB = NULL; } void printBSize() …
-3
votes
2 answers

why Dangling pointer cannot store any value and why does it throw 0?

why can Dangling pointer not store any value and why does it throw 0? As it points to same memory which is freed. why 0 if we try to store some value? #include #include int main() { int *p; …
Sumit Kumar
  • 25
  • 1
  • 7
1 2 3
11
12