473,396 Members | 1,892 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,396 software developers and data experts.

Objects in Memory (Behind the Scenes Question)

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

Similar topics

1
by: Alfonso Morra | last post by:
Anyone knows why it is not possible (at least without a great deal of "jiggery-pockery" behind the scenes), to store objects that contain virtual functions in a shared memory block? I think I...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
6
by: Don Stewart | last post by:
From within a .NET 1.1 application and using C# Code, I need to get a list of all the instatiated objects in the application, with the following columns: TypeName, Instatiation-Count, Size...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
9
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the...
19
by: jsanshef | last post by:
Hi, after a couple of days of script debugging, I kind of found that some assumptions I was doing about the memory complexity of my classes are not true. I decided to do a simple script to...
12
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.