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

Objects in Memory (Behind the Scenes Question)

P: n/a
I have some questions regarding memory and C++ Objects. For these
questions, I'll use the following sample for explanation sake:

------------------------------------
class CBase
{
public:
int iGetA() { return iVarA; }
void vSetA(int iNewA) { iVarA = iNewA; }
private:
int iVarA;
};

class CTierOne : public CBase
{
public:
int iGetB() { return iVarB; }
void vSetB(int iNewB) { iVarB = iNewB; }
private:
int iVarB;
};

int main(void)
{
CBase* myBase = new CBase; // LINE 1
CTierOne* myTier = new CTierOne; // LINE 2
// Relevant code here
delete myBase;
delete myTier;
}
------------------------------------

Q1) In LINE 1 the 'new' keyword allocates memory and instantiates the
class CBase as an object, myBase. The memory that is allocated, what
is actually in there? Is it just allocating memory for the member
variable iVarA or is it allocating space for the executable code
contained in the iGetA() and iSetA methods? Or is it allocating memory
for the variable iVarA and 2 pointers to the executable code for the 2
methods, iGetA() and iSetA()?

Q2) In LINE 2 when the CTierOne class is instantiated as the myTier
object, how is the memory arranged as far as contiguous storage? Is a
block of memory set aside and the first part of the memory contains
all of the data for CBase and the second all of the data for CTier?

Q3) Is there an easy way to do a memcpy of all of the variable data in
an object into a byte array so that it can be "offloaded" (either
written to a file or a database) and at a later time, re-instantiate
the same class as another object and copy that data back into the
object?

If anyone knows the answer to any of these questions, I would greatly
appreciate it!

Thanks!

-JH
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
JHenstay wrote:

Q1) In LINE 1 the 'new' keyword allocates memory and instantiates the
class CBase as an object, myBase.
Yep.
The memory that is allocated, what
is actually in there? Is it just allocating memory for the member
variable iVarA or is it allocating space for the executable code
contained in the iGetA() and iSetA methods? Or is it allocating memory
for the variable iVarA and 2 pointers to the executable code for the 2
methods, iGetA() and iSetA()?
That would be silly.
In the object there only need to be the things that are different from
other CBase objects. Everything else can be shared.
So memory is reserved for the iVarA member variable, while the code
snippets reside somewhere else in memory.

Q2) In LINE 2 when the CTierOne class is instantiated as the myTier
object, how is the memory arranged as far as contiguous storage? Is a
block of memory set aside and the first part of the memory contains
all of the data for CBase and the second all of the data for CTier?
That depends on your compiler, but typically that would be the case.

Q3) Is there an easy way to do a memcpy of all of the variable data in
an object into a byte array so that it can be "offloaded" (either
written to a file or a database) and at a later time, re-instantiate
the same class as another object and copy that data back into the
object?


In principle one can do that iff he is very careful and knows what he does.
In general you should not do this. At the moment your class contains virtual
functions this strategie is bound to fail. Also if your class contains
pointers or other members which by themselfs are not POD this strategy becomes
a mess.

Stick to the simple strategy:
In a write function write each member seperately in turn.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.