Solved

size of an object

Posted on 2000-02-23
6
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
JMu earned 200 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

734 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