473,605 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size of object

hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.

now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.

Am i right ??

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.

Regards
Tarun
Jul 22 '05 #1
18 2524

"Tarundeep" <ta*******@gmai l.com> wrote in message > hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.

now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.
First of all things we are talking about are implementation specific.
Generally a class would not hold a vtable pointer but each object of class
would hold it.
Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.

Am i right ??
No, there is no such promise about v-tables or memory layout made by the
Standard. An implementation _could_ choose to have it the way you are
saying.
Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.


All that the standard ensures is that if it is the most derived class in a
hierarchy then it will have a size greater than zero. EBCO (Empty base class
optimization) can come into picture with empty base classes.

Sharad
Jul 22 '05 #2
"Tarundeep" <ta*******@gmai l.com> wrote in message
news:86******** *************** **@posting.goog le.com...
hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.
The size of a pointer on a 32-bit processor might or
might not be 32 bits.

now i want to know what would be the size of the class with vtable
pointer in it ,
that is it has few virtual functions.
The following uses the C++ notion of 'size', as reported
by 'sizeof', that is, measured in bytes.

The size of an object of class type is not affected by how
many member functions the class has, virtual or not.

The size of an object of class type will be at least the
sum of the sizes of its data members (but possibly larger,
since padding is allowed between members); or lacking any
data members, greater than zero.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.
The address of an object of class type will be the same address
as that of its first declared data member, if one is declared.
'vtable' is not part of the language, it's a mechanism some
implementations use to achieve polymorphism.

Am i right ??
Nope. :-)

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.


The existence or number of member functions does not affect the
size of an object of class type. The size of an object of
class type which has no data members will be greater than zero
(exact size left up to the implementation) .

Summary: The actual physical implementation of an object (such
as one of a polymorphic type) might consume more storage than
that reported by 'sizeof'. This extra 'overhead' is not considered
part of the object's size or representation, nor part of the application's
address space.

-Mike
Jul 22 '05 #3
Mike Wahler wrote:
"Tarundeep" <ta*******@gmai l.com> wrote in message
news:86******** *************** **@posting.goog le.com...
hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.
The size of a pointer on a 32-bit processor might or
might not be 32 bits.

now i want to know what would be the size of the class with vtable
pointer in it ,
that is it has few virtual functions.


The following uses the C++ notion of 'size', as reported
by 'sizeof', that is, measured in bytes.

The size of an object of class type is not affected by how
many member functions the class has, virtual or not.


Actually, the OP might be not that far off. In many implementations
(especially vtable based ones), the sizeof(T) for a class is affected by
the presence of virtual functions more or less in the way the OP assumes.
The following non-portable trick to do a compile time check for polymorphic
classes relies on it:

template < unsigned long M, unsigned long N >
class CHECK_is_equal {
private:

template < unsigned long A >
class XXX {
public:

void operator== ( const XXX & ) const {};

};

public:

CHECK_is_equal ( void ) {
XXX< M > a;
XXX< N > b;
a == b;
}
};

template < typename T >
class CHECK_is_polymo rphic {
private:

class NonVirtual : public T {
public:

~NonVirtual ( void ) {}

};

class Virtual : public T {
public:

virtual ~Virtual ( void ) {}

};

public:

CHECK_is_polymo rphic ( void ) {
CHECK_is_equal< sizeof( NonVirtual ), sizeof( Virtual ) > x;
}
};

This works because the vtable pointer consumes some space in the object. As
far as I know, boost::is_polym orphic<> uses a refinement of this idea. I
agree, however, that it is by no means guaranteed by the standard.

The size of an object of class type will be at least the
sum of the sizes of its data members (but possibly larger,
since padding is allowed between members); or lacking any
data members, greater than zero.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.


The address of an object of class type will be the same address
as that of its first declared data member, if one is declared.
'vtable' is not part of the language, it's a mechanism some
implementations use to achieve polymorphism.

Am i right ??


Nope. :-)

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.


The existence or number of member functions does not affect the
size of an object of class type. The size of an object of
class type which has no data members will be greater than zero
(exact size left up to the implementation) .

Summary: The actual physical implementation of an object (such
as one of a polymorphic type) might consume more storage than
that reported by 'sizeof'. This extra 'overhead' is not considered
part of the object's size or representation, nor part of the application's
address space.


I doubt that objects of class T are allowed to use more memory than

malloc( sizeof(T) );

allocates. However, there could be a misunderstandin g on my part, because I
am not really clear about what you mean by "overhead" that "is not
considered part of the object's size or representation, nor part of the
application's address space". As far as I can tell, everything that the
compiler needs to stick into an object to make it work as prescribed by the
standard is part of its representation, although the standard makes no
claims as to what those tings might be that compiler sticks in there.
However, if a compiler finds it convenient to stick in a vtable pointer,
then I do not see any language in the standard that would sizeof() prevent
from accounting for that pointer. (Then, again, the standard is long; and I
might be missing something.)
Best

Kai-Uwe Bux
Jul 22 '05 #4
Tarundeep posted:
hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.
A very fair assumption.
now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.
No necessarily the very first part of the object, but again to assume so
would be a fair assumption.
Am i right ??

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.
If the function is NON-virtual, then an object of the class shouldn't
consume ANY memory. (But again, a perverted implementation may choose to do
whatever it likes!).

If the function is virtual, then an object of the class should theoretically
be of the size: sizeof(void*), ie. the size of a pointer (to the vtable).
But again, it's all implementation specific...

Note, even if you declare the function as virtual, if the compiler can
figure out at compile time what function you want to call, it may optimize
away the vtable, leaving you with objects which take up no memory.

There's some rule somewhere I believe that sizeof(ANYTHING ) >= 1 in C++, but
still this doesn't mean that objects of the class will actually allocate any
memory.
-JKop

Regards
Tarun


Yeah, that makes perfect sense.
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL > wrote in message > Tarundeep posted:

If the function is NON-virtual, then an object of the class shouldn't
consume ANY memory. (But again, a perverted implementation may choose to do whatever it likes!).
There is valid reason why implementations reserve some memory for objects of
empty classes. Think about array of such a class and addresses of elements
in it.
There's some rule somewhere I believe that sizeof(ANYTHING ) >= 1 in C++, but still this doesn't mean that objects of the class will actually allocate any memory.


Well, let's get what the Standard says (5.3.3/2)- "The size of a most
derived class shall be greater than zero". The reason for "most derived" is
that an implementation could implement EBCO.

Sharad
Jul 22 '05 #6
On Tue, 14 Sep 2004 05:21:02 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:
Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.


The address of an object of class type will be the same address
as that of its first declared data member, if one is declared.


That is true only for POD types, and may or may not be true for
non-PODs. On most implementations it isn't true for objects with
virtual functions.

Tom
Jul 22 '05 #7
"Tarundeep" <ta*******@gmai l.com> wrote in message
news:86******** *************** **@posting.goog le.com...
let us say it is a 32 bit processor then the size of the pointer is 32
bits.

now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.
The typical implementation will add one word to the contest of the object
for a vtable pointer, plus any padding that might be necessary for alignment
purposes. The real question, though, is why do you care?
Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.
There is no reason to believe that the vtable pointer will always be at the
beginning of the object.
Also what is the size of the class with no data member and just one
function.


For most implementations , the size of an object is the size of its data
members, plus one word for the vtable, plus one word for the vtable of every
base class (direct or indirect) that has virtual functions, plus padding.

But I still want to know why you care.
Jul 22 '05 #8
> For most implementations , the size of an object is the
size of its data
members, plus one word for the vtable, plus one word for the vtable of every base class (direct or indirect) that has virtual functions, plus padding.
"plus one word for the vtable of every base class..."

BULLSHIT.

But I still want to know why you care.

It's called curiousity and wanting to know what goes on
under the hood. What's with the arogant attitude, is it
because you're an asshole?
-JKop
Jul 22 '05 #9
"JKop" <NU**@NULL.NULL > wrote in message
news:wP******** **********@news .indigo.ie...
For most implementations , the size of an object is the

size of its data
members, plus one word for the vtable, plus one word for

the vtable of
every base class (direct or indirect) that has virtual

functions, plus
padding.


"plus one word for the vtable of every base class..."

BULLSHIT.

But I still want to know why you care.

It's called curiousity and wanting to know what goes on
under the hood. What's with the arogant attitude, is it
because you're an asshole?


Good job. You've just insulted a person who is more
capable of helping you with C++ than well over 99 percent
of everyone posting here.

If you have issues with the correctness of someone's
assertions, then provide rational refutation.

Expletives and name calling are the traits of an
immature child, produce nothing of value, and are
not appreciated here.

-Mike

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
4522
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
2
22777
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86) ==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
2
28557
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
6
2463
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following. It's not guaranteed behaviour but it seems to work. Doesn't lose verifiability either. Enjoy. Cheers Jon Jagger namespace JSL
6
40994
by: cameron | last post by:
I need to get the size of an objet in memory. I have tried: System.IO.MemoryStream m = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); b.Serialize(m, Obj); double size = Convert.ToDouble(m.Length); but not everything is serializable. The thing is I want to be able to
4
3487
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a snippet from my .css file: *************************** body { margin:0; padding:0;
5
5120
by: Phil Jones | last post by:
Is there a way to determine the size (number of bytes) of an object? I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do this for performance reasons. I'm hoping there is some Framework class that dishes up the in-memory size of an object. Is there??? Thanks everyone....
8
564
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie it won't be more than, say, 1K, but it would be tidier to use the actaul size. Is it actually possible to find out how big an object (or even better, a class) would be when it is serialised (I suppose one way would be to separately serialise it...
6
14326
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the data and send the other one to a thread that records it's content to the disk. How do I do such thing? TIA, Scirious.
0
7934
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
8425
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
8288
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
6743
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
5445
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();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
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
0
1271
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.