Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object size 1

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
Hello anybody...

I have a question about the size of the objects.
Could anybody tell me what kind of information is stored in memory for a particular object?

Is it the object data (pointers, static etc.) along with pointers to each object members(i know the VTable is memorized in each object) stored or is it only the object data, with its VTable and the compiler determines the place of member implementation by the object type...

I really don't know this nither am i to sure about the way C++ compilers generally work, so does anybody know about this?

I'm asking this because when i look at a String object i get really scared... The STL approach provides a HUGE object and i think it would be an enormous waste of space to use a String object somewhere where a lot of them would be necessary.
Yet again, i think that i might be missing something, so pleas help me out... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
I give my example to show a C++ code and equivalent C code.
Like this code, all other C++ capabilities can be
implemented and explained. I hope my code can give you
a little idea.


#include <iostream.h>

class A {
protected:
int data;

public:
virtual void virtual_method() { cout << &quot;A::virtual method\n&quot;; }
void method() { virtual_method(); }
};

class B : public A {
public:
// the following virtual method overrides virtual_method defined in the class A
virtual void virtual_method() { cout << &quot;B::virtual method\n&quot;; }
void method() { A::method(); }
};


// We can write equivalent C code to explain how C++ compiler works.

struct _A {
int data;
// virtual method is actually a function pointer
// Some compilers like VC doesn't manage its vtable in this way.
// you know COM object used in C language calls its method like this :
// a->lpVtbl->method();
// It is the way MS VC++ manages their vtable.
void (* virtual_method)( void );
};

// the follwing constructor for class A is defined automatically
// by C++ compiler.

static void _A_virtual_method( void )
{
cout << &quot;_A::virtual method\n&quot;;
}

void _A_construct( struct _A* a )
{
// C++ compiler generates this code automatically
a->virtual_method = _A_virtual_method;
}


// the first argument of C++ method is the pointer of the class
// : it is 'this' pointer
// But some compilers like VC++ doesn't use use this way. They
// send 'this' pointer to ECX register and it is called
// thiscall calling convention

void _A_method( struct _A* a /* <----- this pointer */ )
{
a->virtual_method();
}


struct _B {
struct _A a; // simulate inheritance in C
};


// the following constructor for class B is also defined automatically
// by C++ compiler

static void _B_virtual_method( void )
{
cout << &quot;_B::virtual method\n&quot;;
}

void _B_construct( struct _B* b )
{
// C++ compiler generates this code automatically
b->a.virtual_method = _B_virtual_method; // _A_virtual_method is overrided
}


// the name of method in class B need to be mangled by a programmer in C.
// C++ does it internally and automatically but you don't know what is the
// actual name of B::method if you use C++.

void _B_method( struct _B* b )
{
// A::method() was done in C++ so we simply do it
_A_method( &b->a );
}


int main( void )
{
// C++
B b; // constructor for class B is called here
b.virtual_method(); // virtual method call
b.method(); // a method call

// C
struct _B _b;
_B_construct( &_b ); // we need to call constructor for _B explicitly in C
_b.a.virtual_method(); // virtual method call
_B_method( &_b ); // a method call

return 0;
}


// end of code
Hee S. Chung
heesc@netian.com
 
Ok Hee Chung, thanks for the post. I am quite familiar about the virtual mechanisms in C++, anyway, that VC++ stuff with ECX register is really interesting and i didn't know about it.

So, if i understood corectly from your struct analogy, an object is represented in memory along with its pointers to the member functions.

Then, i want to ask you (or anybody bothering looking at this) again about using string class. Seems to me that when you say
Code:
string s(&quot;This is a somehow long piece of string&quot;);
the actual data memorized for the string data itself would be about 40 bytes (didnt' count them) while the object itself would occupy some 200 or more bytes!
What if you would have to use the string class to keep individually all the words in the posts from this thread? Would you use 10kb of RAM or even more for a 1 kb text?

I know that you can implement your own class of string manipulation, but... This would be the STL we're talking about... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
I'm sorry maybe my source cannot be answer for you...
Maybe I couldn't fully understand your question -
English is not my native language.

And for your new question, do you mean sizeof(string)
is 200 or more bytes? I tested it but it say it is
16 bytes.

cout << sizeof(string);

By the way, how could you say the size of object itself
is 200 or more bytes?

If the class 'string' occupies relatively many bytes
as you said, I'll never use string class. But in
my test, it is 16 bytes and I think it can be
acceptable.
Hee S. Chung
heesc@netian.com
 
Well, this is just the point of my question.
If you look in STL, you will find that the string class has a great number of methods along with it. I didn't count them, but i think they are some 30 or 40 methods.

This is my question:
Do the pointers to the methods get stored with the object itself?
If you would have an empty object (no data contained), with 30 methods, would or would not its size be 120 bytes (as far as 4 bytes is the size of a pointer).

Thanks again for your interest Hee Chun... And... By the way, english is not my native language either, so i might have made some mistakes myself... :(.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
If so, my source code can give some help.

As you see in my source, plain method contained
in a C++ class is not actually contained it.
You can see that in the equivalent C code.
Only data members and virtual functions affect
the size of a class. My source code proves it.

Thank you so much.
Hee S. Chung
heesc@netian.com
 
Ok, thanks for clearing that up for me! It was a great help, i appreciate it! [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
Hi Nosferatu

:)Seems like we (had && have && will have) similar doubts ... Just skim through... I tried to answer ur questions at the bottom... hope this helps...

The declaration of a class (or struct, for that matter) like say, MyType below:
class MyType {
//...//
//...//
};
does not define any objects of MyType but only specifies what they will contain. Class specification only provides a template and does not provide a memory space for the objects. And it is at this time that the pointer to the methods get stored (if that happens). Note that till now no allocation has been done for any of the data members as no object of type MyType has been defined.

It is only when the object is declared, that the necessary memory space is allocated.
Objects can be declared during the class declaration
class MyType {
//...//
//...//
} x, y, z;
or later by
MyType x, y, z; // or string x, y, z; as in your case
And this is when the space for the objects is allocated(and at a different place away from wherever the methods are stored). The total space would be 3*(sizeof(MyType)) as there are three objects (x, y, z) each occupying a space of sizeof(MyType). Incidentally (and obviously) sizeof(MyType) gives the sizeof the data members only, irrespective of how many methods are there in the class.

&quot;Do the pointers to the methods get stored with the object itself?&quot;
The pointer to the member funtions are NOT stored with the data members. However, if u specifically use a 'pointer to a function' as a member of the class (the only way to have a function inside a struct in C - and obviously this is NOT done normally in C++), it gets stored as any other pointer.

&quot;If you would have an empty object (no data contained), with 30 methods, would or would not its size be 120 bytes (as far as 4 bytes is the size of a pointer).&quot;
Absolutely NOT. Like I said earlier, the functions are stored elsewhere. The object would more or less be stored similarly to an empty similar struct in C with just the class data members as the members of the struct in C. Ofcourse the exact space will depend on what/how the data members are declared...

As far as that string class is concerned, the sizeof(string) returns the sizeof its data members, I presume.

Hope this helped. PLEASE correct me if I'm wrong. :) Ankan.

Please do correct me if I am wrong. s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top