473,671 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1592
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
1636
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 know the answer - possibly something to do with unresolvable function addresses when mapping from vf table to virtual address - but this is an uneducated guess at best, on my part. It would be useful to get a more experienced C++ programmers' view...
6
2399
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
3592
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
3143
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 I was thinking of using the Garbage Collector somehow or perhaps a Heap and Stack Dump? Using an external utility application is NOT an option. Does anyone have any ideas?
27
2553
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 Standard is saying that "object is a region of storage", I deduce from that that literal constants aren't objects because they may not be alocated as regions of storage in the memory.
9
9224
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 website. The problem is: it's eating more memory than a black hole. It eats the current limit of 256MB set in php.ini, in an application that would hardly consume 4MB if written in C. I don't care if this application takes much longer to run...
19
4634
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 isolate the problem: class MyClass: def __init__(self,s): self.mystring = s
12
3582
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 gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
8
3608
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 class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8673
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.