search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
27 views
asked Geek 15.3k points 14 39 58

3 Answers

0 votes
A dangling pointer arises when you use the address of an object after its lifetime is over. This

may occur in situations like returning addresses of the automatic variables from a function or using

the address of the memory block after it is freed. Example: The following code snippet shows this:

class Sample

{

public:

int *ptr;

Sample(int i)

{

ptr = new int(i);

}

~Sample()

{

delete ptr;

}

void PrintVal()

{

cout << “The value is ” << *ptr;

}

};

void SomeFunc(Sample x)

{

cout << “Say i am in someFunc ” << endl;

}

int main()

{

Sample s1= 10;

SomeFunc(s1);

s1.PrintVal();

}

In the above example when PrintVal() function is called it is called by the pointer that has been freed

by the destructor in SomeFunc.
answered Guru 39.1k points 10 21 31
0 votes
A dangling pointer arises when you use the address of an object after its lifetime is over.This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.
answered Geek 15.3k points 14 39 58
0 votes
A dangling pointer arises when you use the address of an object after its lifetime is over.This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.
class Sample

{

public:

int *ptr;

Sample(int i)

{

ptr = new int(i);

}

~Sample()

{

delete ptr;

}

void PrintVal()

{

cout << “The value is ” << *ptr;

}

};

void SomeFunc(Sample x)

{

cout << “Say i am in someFunc ” << endl;

}

int main()

{

Sample s1= 10;

SomeFunc(s1);

s1.PrintVal();

}

In the above example when PrintVal() function is called it is called by the pointer that has been freed
by the destructor in SomeFunc.
answered Geek 15.3k points 14 39 58

Related questions

0 votes
1 answer 101 views
101 views
. What is a smart pointer?
asked steve Sr Member 1.5k points 8 23 36
0 votes
1 answer 44 views
44 views
What is an opaque pointer?
asked steve Sr Member 1.5k points 8 23 36
0 votes
1 answer 1.6K views
1.6K views
what is multi-programming?
asked albertina Geek 15.7k points 5 9 21
0 votes
1 answer 10 views
10 views
asked smith Geek 15.3k points 14 39 58
0 votes
1 answer 32 views
32 views
asked smith Geek 15.3k points 14 39 58
...