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

How to have valid references to objects owned by containers that dynamically move their objects?

If you have a pointer or reference to an object contained in a container, say a dynamic array or a hash table/map, there's the problem that the objects don't permanently stay there, and so it seems any references to these objects become invalid…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
0
votes
3 answers

Question related to std::string object and c_str() method in 3 different implementations

I saw a strange behavior the other day. So I wanted to store lines(present in a vector) in a char array and wanted to use '\n' as delimiter. I know c_str() method in string class returns a pointer to a char array ending in '\0'. Based on my…
0
votes
1 answer

Creating a factory function in Rust

I'm just starting out learning Rust, and I've hit a bit of a roadblock; I'm trying to create a function that initialises the rusty_v8 library. They've given the following code get set up with: use rusty_v8 as v8; let platform =…
J-Cake
  • 1,526
  • 1
  • 15
  • 35
0
votes
2 answers

Dangling pointers in C

When a pointer is allocated memory using malloc, pointer (say x)will now point to memory address. Later I free this(x) memory pointer,but pointer is still pointing to it's old memory. This would now create dangling pointer. (Because I did not point…
bbcbbc1
  • 95
  • 7
0
votes
3 answers

Error (Segmentation Fault) while reusing a dangling or null pointer

#include #include int main ( void ) { char *title=NULL; title = (char *) malloc(15); strcpy(title, "C Programming"); printf("String = %c", *title); free(title); // title=NULL; //or title = 0; strcpy(title,…
0
votes
0 answers

How do I create a new instance containing reference field in Rust?

I'm writing an application that communicates with a database. I thought the new method in the Repository would initialize the struct member conn to the reference of MysqlConnection::establish(...) in the below code. struct Repository<'a> { conn:…
Simon Park
  • 694
  • 1
  • 7
  • 18
0
votes
1 answer

Why one method of reading string in C using char* pointer works while other doesn't?

I am reading a string in C using the pointers, through the following two methods, both are exactly same(at least they appear to me). The only difference is in one method I have made a dedicated function to do the job. Method 1: // Works as…
deadLock
  • 390
  • 6
  • 16
0
votes
2 answers

C gmtime() Variable scope

I have been working with C and I thought that a pointer should not point to a local variable, but the library holds a function gmtime(), which seems to return a pointer to a variable created inside of it. Is my understanding correct? time_t…
0
votes
1 answer

C++ : unallocated pointer to object can be repeatedly safely dereferenced... why?

why is this code practically reliably working, is not unstable, undefined? it's dereferencing unallocated, dangling pointers to objects. thanks. #include using namespace std; class Base{ public: void vf() { …
Petr
  • 11
  • 1
0
votes
2 answers

Global pointer and dangling pointer

#include #include int *p; void f() { int a=5; p=&a; printf("%d ",*p); } void main() { f(); printf("%d",*p); } Why the O/P is 5 5? After the function call is over then the local variable a is removed. So p…
0
votes
1 answer

Struggling with dangling references and suggestions for static lifetimes in Rust

Excuse my novice Rust-ing. I wrote a function to clean a file, and verified that the logic of the function appears to be fine. I then wanted to rename this function from main to a helper function. I'm aware that I could keep some of IO stuff in the…
cryptograthor
  • 455
  • 1
  • 7
  • 19
0
votes
1 answer

How does clang++ 9.0 magically cure dangling reference use in a lambda?

I was experimenting on wandbox in hopes of finding compiler warnings that would help with inadvertent dangling references in lambdas. I had this example which misbehaves in multiple ways: std::array,N>…
Mark
  • 101
  • 1
  • 9
0
votes
2 answers

Do we get a dangling pointer by copying a dangling pointer?

On one hand, if I write the following code : int* fortytwo_pointer () { int v = 42; return &v; } I get a dangling pointer and I can check this by calling printf ("forty two : %d\n", *(fortytwo_pointer())); On the other hand int* copy (int…
QGarchery
  • 29
  • 4
0
votes
1 answer

How is the memory allocated to a variable if another (same data-type) variable is freed just before it?

I saw this piece of c++ code under the topic of dangling pointer on the internet and tried executing it. This might not be pleasant to look at but here goes... int *x, *y, *z; x = new int; *x = 3; y = x; delete x; x = NULL; z = new int; *z = 5; *y…
Sunny
  • 51
  • 1
  • 7
0
votes
2 answers

How is returning a variable "let" inside a function allowed in rust?

This example compiles and returns the "expected" output. But is this not a dangling pointer scenario? If so, how come the rust compiler allows this? use serde_json::{Value, json}; use std::io::Result; fn main(){ println!("{:#?}",…
sprajagopal
  • 322
  • 1
  • 4
  • 20