dangling-pointer Questions

4

I have a code where I use a pointer to access some datablock. In some rare cases, a few members of the datablock are empty, and as a result the pointer becomes dangling. In fact, I get the correct ...
Immingle asked 5/10, 2011 at 9:24

2

Solved

I am facing a situation where it would be nice to launch an std::async operation totally asynchronously. future<void> MyClass::MyAsyncFunc() { std::future<void> f = std::async(...); ...

5

Solved

My code: #include <stdio.h> #include <stdlib.h> int main(void) { int *p = (int *)malloc(sizeof(int)); free(p); *p = 42; return 0; } I created a pointer, then I pointed it to all...
Gaiser asked 21/12, 2013 at 21:23

1

Solved

I'm studying the C++ Primer 4th edition by Stanley B. Lippman. In section 12.4.1, when the author talks about constructor initializers, he gives this example: class ConstRef { public: ConstRef(i...
Guzel asked 25/5, 2012 at 10:45

7

Solved

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 ...
Ultrasonics asked 10/12, 2011 at 11:57

21

Solved

I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the ...
Giacometti asked 22/6, 2011 at 14:5

7

Solved

Is it compulsory to initialize t in the following code, before assigning value to t? Is the code correct? void swap(int *x, int *y) { int *t; *t = *x; *x = *y; *y = *t; }
Birr asked 23/3, 2011 at 10:33

4

Solved

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; Sampl...
Louislouisa asked 12/8, 2010 at 13:58

4

I was asked this in a recent interview, basically writing a function to combine the functionality of free and assigning null. I answered in the following manner: void main() { int *ptr; ptr = ne...
Fraise asked 2/7, 2010 at 7:6

2

Solved

Is there a simple, efficient weak/guarded pointer? I need multiple pointers to the same object that are all automatically set to NULL when the object is deleted. There is one "master" pointer that ...
Younger asked 9/12, 2009 at 19:15

© 2022 - 2024 — McMap. All rights reserved.