Wednesday, July 27, 2005

Dangling Pointer

Last night I was trying my hand on C++ .... and I came across an interesting problem ...
all I was trying to do following
1) create a class ( say TestClass ) which will have an int variable( age ), and we can set value into this variable using 2 methods(set and get).
2) create a global scope pointer of this type in the file.( say m_TestClass ) ...
3) write a method( say createTestClassObject ) where I will create an object of the class "TestClass" and the pointer( m_TestClass ) will point to this object.
then I will do a set and get( printf ).
4) "createTestClassObject" method will be called from the main and after this method returns to main .. I will do another m_TestClass->getAge()( priintf ) .
This is what I have written ...( I have used .NET IDE )

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "conio.h"
#using
using namespace System;
using namespace std;

void createTestClassObject();


class TestClass
{
int m_Age;
public:
TestClass()
{
// dummy constructor
}

void setAge( int age )
{
m_Age = age;
}

int getAge()
{
return m_Age;
}
};

TestClass *m_TestClass;
int main()
{
createTestClassObject();
printf("\n%d", m_TestClass->getAge() );
int i = getch();
return 0;
}

void createTestClassObject()
{
TestClass obj;
m_TestClass = &obj;
m_TestClass ->setAge(100);
printf("\n%d", m_TestClass->getAge());
}

the output of the program is first 100 and then it is 4( sometime it is 6, sometime some crazy values), I was wondering why not my program is printing 100 twice?

After spening some time on it I got the cause .. as "obj" has a local scope( in the routine createTestClassObject ), hence the object dies when program flow returns to "main" and m_TestClassbecome a Dangling Pointer( I hope I am correct :) ).

I was just trying out several options to fix this issue ... and one of them paid off , this is what I did ...
I have just modified the createTestClassObject routine .. and the updated version looks like........
void createTestClassObject()
{
TestClass *obj = new TestClass();
m_TestClass = obj;
m_TestClass ->setAge(100);
printf("\n%d", m_TestClass->getAge());
}

All I have changed is created a pointer of TestClass and it points to new instance of TestClass, and it started working, but all in all I am confused, doesn't the object( new TestClass() )get dies when the routine returns to the main function ? I am in dark, can anybody please throw some light.
Neo.





1 comment:

Neo said...

Thanks to Atul sir and PalB( a true geek )
Now I have understood the issue,

the issue was following ...
TestClass obj; .............. this statement creates an instance of TestClass in the stack( that is quite new concept for any seasoned programmer of .NET -:) ) and the object gets cleared from the stack when the program flow goes back to Main, hence it was printing junk values instead of 100. In the case of TestClass *obj = new TestClass(); an instance of TestClass gets created in the heap and it stays until the program free the memory explicitly( using the "delete" keyword ), hence in the second case it worked.

My special thanks to Atul sir for spending his time on my blog and clear my doubts.

Neo.