memory allocation problem

J

John Harrison

chandra sekhar said:
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
 
C

chandra sekhar

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-
 
C

chandra sekhar

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
 
P

Patrik Stellmann

static void create(A** obj)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...
 
R

Rolf Magnus

chandra said:
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.
 
J

John Harrison

chandra sekhar said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,429
Latest member
JacelynKit

Latest Threads

Top