sizeof() classes
Does sizeof(SomeClass) return the sumation of the class''s variables? Could there possibly be some space between the variables?
quote:Original post by executor_2k2Yes. To make it more efficient.
Could there possibly be some space between the variables?
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]
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]
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.
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.
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 ]
whoops...
pVerts is an pointer to an array of my vectors(which are a class). How does this change things?
pVerts is an pointer to an array of my vectors(which are a class). How does this change things?
Could i just use this...
[edited by - executor_2k2 on March 7, 2003 5:49:40 AM]
[edited by - executor_2k2 on March 7, 2003 5:52:11 AM]
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]
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement