New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrectly initialized bit fields in structure #1262
Comments
For which standard? Anyway OW compiler process your structure following way
Therefore size is 4-bytes. What is done by gcc is completely different then C standard. |
If you want bits be packed by OW as by gcc then use same type as struct key { |
Thank you, I modified the example for both fields as #include <stdio.h>
typedef struct key key;
struct key {
struct {
unsigned short type : 2;
unsigned short row : 12;
};
};
#define MAX_ROW (4*60*60/8)
typedef enum {
STEP = 0,
SMOOTH = 1,
LINEAR = 2,
RAMP = 3,
} simp_type;
static char *TYPE_STR[] = {
"STEP", "SMOOTH", "LINEAR", "RAMP",
};
key keys[] = {
{ .row = MAX_ROW, .type = RAMP },
};
int main() {
printf("sizeof(key) = %i\r\n", sizeof(key));
printf("keys[0] = { .row = %3i, .type = %s }\r\n",
keys[0].row, TYPE_STR[keys[0].type]);
printf("%04x\r\n", *(int *)&keys[0]);
/* set values again */
keys[0].row = MAX_ROW;
keys[0].type = RAMP;
printf("keys[0] = { .row = %3i, .type = %s }\r\n",
keys[0].row, TYPE_STR[keys[0].type]);
printf("%04x", *(int *)&keys[0]);
} Now prints out:
But
|
It looks like bug in static initialization of bit fields. |
Creating a struct that uses packed bit fields does not match the C specification with differently sized types. For example:
when built with
Version 2.0 beta Mar 1 2024 01:51:55 (64-bit)
prints the output:However building with
gcc -m32 keys.c -o keys
gives the following output:The text was updated successfully, but these errors were encountered: