Solved

size of an object

Posted on 2000-02-23
6
201 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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