dangling-pointer

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

一世执手 提交于 2019-12-01 20:40:28
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, because the p will go out of scope, and the function will return a wild pointer (dangling pointer). The example in C# is OK, because the anonymous new person will stay in scope as long as there is a reference to it. (The calling function gets one.) Did I get this right? Xeo person* NewPerson(void) { person p(); /* ... */ return &p; //return pointer to person.

What's the difference between delete-ing a pointer and setting it to nullptr? [duplicate]

喜你入骨 提交于 2019-12-01 19:32:14
This question already has an answer here: delete vs NULL vs free in c++ 6 answers Is saying delete pointer and pointer = nullptr the same? Probably not, but does the latter free up memory? What about delete pointer; pointer = nullptr / pointer = nullptr; delete pointer ? Why not use that to make a safe way to delete pointers prematurely if required, where they would normally be deleted some other time and cause an error with a normal delete? It is not the same, because while you may be setting the pointer to null, the contents that the pointer pointed to would still be taking up space. Doing

Detect dangling references to temporary

坚强是说给别人听的谎言 提交于 2019-11-30 06:18:27
Clang 3.9 extremely reuses memory used by temporaries. This code is UB (simplified code): template <class T> class my_optional { public: bool has{ false }; T value; const T& get_or_default(const T& def) { return has ? value : def; } }; void use(const std::string& s) { // ... } int main() { my_optional<std::string> m; // ... const std::string& s = m.get_or_default("default value"); use(s); // s is dangling if default returned } We have tons of code something like above ( my_optional is just a simple example to illustrate it). Because of UB all clang compiler since 3.9 starts to reuse this

Detect dangling references to temporary

空扰寡人 提交于 2019-11-29 06:03:41
问题 Clang 3.9 extremely reuses memory used by temporaries. This code is UB (simplified code): template <class T> class my_optional { public: bool has{ false }; T value; const T& get_or_default(const T& def) { return has ? value : def; } }; void use(const std::string& s) { // ... } int main() { my_optional<std::string> m; // ... const std::string& s = m.get_or_default("default value"); use(s); // s is dangling if default returned } We have tons of code something like above ( my_optional is just a

Dangling Pointer in C

穿精又带淫゛_ 提交于 2019-11-28 02:20:57
I wrote a program in C having dangling pointer. #include<stdio.h> int *func(void) { int num; num = 100; return &num; } int func1(void) { int x,y,z; scanf("%d %d",&y,&z); x=y+z; return x; } int main(void) { int *a = func(); int b; b = func1(); printf("%d\n",*a); return 0; } I am getting the output as 100 even though the pointer is dangling. I made a single change in the above function func1() . Instead of taking the value of y and z from standard input as in above program, now I am assigning the value during compile time. I redefined the func1() as follows: int func1(void) { int x,y,z; y=100; z

realloc() dangling pointers and undefined behavior

泪湿孤枕 提交于 2019-11-27 07:33:11
问题 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 be some other object getting allocated into what happens to be the memory that was used before, and if you use the pointer to access memory, that's obviously undefined behavior. Dangling pointer memory overwrite lesson 1, pretty much. But what if the

Sieve of Eratosthenes algorithm in C

前提是你 提交于 2019-11-27 06:31:22
问题 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 chunk of dynamically allocated memory that holds all the primes <= num. *count will have the count of primes. Here is my function getPrimes : void getPrimes(int num, int* count, int** array){ (*count) = (num - 1); int sieve[num-1], primenums = 0, index,

Dangling Pointer in C

雨燕双飞 提交于 2019-11-27 04:54:13
问题 I wrote a program in C having dangling pointer. #include<stdio.h> int *func(void) { int num; num = 100; return &num; } int func1(void) { int x,y,z; scanf("%d %d",&y,&z); x=y+z; return x; } int main(void) { int *a = func(); int b; b = func1(); printf("%d\n",*a); return 0; } I am getting the output as 100 even though the pointer is dangling. I made a single change in the above function func1() . Instead of taking the value of y and z from standard input as in above program, now I am assigning

Is it legal to compare dangling pointers?

北城以北 提交于 2019-11-26 22:31:22
Is it legal to compare dangling pointers? int *p, *q; { int a; p = &a; } { int b; q = &b; } std::cout << (p == q) << '\n'; Note how both p and q point to objects that have already vanished. Is this legal? Introduction: The first issue is whether it is legal to use the value of p at all. After a has been destroyed, p acquires what is known as an invalid pointer value . Quote from N4430 (for discussion of N4430's status see the "Note" below): When the end of the duration of a region of storage is reached, the values of all pointers representing the address of any part of the deallocated storage

Is it legal to compare dangling pointers?

孤人 提交于 2019-11-26 08:24:05
问题 Is it legal to compare dangling pointers? int *p, *q; { int a; p = &a; } { int b; q = &b; } std::cout << (p == q) << \'\\n\'; Note how both p and q point to objects that have already vanished. Is this legal? 回答1: Introduction: The first issue is whether it is legal to use the value of p at all. After a has been destroyed, p acquires what is known as an invalid pointer value . Quote from N4430 (for discussion of N4430's status see the "Note" below): When the end of the duration of a region of