Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
struct {
    unsigned int a: 8;
} bits = {255};


int main(void) {
 
    printf(LANG ", unsigned 8-bit field promotes to %s.\n",
        (bits.a << 24) < 0 ? "signed" : "unsigned");
}

The type of the expression (bits.a << 24) is compiler dependent and may be either signed or unsigned depending on their interpretation of the standard.
The first interpretation is that when this value is used as an rvalue (e.g., lvalue = rvalue;) (wink) the type is "unsigned int" as declared.  An unsigned int cannot be represented as an int so integer promotions require that this be an unsigned int, and hence "unsigned".

...

Code Block
struct {
    unsigned long long size: 24;
} ull;

void* AllocBlocks(ull cBlocks) {
  if (cBlocks == 0) return NULL; 
  unsigned long long alloc = cBlocks.size * 16;
  return (alloc < UINT_MAX) 
    ? malloc(cBlocks * 16)
    : NULL;
}

...

Code Block
void* AllocBlocks(size_t cBlocks) {
  if (cBlocks == 0) return NULL; 
  unsigned long long alloc = 
           (unsigned long long)cBlocks.size * 16;
  return (alloc < UINT_MAX) 
    ? malloc(cBlocks * 16)
    : NULL;
}

The assumption concerning the relationship of unsigned long long int and size_t must be document in the header for each file that depends upon this assumption for correct execution.

References