By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,033 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,033 IT Pros & Developers. It's quick & easy.

memory allocation problem

P: n/a
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-

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"chandra sekhar" <cc*****@sisl.co.in> wrote in message
news:40***************@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
Jul 22 '05 #2

P: n/a
To change the contents of the variable memory for the variable should
exists.. I am checking the memory existance by calling get_i in the main()
Here the problem is the memory for the variable is not existing inspite of
allocation in the static method.

Thanks
Chandra

John Harrison wrote:
"chandra sekhar" <cc*****@sisl.co.in> wrote in message
news:40***************@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


Jul 22 '05 #3

P: n/a
>>> 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;
}

you functions writes the value of i to g_out but what you need is i
itself (or a pointer to it). So something (extremly 'ugly') like
int** get_i()
{
return &i;
}
static void create(A** obj)
{
*obj = new A();
int** ii = (*obj)->get_i(ii);
*ii = new int;
}

would probably do. This is - of course - very bad design since you pass
out a pointer to a member variable and there are most likely better ways
to achieve what you're looking for. Maybe a set_i function but the
decision also depends on what else you're doing with this class...

Jul 22 '05 #4

P: n/a
chandra sekhar wrote:
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.


Your above code is extremely obfuscated. However, you never set the i
member of your object and neither the local i variable in main() to a
valid memory address, so it still is a null pointer in the cout line.

Jul 22 '05 #5

P: n/a

"chandra sekhar" <cc*****@sisl.co.in> wrote in message
news:40***************@sisl.co.in...
To change the contents of the variable memory for the variable should
exists.. I am checking the memory existance by calling get_i in the main()
Here the problem is the memory for the variable is not existing inspite of
allocation in the static method.

Thanks
Chandra


Nothing in the static method allocates memory and assigns it to i.

I cannot understand why you think it should.

You need to do something like this

static void create(A** obj)
{
*obj = new A();
(*obj)-> i = new int; // allocate some memory and assign it to i
}

john
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.