-1

I asked AI (Claude), and it told me it is UB. Is that real?

#include <stdio.h>
struct BitFieldStruct {
    unsigned int a : 5;
    unsigned int : 3;  // 3位填充位
    unsigned int b : 6;
    unsigned int c : 10;
};

int main() {
    struct BitFieldStruct s1 = {10, 20, 300};
    struct BitFieldStruct s2;

    printf("s1.a = %d, s1.b = %d, s1.c = %d\n", s1.a, s1.b, s1.c);

    s2 = s1; // UB

    printf("s2.a = %d, s2.b = %d, s2.c = %d\n", s2.a, s2.b, s2.c);
    // may not same as s1

    return 0;
}

I searched the internet and was still confused.

Claude told me that it is according C99(6.7.2.1.14). I think Claude read it wrong. But English is not my native language. I am not sure I read it right.

When a value is stored in an object of structure or union type, including a case as part of a value of a union type, the bytes of the object representation shall be written in order with the values of the members given in increasing bit-field order, with implementation-defined behavior in the case of an overlap. The order for byte components is implementation-defined. The values of members that are unions are stored according to the union definition.

When a value is stored in a bit-field of an object of a structure or union type, the value shall be represented with an integer type and the encoding defined by ANSI/IEEE Std 754-1985. The value shall be stored in the minimum number of bits needed to represent the value and the integral part of the value, if any, shall occupy the least significant bit position(s).

When a value in a bit-field is read, its value shall be represented with an integer type and the encoding defined by ANSI/IEEE Std 754-1985. If insufficient memory remains, whether or not the entire bit-field can be read, the value shall be read as an unsigned integer and the value of the entire bit-field becomes an indeterminate value.

The behavior is undefined in the following cases:

**When storage is accessed by an lvalue that does not designate an object of a structure or union type compatible with the effective type of the lvalue.

When a value is stored into or read from a bit-field of an object in a different order than the order of allocation of the components of the value having an integer type.**

6
  • 2
    What reasons did Claude give you and which parts of which standard did it quote? Did you verify those quotes?
    – Yunnosch
    2 days ago
  • 2
    Why do you expect truthful answers from a confabulation engine? 2 days ago
  • 3
    I think Claude needs to read the standard again. 2 days ago
  • Claude says it is C99(6.7.2.1.14). 2 days ago
  • 2
    And did you check this statement? -- General rule: Never trust statements of LLVMs. They hallucinate and have no understanding. They are not intelligent, but can put words well together. Unfortunately we humans tend to assume that well-worded statements tell the truth. They do not. 2 days ago

1 Answer 1

2

Assigning structure to compatible structure is well defined. It doesn't matter if it has bit fields or not. Quote from C99 draft N1256:

6.5.16.1 Simple assignment

Constraints

  1. One of the following shall hold:96)

    • the left operand has qualified or unqualified arithmetic type and the right has arithmetic type;
    • the left operand has a qualified or unqualified version of a structure or union type compatible with the type of the right;

C99 §6.7.2.1.14 has nothing to do with structures. It's about unions:

The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa.

Your quoted text is also complete nonsense. For example:

When a value is stored in a bit-field of an object of a structure or union type, the value shall be represented with an integer type and the encoding defined by ANSI/IEEE Std 754-1985.

IEEE-1985 is a floating-point standard, which has nothing to do with integers.

2
  • Also C17 6.2.7 "Two types have compatible type if their types are the same." That applies here. Then the same chapter goes on about various intricate rules for when two structs with different types are considered compatible, but that doesn't matter here.
    – Lundin
    yesterday
  • And that last quote is not in the C99 standard or any other C standard. The actual part of the C99 standard 6.2.6.1 §6 goes (emphasis mine) "When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values." The AI is not quoting, it is hallucinating together something that looks like a quote.
    – Lundin
    yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.