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, 2980👍, 0💬
Popular Posts:
What is the benefit of using an enum rather than a #define constant? The use of an enumeration const...
How Can we change priority & what levels of priority are provided by Dot Net? Thread Priority ca...
What will be printed as the result of the operation below: main() { int x=20,y=35; x=y++ + x++; y= +...
What are shared (VB.NET)/Static(C#) variables? Static/Shared classes are used when a class provides ...
How To Set Background to Transparent or Non-transparent? - CSS Tutorials - HTML Formatting Model: Bl...