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, 2955👍, 0💬
Popular Posts:
Can one execute dynamic SQL from Forms? Yes, use the FORMS_DDL built-in or call the DBMS_SQL databas...
Where are cookies actually stored on the hard disk? This depends on the user's browser and OS. In th...
How To Delete All Rows a Table? - MySQL FAQs - Understanding SQL INSERT, UPDATE and DELETE Statement...
How does one iterate through items and records in a specified block? One can use NEXT_FIELD to itera...
How To Set session.gc_maxlifetime Properly? - PHP Script Tips - Understanding and Using Sessions As ...