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
Solved

size of an object

Posted on 2000-02-23
6
207 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 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 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.

789 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