Search This Blog

2009-08-23

C++ fails on my logic...

union u1 {
char[0];
long[0];
}

union u2 {
char[1];
}

union u3 {
char;
long;
}

union u4 {
char;
long[0];
}

union u5 {
char[1];
long[0];
}

// i can undesrtand these...
sizeof(u1); // 0
sizeof(u2); // 1
sizeof(u3); // 8

// but these do not make sense to me...
sizeof(u4); // 8... not 1?
sizeof(u5); // 8... not 1?

// found this when trying to code this:

template <unsigned int unitSize = 1, unsigned int length = 1>
union Bytes {
char c[length * (unitSize / sizeof(char))];
bool b[length * (unitSize / sizeof(bool))];
short s[length * (unitSize / sizeof(short))];
int i[length * (unitSize / sizeof(int))];
long l[length * (unitSize / sizeof(long))];
float f[length * (unitSize / sizeof(float))];
double d[length * (unitSize / sizeof(double))];
long double ld[length * (unitSize / sizeof(long double))];
};

No comments:

Post a Comment