Archived

This topic is now archived and is closed to further replies.

executor_2k2

sizeof() classes

Recommended Posts

So then reading in using fread an array of floats into an array of some vector class, could potentially store the vectors wrong?

ex. fread(pObject->pVerts, 1, pPreviousChunk->length - pPreviousChunk->bytesRead, m_FilePointer); // pVerts is an array of vector classes and the data being read is and array of floats (grouped into 3 for a vector)

[edited by - executor_2k2 on March 7, 2003 4:27:50 AM]

[edited by - executor_2k2 on March 7, 2003 5:20:27 AM]

Share this post


Link to post
Share on other sites
Yes and no, depending on your class and on how the data was stored. As SiCrane said, vtable information is included in the size, so if you have virtual functions, don''t think about reading the entire class at once, read element by element, or if you group the data in a second structure you can read that structure instead.

The eventual space between members will only cause trouble if the program that wrote the file is using different padding that the program reading the file (your program). If your program also wrote the file (like a save and load feature in a game), you can be pretty sure the padding is the same, so you don''t have to worry about that.

Share this post


Link to post
Share on other sites
quote:
Original post by executor_2k2
So then reading in using fread an array of floats into an array of some vector class, could potentially store the vectors wrong?

ex. fread(pObject->pVerts, 1, pPreviousChunk->length - pPreviousChunk->bytesRead, m_FilePointer); // pVerts is a vector class and the data being read is and array of floats (grouped into 3 for a vector)



Yes, you must not use fread for that, but write a proper serialisation routine. fread works only for POD types (i.e. C-style structs).

See this thread.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Could i just use this...


      
float* pTempVerts = new float[(pPreviousChunk->length - pPreviousChunk->bytesRead)/sizeof(float)];
pPreviousChunk->bytesRead += fread(pTempVerts, 1, pPreviousChunk->length - pPreviousChunk->bytesRead, m_FilePointer);
float* count = pTempVerts;
for(int i = 0; i < pObject->numOfVerts; i++)
{
pObject->pVerts[i] = CVector3(*pTempVerts, *(pTempVerts+1), *(pTempVerts+2));
count+=3;
}


[edited by - executor_2k2 on March 7, 2003 5:49:40 AM]

[edited by - executor_2k2 on March 7, 2003 5:52:11 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
or if you group the data in a second structure you can read that structure instead.




So structures dont have padding problems? I thought the variables werent necessarily packed next to each other. For instance if they number of bytes of the summed varibles was 20 bytes it would make the structure size 24. x86 likes strips divisible by 8 or something.

Share this post


Link to post
Share on other sites
Sorry, I was a bit unclear on that point. Structures are the same as classes, and the have the same "problem" with padding. If you have virtual functions, you will have problems reading the whole class the usual way. If you group the members into a second structure, you have separated the members and the vtable, so you don''t have to read the file member by member. You still have the "problem" with padding in the structure, but you have eliminated the problem with the vtable.

Personally, I think grouping the members into a second structure only for the purpose of reading them in one go is a bit ugly compared to reading the file member by member. But you can try it if you want to.

Share this post


Link to post
Share on other sites