473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

memory allocation problem

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
5 1731

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
15
by: berthelot samuel | last post by:
Hi, I'm trying to develop an application for modeling 3D objects from Bezier patches, but I have a memory allocation problem. Here are my structures: typedef struct _vector3 { union { struct...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
3
by: Florin | last post by:
Hi all, I have a problem related to memory grow on a server application which is basically stateless (I have some static info loaded). The client accesses this server using remoting and it has...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.