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

Dangling pointers and double free

After some painful experiences, I understand the problem of dangling pointers and double free. I am seeking proper solutions. aStruct has a number of fields including other arrays. aStruct *A = NULL, *B = NULL; A = (aStruct*) calloc(1,…
user151410
  • 776
  • 9
  • 22
3
votes
4 answers

dangling pointer, reason for value change after free()?

In the following code segment, after free(x), why does y become 0? As per my understanding, the memory in the heap that was being pointed to by x, and is still being pointed to by y, hasn't been allocated to someone else, so how can it change to…
Aman Jain
  • 10,927
  • 15
  • 50
  • 63
3
votes
2 answers

Sieve of Eratosthenes algorithm in C

Okay, so this function I created uses the Sieve of Eratosthenes algorithm to compute all the primes <= n. This function stores the prime numbers and the count of primes in the parameters. When the function exits, primes should be pointing to a…
2
votes
1 answer

I don't understand why I have a dangling pointer

I have written this method: std::string Utils::GetFileContents(const char* filePath) { std::ifstream in(filePath, std::ios::binary); if (in) { std::string contents; in.seekg(0, std::ios::end); …
Valentin Popescu
  • 174
  • 2
  • 12
2
votes
1 answer

Returning ref to data from function leads to dangling ref issue

Please check the two code snippets below. While in sample 2, there clearly resides dangling reference issue as ref of local variable is passed, do you think the same problem exists in sample 1? I myself think sample 1 is correct. While data was…
Faisal
  • 35
  • 1
2
votes
2 answers

Is there a C equivalent to Rust's NonNull::dangling() pointer instantiation?

If it exists, it should satisfy the following properties: Has the type void * Does not require the instantiation of a "dummy object" to act as the address It is guaranteed to not compare equal to NULL Can be constructed without invoking undefined…
Ryan
  • 112
  • 1
  • 6
2
votes
3 answers

Is this undefined behaviour in C++ calling a function from a dangling pointer

A question came up here on SO asking "Why is this working" when a pointer became dangling. The answers were that it's UB, which means it may work or not. I learned in a tutorial that: #include struct Foo { int member; void…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
2
votes
2 answers

In Windows API application-defined callback functions, what is the lifetime of pointer parameter data? Does it persist after the callback returns?

As an example, let's look at EnumWindowStations(), which requires the caller to pass an EnumWindowStationsProc() callback function. The callback function will be invoked once for every window station in the current terminal session. Let's look at…
bgoldst
  • 34,190
  • 6
  • 38
  • 64
2
votes
1 answer

Move semantics in Rust

I'm wrapping a C library in Rust, and many of its functions take parameters by pointers to structs, which themselves often have pointers to other structs. In the interest of reducing overhead, I'd like to provide the ability to cache the results of…
jmegaffin
  • 1,162
  • 11
  • 22
2
votes
1 answer

C++ State Pattern implementation: Mechanism of pointer to State Machine becoming invalid?

While trying to implement a simple State Pattern example from the book "Head First Design Patterns", I came across a situation which strikes me as kind of peculiar. Mind you, this question is not about implementing the pattern correctly, but about…
mbw
  • 352
  • 3
  • 10
2
votes
3 answers

realloc() dangling pointers and undefined behavior

When you free memory, what happens to pointers that point into that memory? Do they become invalid immediately? What happens if they later become valid again? Certainly, the usual case of a pointer going invalid then becoming "valid" again would…
Myria
  • 3,372
  • 1
  • 24
  • 42
2
votes
5 answers

Trouble with dangling pointers and character arrays in C

main(){ char *cmd1[20] = {NULL}; int x = parse_command(cmd1); printf("%s\ ",cmd1[0]); } parse_command(char *inTempString){ char tempString[256]; (call to function that assigns a string to tempString) cmd1[0] = tempString; } There…
Pate
  • 21
  • 1
2
votes
3 answers

c: strategies for debugging obscure memory leaks?

I'm working on a project in c, and I'm trying to understand how to debug an obscure bug that crashes my program. Its kinda large, attempts to isolate the problem by making smaller versions of the code are not working. So I'm trying to come up with a…
jason dancks
  • 1,152
  • 3
  • 9
  • 29
2
votes
4 answers

When an std::vector grows are addresses to elements within it no longer valid?

Suppose I have the following: struct Foo { Foo () : bar(NULL), box(true) {} Bar* bar; bool box; }; and I declare the following: std::vector vec(3); I have a function right now which does something like this: Foo& giveFoo() { //finds a certain…
Palace Chan
  • 8,845
  • 11
  • 41
  • 93
1
vote
0 answers

Will there be dangling references to map values if it gets resized?

Would reference to values in std::map or std::unordered_map be valid/maintained if the map's being inserted new elements or removed from? For example, is the below code safe? Or would there be dangling references from any edge cases or map…