H
hammer1234
Let me guess, all your programming teachers took points off for not
commenting like this?
No actually if you read any of the previous postings. MISRA C:2004
states that c90 must be used for compliant code. Therefore this is the
only way to comment. There is a specific rule that bans the use of //.
What you're doing is highly implementation dependent anyway so I'm not
going to bother to nitpick if unsigned int doesn't have the same
size/alignment as float or if unsigned int has any trap representations
but I will say that the only correct way to do the above is to
basically use:
float f = whatever;
unsigned int in;
assert(sizeof(f) == sizeof(in));
memcpy(&in, &f, sizeof(f));
or the following:
float f = whatever;
unsigned char *cp = (unsigned char*)&f;
for (size_t i = 0; i != sizeof(f); ++i) {
do_something_with(cp);
}
which is the only way guaranteed not to do anything undefined by the
standard but since you need to know how the bits are laid out anyway
and it's really annoying messing with the float bits as unsigned
char[sizeof(float)], I'd probably stick with the first one (but with
the unsigned int typedeffed as floatbits_t or something).
I am not worried about doing anything undefined by the standard as I am
creating test cases with intentional violations. Using the bit
representations of floats is a straight violations of MISRA C 2004 rule
12.12.