Fedora Linux Support Community & Resources Center
  #1  
Old 8th September 2007, 10:20 PM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
Size of a class

I've defined a class MyClass; it has no member functions, just data. Data is: 11 double's and 3 unsigned short's.

That should amount a total of 94 bytes (11*8 + 3*2). However, if I print a sizeof(MyClass) to the console, it comes out with 96 bytes... Why?

And it becomes more puzzling: the same code in windows using MSYS and MinGW, comes out with a result of 104 bytes!!

I've checked, and in both platforms, double's are 8 bytes and unsigned short's are 2 bytes... So what are these extra bytes? How can I make sure the class will be the same size on both platforms (I intend to do a multiplatform app).

Thanks.
Reply With Quote
  #2  
Old 8th September 2007, 10:35 PM
ppesci's Avatar
ppesci Offline
Registered User
 
Join Date: May 2006
Location: Caracas, Venezuela
Posts: 1,828
Size depends on how compiler work and what libraries you use. The extra bytes is for wrap your data in a class I think. If you want the same size, maybe you get better (but not guaranteed) results, if you use a struct to hold your data.
__________________
Pietro Pesci Feltri

PowerBook 15" G4 and
MacBook Pro 17" Intel Core 2 Duo
Intel I5 Desktop
Reply With Quote
  #3  
Old 8th September 2007, 11:02 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,076
Quote:
Originally Posted by pepribal
I've checked, and in both platforms, double's are 8 bytes and unsigned short's are 2 bytes... So what are these extra bytes?
The compiler may add extra bytes to align the data members for optimal access speed.
Quote:
Originally Posted by pepribal
How can I make sure the class will be the same size on both platforms
You can't do that in a portable way. Write functions that pack/unpack data into byte arrays with known byte ordering for binary data storage and exchange between different platforms. Alternatively, use text-based data exchange formats.
Reply With Quote
  #4  
Old 8th September 2007, 11:08 PM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
OK, thanks a lot, guys.
Reply With Quote
Reply

Tags
class, size

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the size of a custome class type in java cola Programming & Packaging 1 17th February 2009 11:05 PM
Problem with virtual size and real size of monitor djey Hardware & Laptops 1 17th October 2006 08:55 AM


Current GMT-time: 08:48 (Saturday, 18-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat