Click to See Complete Forum and Search --> : What is an Object's Memory Footprint ?


VipulPathak
March 25th, 2003, 07:53 AM
I have some Basic Questions about Objects in C++ :

Consider a class below (Just as an Example) -

class Abc
{
public:
int a ;
short b ;
char cc ;

public:
Abc() { a=1 ; b=2; cc=4 ; } ;
void HelloWorld() { printf("Hello World\n\n") ; }
} ;

Abc J ;


a) What Picture in the Memory will be Created ?
b) Where the Data Members are Placed ?
c) Where the Member Functions are Placed ?
d) How they will be Located, when a user use "J.b" and "J.HelloWorld()" ?
e) On VC++ 6.0, Why the Size of Abc is 8 Bytes ?
f) What is the Memory Footprint of this Object J ?


Thank you !!
-Vipul Pathak, India.

dude_1967
March 25th, 2003, 01:41 PM
Vipul,

The minimum theoretical size of the class in memory could be 7 bytes due to the following for your compiler:



sizeof(int) + sizeof(short) + sizeof(char) = 4 + 2 + 1 = 7



Your compiler chooses to align structures and classes on 8-byte boundaries. Since 8 is the nearest boundary to 7, the size of the class in memory is 8 bytes. By the way it is possible to adjust the alignment (Project... Settings... C++... Struct Alignment).

Data members will most likely be placed in RAM memory, either on the stack or heap or within the program RAM address space, depending on the usage. For example for:



void test(void)
{
Abc abc;
}



the instance abc will most likely be allocated on the stack for the function call.

The member function is placed once somewhere in program code and will be given an internal name by the compiler.

Sincerely, Chris.

:)

VipulPathak
March 26th, 2003, 01:42 AM
Thankyou !

Whay about other Questions ?
How can I visualize the Memory to Locate a Member of Class ?

Thanks Again ...
-Vipul ;

Graham
March 26th, 2003, 03:46 AM
How can I visualize the Memory to Locate a Member of Class ?
Don't even think about it. That's why class members have names. The physical layout of members in memory can vary from compiler to compiler, so you can't trust it.

The point is that all the questions you ask do not have a simple answer: in all cases the answer is: "it depends". It depends on the compiler (possibly even the version of the compiler) and the architecture of the target machine. And, quite frankly, you don't need to know.

Paul McKenzie
March 26th, 2003, 04:36 AM
Originally posted by VipulPathak
How can I visualize the Memory to Locate a Member of Class ?
As Graham pointed out, this is implementation defined. You can't predict where these members are, unless your class or struct contains all POD (Plain Old Data) types -- ints, floats, doubles, etc. and it isn't a derived class. In that case, you have the 'C' offsetof() macro to find where a member exists.

Once the class or struct contains member functions or it is not a base class, there is no standard way of knowing where the members are located, or how the struct or class is laid out in memory. So as Graham says "don't even think about it".

Forgive me for asking, but why do you need to know these details? If you are trying to accomplish something that you believe requires knowledge of these details, there is more than likely a standard way of accomplishing your task. Trying to guess where members are isn't guaranteed to work.

Regards,

Paul McKenzie

VipulPathak
March 26th, 2003, 05:58 AM
Thankyou Sir !

I want to know these details Just for the Sake of Knowledge.
Initially the Question arises, when a class is derived from a class, or even when it is derived from 2 classes. At that time I just asked my self that, How the Data and Operations of these base classes and a derived class is arranged (or mapped) in memory or in resulting object. But before answering that question, I asked another question to myself, How a single class object Arrange these entities ?
The situation of mapping virtual functions in VTBL might be more complex. How these simple functions of these class are mapped in that situation. Even if they contain overridding functions and multiple and multilevel inheritance is present.

I found myself Messed up with these questions, So I asked them at CodeGuru. Can You (or Other People) Answer these ?

Thank you !

-Vipul Pathak;

Elrond
March 26th, 2003, 06:38 AM
The only one that can be answered for sure is the question e).

As pointed by dude_1967, it a problem of data alignement.

It can be changed either in the project settings, or locally changed using the directive: #pragma pack (have a look at the help about that to know more). This alignement affects the result of the sizeof function.

dude_1967
March 27th, 2003, 01:11 PM
Vipul,

As the other users pointed out, there are many implementation-specific details involved in your question. Nonetheless, you can start gaining experience with these types of issues by studying the assembler output and memory-listing files from your Microcoft Developer Studio compiler. It is only one compiler, but it's a good place to start...

To generate assembler output, click on Project... Settings... List Files... Assembler and Machine Code. The assembler listings will be output to the Debug or Release directories, depending on the active configuration. Study the code in Release since the Debug version is non-optimized.

To generate a memory-listing file, go to Project... Settings... Linker... and check the box for generate list file.

Good luck.
Chris.

:)