Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is a dangling pointer? C++
What is a dangling pointer? C++
✍: Guest
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. 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.
2012-01-06, 2957👍, 0💬
Popular Posts:
How is the MVC design pattern used in Struts framework? In the MVS design pattern, there 3 component...
Can JavaScript steal text from your clipboard? It is true, text you last copied for pasting (copy &a...
What will be printed as the result of the operation below: main() { char s1[]="Cisco"; char s2[]="sy...
Write out a function that prints out all the permutations of a string. For example, abc would give y...
What is the difference between strings and character arrays? A major difference is: string will have...