问题
I read here that bit fields are not portable. Does that mean that the code below that defines bit fields (code taken from here) could not compile on certain machines?
If so, then why?
#include <stdio.h>
#include <string.h>
/* define simple structure */
struct
{
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
/* define a structure with bit fields */
struct
{
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( )
{
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
回答1:
Bit fields are portable, in the sense that they are a part of the C language as specified in the standard (C11 section 6.7.2.1). Any compiler that fails to recognise code that uses bitfields is not standard-compliant. There's also nothing really questionable about your example, since all it does is have bitfields present.
What they probably mean is that the fields themselves may be packed unpredictably in location and order (allowed by the standard, previous ref. paragraph 11). This means that a struct with e.g. four bitfields of size 4, 12, 13 and 3 does not necessarily take up 32 bits and they won't necessarily be placed within the struct in that order; the compiler can place them where it likes. This means that the struct cannot be treated as an actual component-wise representation of an underlying binary object.
In contrast, bitmasks applied manually to integers exist exactly where you put them. If you define masks that mask out the first 4 bits, second 12 bits, etc. of an unsigned integer, the "fields" will actually apply to the bits, in order and in position (assuming you know the endianness, anyway). This makes the representation compiler-independent.
i.e. they are portable, but what they do may not necessarily be exactly what a person actually wanting to manipulate individual bits may need.
回答2:
Bit fields are standard language feature. They will compile in all C compilers. They are portable in that sense of the term. Your code is well-formed and will compile in all C compilers as well.
Statements like "bit-fields are not portable" usually mean that the physical layout of bit-fields in memory might differ from implementation to implementation (i.e. from one compiler to another). You might get different output from your program, if compiled on different implementations. But the the difference in program's behavior can only occur if (and when) your code depends on the memory layout of objects with bit-fields (e.g. if you measure their size, as you do in your program).
In other words, to say that "bit-fields are not portable" is pretty much the same thing as to say that type int
is "not portable" just because it can have different size on different platforms or use different endianness in its internal representation.
回答3:
An int
is required to be at least 16 bits in C. Therefore, if you wish to use bit-fields in maximally portable code, you cannot have a bit-field that occupies more than 16 bits.
C.11 §6.7.2.1¶5:
A bit-field shall have a type that is a qualified or unqualified version of
_Bool
,signed int
,unsigned int
, or some other implementation-defined type.
C.11 §5.4.2.1¶1:
— maximum value for an object of type
int
INT_MAX +32767 //
215 − 1
— maximum value for an object of typeunsigned int
UINT_MAX 65535 //
216 − 1
来源:https://stackoverflow.com/questions/25345691/bit-fields-portability