Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
the size of a struct containing bitfields

I have a struct as follows.

struct A {
  uint32_t a : 1;
  uint32_t b : 1;
};

Is struct A guaranteed by the standard to have size 4? Is it possible that a compiler instead uses only one byte for struct A since it uses exactly two bits?

struct B {
  uint32_t a : 1;
  uint32_t b : 1;
  uint32_t c : 30;
} b;

If I would like to set the bitfields in b to zero, since sizeof(struct B) = 4, it looks good to me to do so by *(uint32_t*)&b = 0. But I saw a lot of discussions on SO arguing that such a pointer cast is a bad practice. I wonder what makes *(uint32_t*)&b = 0 bad here and what I should do (I know I can use memset to reset the bitfields but I am interested in other portable ways).

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The first question, you can add an alignment, but this will affect the execution efficiency:

#pragma pack(push)
#pragma pack(1)
struct A {
    uint32_t a : 1;
    uint32_t b : 1;
};
#pragma pack(pop)

The second question, you can define it as union

union B {
    struct {
        uint32_t a : 1;
        uint32_t b : 1;
        uint32_t c : 30;
    };
    uint32_t u;
} b;

// set the bitfields in b to zero
b.u = 0;
over 4 years ago · Santiago Trujillo Report

0

Is struct A guaranteed by the standard to have size 4?

No. It might be size 4, might not.

Is it possible that a compiler instead uses only one byte for struct A since it uses exactly two bits?

Yes, it is possible.

I wonder what makes *(uint32_t*)&b = 0 bad here ...

  • Assignment may be insufficient to zero out b.
  • Rare: the optional type uint32_t may not exist.
  • Code risks aliasing issues.

... and what I should do

To zero out b, assign a struct B that is zero. Research compound literal. Notice this code below applies to various types, not just struct with bit-fields.

b = (struct B){0};

Or maybe, if you desire a non-zero value:

b = (struct B){.a = 0, .c = 42, .b = 1};

Bit fields are tricky. Use signed int, unsigned int and _Bool and not uint32_t for maximum portability.

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. It is implementation-defined whether atomic types are permitted. C17dr § 6.7.2.1 5

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!