Objects in Memory (Behind the Scenes Question)

J

JHenstay

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
 
K

Karl Heinz Buchegger

JHenstay said:
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.
 

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
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top