I'd like to understand the trap representation concept in details. The definition is pretty clear Section 3.19.4
:
an object representation that need not represent a value of the object type
Okay, I'd like to try it by some examples.
struct test_t{
uint64_t member;
};
struct test_t *test_ptr = malloc(sizeof(uint32_t));
struct test_t = *test_ptr; //1
I don't think that //1
causes UB here, because Section 6.2.6.1
:
If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined.
but
The value of a structure or union object is never a trap representation, even though the value of a member of the structure or union object may be a trap representation.
I think UB will be caused by something like
printf("Test.member = %lu\n", test.member);
But I'm not sure how to prove that in such a case the representation of member
is a trap.
//1
. – Some programmer dude*test_ptr
is anlvalue
and the value of a structure is never a trap. Why is this UB? Could you give some reference? – Some Nameuint64_t member; ... printf("Test.member = %lu\n", test.member);
unnecessarily adds confusion as"lu"
is not certainly the matching print specifier foruint64_t
, thus making theprintf("Test.member = %lu\n", test.member);
UB right there. Better to code with"%" PRIu64
. – chux - Reinstate Monica