C++ struct has no default memberwise comparison for operator==?!

Started by
30 comments, last by Xentropy 16 years, 10 months ago
Quote:Original post by Saucetenuto
For performance reasons, the compiler requires that each field of a struct be allocated at a memory address that's a multiple of 4 (on a 32-bit architecture; different systems have different requirements here). If the whole struct is stored at address 0, then a is stored at 0, b is stored at 4, and c is stored at 8.

Actually most compilers (and MSVC for sure) require that struct members be aligned on a boundary that is a multiple of their size, unless you force a particular alignment.

So

struct A
{
int a;
char b;
char c;
int d;
};

would be 12 bytes similar to your class above, c being stored at address 5.
Advertisement
Quote:Original post by Antheus
#1 rule of serialization: Do not rely on data sizes or structure layouts.


In general I agree with you, but as a 20-year-old BBS game that hasn't been updated in years, I find it unlikely the layouts of the files will change. Still, you have a great point and I'm keeping that in mind and considering alterantive (more portable, less loudly ticking ;) ) ways of doing what I'm doing.

[Edited by - Xentropy on June 14, 2007 5:14:35 AM]

This topic is closed to new replies.

Advertisement