struct

Started by
2 comments, last by browny 21 years, 11 months ago
Lemme get straight to the point. I have a "struct" something like this struct TGAHEADER{ // blah // blah }; sorry, i dont have the exact code with me right now since i''m writing this from a cyber cafe. The thing is, when i count the size of TGAHEADER manually i get 18 bytes, but when i use sizeof(TGAHEADER) it gives 20 bytes !! ( I''m using MSVC++ 6.0 for your information ). Well.. this is aint a new thing for me though since i ran into a similar problem few months back in GCC compiler. God knows how many times i banged my head against the monitor until i came across this magical thing called "__attribute((packed))". Is there any such "__attribute((packed))" like thing in VC++. I know this is all about "structure data alignment" which could be fixed by changing the "Data Alignment" from Project->Settings->C++->Code Generation. But that''s gonna effect my whole project right ? Obviously i dont want to let ONLY ONE "struct" to effect my whole project. The funny things is, even if i change the data alignment, the compiler says: **warning: the data alignment is not consistent with the pre-compiled header so ignoring the /Z flag DUH ! WHAT DO I DO KNOW ? Anyone with better idea ?
Z
Advertisement
#pragma pack(push)#pragma pack(1)#define BYTE_ALIGNstruct YADAYADA{  data...  data...}BYTE_ALIGN;#pragma pack(pop)#undef BYTE_ALIGN   



[edited by - Rickmeister on May 6, 2002 11:37:53 AM]
This happens because your compiler is optimizing the struct so that it fits on a 32-bit alignment. Different compilers might not do this, but as long as you use sizeof, you should have no major problem.

-Doug

"Sometimes I think the surest sign that intelligent exists elsewhere in the universe is that none of it has tried to contact us" -Calvin
"Sometimes I think the surest sign that intelligent exists elsewhere in the universe is that none of it has tried to contact us" -Calvin
quote:Original post by Rickmeister
#pragma pack(push)#pragma pack(1) 


You can just say
#pragma pack(push, 1) 

This topic is closed to new replies.

Advertisement