Re: memory allocation problem

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 02/18/04


Date: Wed, 18 Feb 2004 11:44:09 -0000


"chandra sekhar" <ccadmin@sisl.co.in> wrote in message
news:403350FD.DA178334@sisl.co.in...
> Hi All,
> I am trying to allocate memory for member variable in a static
> method belongs to same class.. the allocation is o.k and when try to
> access the member variable I find that it is a null pointer.
>
> Here is the code ...
>
> #include <iostream.h>
>
>
> class A
> {
> public:
> A():i(0){}
> ~A(){ if(i) { delete i; i = 0;}}
> static void create(A** obj)
> {
> *obj = new A();
> int* ii = 0;
> (*obj)->get_i(ii);
> ii = new int;
> }
> void get_i(int*& g_out)
> {
> g_out = i;
> }
> private:
> int* i;
> };
>
> int main()
> {
> A* ob = 0;
> A::create(&ob);
> int* i = 0;
> ob->get_i(i);
> try
> {
> cout << " value " << *i << endl;
> }catch(...)
> {
> // thows the null pointer exception
> }
> if( ob ) { delete ob; ob = 0;}
> return 0;
> }
>
> Any suggestions in this regard is welcome.
>
> Thanks
> Chandra-
>

Nowhere in the code above do you assign to the member variable i, so it
stays NULL. Where did you think you were changing member variable i?

john