Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda