Solved

size of an object

Posted on 2000-02-23
6
181 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 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now