Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

size of an object

Posted on 2000-02-23
6
Medium Priority
?
234 Views
Last Modified: 2010-04-02
I'm trying to write an object to a file.
Here is the code being used now.

int eCount = sizeof(Session);
TFileStream *eStream = new TFileStream  ("aTest.fse" , fmCreate);
 
eStream->Write(this, eCount);
delete(eStream);

The problem is that the sizeof is not giving the right size so it doesnt write the correct amount to the file.  This method is actually inside the class. So you call a method in the class and it writes the object to the disk.  Any ideas how to correct the size problem?

0
Comment
Question by:andrew161
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
JMu earned 800 total points
ID: 2552214
Hopefully the object does not contain anything other than standard datatypes and no pointers.

Compiler propably aligns all data members to size of int.

int
char
int

This comes actually

int
char
char[3] padding
int

Enter
#pragma pack(1)
before declaration of class/structure. This tells to compiler that it should pack structures.

JMu
0
 

Author Comment

by:andrew161
ID: 2552323
well. Alittle more regarding the object.
It is actually derived from another object which has a couple of virtual methods.  

I dont believe there are any points to outside the structure.

Will that still work?

0
 
LVL 22

Expert Comment

by:nietod
ID: 2552368
"#pragma pack(1)" is non-standard.  Not all compilers will support it.  (It will work on the ones that do support it, like VC.)  What compiler are you using?

>> It is actually derived from another object
>> which has a couple of virtual methods.
It is not save to do this.  This object is not POD (plain old data) because it contains virtual functions.  This means it may contain data that the compiler uses to help make the object "function correctly"  For example, it may contain a virtual function table pointer (vptr).  While it would be okay to writ this hidden data out, it would be a waste.  More importantly it would be a dangerious mistake to ever read the data back in.  

If the class is not POD, you cannot treat it as a simple aggegate and read and write it as a single chunk.  Instead you must read and write each data member stored in the class individually.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:JMu
ID: 2552371
I tested this. I wrote two simple classes (one derved from another). Both had one char member.

sizeof was 2. OK.

I added a virtual function to base class. Sizeof was 12.

struct alignment was 8.

I added #pragma pack(1) before classes. Sizeof was 6.

Pointer to virtual function table is 4 bytes + 2 chars = 6.

I added another virtual function. No effect on size.

So, don't use virtual functions or write every member one by one.

JMu
0
 
LVL 1

Expert Comment

by:JMu
ID: 2552378
BTW. It's not portable to write eg. integer to a file. Reading processor may use other byte ordering than the writing one.

JMu
0
 
LVL 22

Expert Comment

by:nietod
ID: 2552498
>> So, don't use virtual functions
You need to make sure that no data members have virtual functions too.  That is probably sufficient, but technically it is not.  You are only guaranteed to be save if the class/struct is POD.  

>> It's not portable to write eg. integer to a file
The code that read/writes the the class will be portable.  Only the data file might not be portable.  (usually "portable" refers to source code.)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question