Question: sizeof() returns strange values for a struct in C. Why?
Answer: Yeah, it does return strange values. Not only this, the values may differ from one platform to another. The reason is that the compiler adds external padding (in some cases internal padding as well) to preserve alignment. This padding varies from one system to another. On your system (or for your compiler), it may be 1 byte for 'char', 2 bytes for 'short', 4 bytes for 'int', ... and so on. On some other system it may be different. So, the bottom line is that we should avoid using sizeof() value for a struct in our code for any computation simply because it's highly unreliable :-)
Sunday, June 1, 2008
sizeof() returns strange values for a struct in C. Why?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment