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

Dangling Pointer in C

I wrote a program in C having dangling pointer. #include int *func(void) { int num; num = 100; return # } int func1(void) { int x,y,z; scanf("%d %d",&y,&z); x=y+z; return x; } int main(void) { int *a =…
pradeepchhetri
  • 2,899
  • 6
  • 28
  • 50
1
vote
1 answer

Zoombie vs nil object

In objective C, we can send message to nil object. Zoombie (dangling pointer) object is an object which points to an object whose memory got released.i.e not pointing to any valid memory location. In this case reference doesn't hold nil but the…
srus2017
  • 394
  • 2
  • 14
1
vote
1 answer

Is it possible to redirect to a different address when an address is dereferenced?

I have a fairly large visual studio C++ code base which many people are modifying. There is a requirement to delete an object which possibly many other objects are referring to(using address of raw pointers). I have tried to remove the address…
user9639921
  • 351
  • 2
  • 11
1
vote
1 answer

Creation of controlled type will call finalize on return

I want to create a function for creating and initializing a controlled type (a bit like a factory) in the following manner: function Create return Controlled_Type is Foo : Controlled_Type; begin Put_Line ("Check 1") return Foo; end…
Erik Stens
  • 1,779
  • 6
  • 25
  • 40
1
vote
2 answers

Detect or avoid dead references to temporary on compile time

The following minimal-ish program segfaults when compiling with -O3 and perhaps with -O2, but executes fine with -O0 (with clang 4.0): #include class A { public: virtual void me() const { std::cerr << "hi!\n"; } }; class B { public: …
krlmlr
  • 25,056
  • 14
  • 120
  • 217
1
vote
3 answers

Clang's ASan does not detect dangling pointer use

In the context of a tool comparison, I do not want to be unfair to ASan if it can detect the problem in the program below: $ cat t.c #include int *G; int f(void) { int l = 1; int res = *G; G = &l; return res + *G; } int…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
1
vote
1 answer

Dangling pointer in Delphi

I'm not using interfaces (so the objects has no reference counting). The objects may be referenced by many others, and i need to deal with the dangling pointers. FreeAndNil() doesn't solves the problem for multiple references. I need that when an…
1
vote
3 answers

Is it safe to cast a IDispatch* into an IUnknown*, without using QueryInterface, for interprocess COM objects?

When dealing with interprocess COM objects, is it safe to cast a IDispatch* into an IUnknown*, without using QueryInterface ? Here our IDispatch object comes from an other process OtherProcess.exe. And a colleague of mine says that I should call…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
1
vote
2 answers

SQLite SQLITE_STATIC Local Variable Issue

I am looking at some vendor code and there is a query like this: BOOL BindQuery(sqlite3_stmt* stmt, PARAMS* params) { char temp[150] = ""; char paramBuf[10] = ""; if (currStmt == NULL) return FALSE; sprintf(paramBuf, "%d",…
jn1kk
  • 5,012
  • 2
  • 45
  • 72
1
vote
1 answer

Is the lvalue reference return of a member function of a temporary object a dangling reference?

There is a class CBase. class CBase { ... CBase &Create() { return *this; } ... } If I declare an lvalue reference and a pointer, CBase &kk = CBase().Create(); CBase *pp = &( CBase().Create() ); is kk a dangling…
ursh
  • 11
  • 2
1
vote
3 answers

Object still accessible after std::unique_ptr goes out of scope. Differing runtime behaviours

The following code passes to the function modify_entry a pointer to an object of type Entry and inside the body of the function a unique_ptr adopts the raw pointer. However, the object to which the pointers point seems to live on after the function…
Drake
  • 857
  • 5
  • 10
1
vote
1 answer

How to avoid error when access to the deleted memory block?

If the title is not clear, I have this example: int *a = new int[5]; int*b = a; delete[] a; a = NULL; Now a is NULL but b isn't. If I access b, it will return wrong values and may crash the program. How to prevent this?
Tiana987642
  • 696
  • 2
  • 10
  • 28
1
vote
1 answer

C++ dangling pointer/deep copy/shallow copy confusion

I have heard that dangling pointer problem arises when we assign same address to two different pointers. That is due to both pointers point to same memory location and if memory is freed using address in one pointer; it can still be accessible from…
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57
0
votes
1 answer

Shouldn't this give me dangling reference errors?

I'm returning to C++ after about a decade of other programming languages so bear with me here. The following program compiles for me in a C++20 project in CLion: #include using namespace std; class MyClass { private: public: …
Jason
  • 2,495
  • 4
  • 26
  • 37
0
votes
1 answer

OLECHAR used as pointer - will it dangling pointer if not nullptr? Function CoTaskMemFree()

I generate a GUID and then save it in OLECHAR* with StringFromCLSID(). If I create a function which returns an OLECHAR and not nullptr the OLECHAR after using CoTaskMemFree() - will it cause dangling pointer? I want to return just the value but…