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
5
votes
4 answers

Dangling pointer example

In the following code, why does s1.printVal causes a dangling pointer error? Isn't the s1 object, i.e. its pointer, still accessible until it's destroyed? class Sample { public: int *ptr; Sample(int i) { ptr = new int(i); …
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
5
votes
3 answers

What is the difference between a dangling pointer and memory leak?

I'm new to C++ and would like to ask if the code below is an example of a dangling pointer or a memory leak because it is pointing outside the dynamically allocated array: int * n = new int[10]; for (int prev = 0; prev < 10; prev++) { *n = *(n +…
5
votes
5 answers

Memory allocation stack

In the stack, memory is reserved for main which we call stack frame for the main function. When we call the Add function, memory is reserved on top of the stack. In the Add function stack frame, a and b are local pointers and c is an integer which…
Stack
  • 235
  • 3
  • 15
4
votes
7 answers

Safe in C# not in C++, simple return of pointer / reference

C++ code: person* NewPerson(void) { person p; /* ... */ return &p; //return pointer to person. } C# code: person NewPerson() { return new person(); //return reference to person. } If I understand this right, the example in C++ is not OK,…
Niklas
  • 1,753
  • 4
  • 16
  • 35
4
votes
2 answers

prevent initializing std::optional> with rvalue std::optional

std::reference_wrapper cannot be bound to rvalue reference to prevent dangling pointer. However, with combination of std::optional, it seems that rvalue could be bound. That is, std::is_constructible_v, int&&>) is…
slyx
  • 2,063
  • 1
  • 19
  • 28
4
votes
1 answer

Is it UB to return a pointer to local variable?

Yes, I know perfectly well you should not do that. If we have this code: int *foo() { int a = 42; return &a; } As most C coders know, this is undefined behavior: Using pointer after free() int *p = foo(); printf("%d\n", *p); Just so that…
klutt
  • 30,332
  • 17
  • 55
  • 95
4
votes
1 answer

Dangling object warning in free after realloc failure

I'm finalizing my function for safe string retrieval and decided to turn my compiler warnings up to see if any of my code raised any flags. Currently I am receiving the following compiler warnings on Pelles C IDE: stringhandle.c(39): warning #2800:…
Keith Miller
  • 1,337
  • 1
  • 16
  • 32
3
votes
1 answer

Coroutines: Do co_yielded string_views dangle?

I want to mix up the co_yielding string literals and std::strings Generator range(int first, const int last) { while (first < last) { char ch = first++; co_yield " | "; co_yield std::string{ch, ch, ch}; …
Tom Huntington
  • 2,260
  • 10
  • 20
3
votes
1 answer

Temporaries lifetime in N3290 C++ draft

A point from N3290 C++ draft, § 12.2, 5th point, line 10. The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the…
user751747
  • 1,129
  • 1
  • 8
  • 17
3
votes
0 answers

crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS at CI::GLContext::init()

Start seeing this crash on Crashlytics after we built with XCode 9, and all crashes happen on iOS 11 devices (though it could just be most people are on iOS 11 today) Crashed: com.apple.main-thread 0 CoreImage 0x186ff417c…
yvetterowe
  • 1,239
  • 7
  • 20
  • 34
3
votes
2 answers

What does fflush() do in terms of dangling pointers?

I came across this page that illustrates common ways in which dangling pointes are created. The code below is used to illustrate dangling pointers by returning address of a local variable: // The pointer pointing to local variable becomes //…
Arpith
  • 570
  • 2
  • 10
  • 26
3
votes
4 answers

Clarify dangling pointer in C/C++

I'm little confusing about dangling pointer in C/C++ void remove(){ Node* curr = new Node(10); Node* pt = curr; delete curr; // do something here // do other thing here } I assume Node* pt is still dangling pointer before the function remove() is…
1234
  • 539
  • 3
  • 12
3
votes
1 answer

Dangling Pointer - Find Out when The Object is being Created

I'm debugging a software Use-After-Free bug using windbg(Don't have access to source code). Where An Object Created --(do something)--> Object Deleted --(do something)--> Object Reference Re-used [App. CRASHHHH!!!] Using windbg and 'PageHeap' I…
Dev.K.
  • 2,428
  • 5
  • 35
  • 49
3
votes
3 answers

Dangling pointer example confusion

Why isn't the following example correct? Why doesn't it demonstrate a dangling pointer? My teacher said it doesn't show the dangling pointer. Thanks in advance! int X = 32; int *p = &X; free(p); *p = 32; //<------Shouldn't this line cause dangling…
user5595985
3
votes
5 answers

Is destructor called when removing element from STL container?

Say I have two containers storing pointers to the same objects: std::list fooList; std::vector fooVec; Let's say I remove an object from one of these containers via one if its methods: std::vector::iterator itr = std::find(…
random
  • 33
  • 1
  • 3
1 2
3
11 12