Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the cppreference page on...
Answer
#9: Post edited
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
Also consider that the object pointed to by dest is a `union u` (whos size is the maximum size of all it's members) not a `struct t`, even though it may be treated as a `struct t` in some cases.- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is the maximum size of all its members) not a `struct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#8: Post edited
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
Also consider that the object pointed to by dest is a `union u` (whos size is the maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is the maximum size of all it's members) not a `struct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#7: Post edited
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
Also consider that the object pointed to by dest is a `union u` (whos size is thr maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is the maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#6: Post edited
Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but the value of the extra bytes is unspecified, since C99. From the [cppreference page on C unions][1]:- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is thr maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed since C99, but the value of the extra bytes is unspecified. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is thr maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#5: Post edited
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but the value of the extra bytes is unspecified, since C99. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but the value of the extra bytes is unspecified, since C99. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- Also note that the implementation must not assume the destination of the memcpy is smaller than a `union u`. Consider the definition of memcpy:
- > Copies count characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.
- Also consider that the object pointed to by dest is a `union u` (whos size is thr maximum size of all it's members) not a `sruct t`, even though it may be treated as a `struct t` in some cases.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#4: Post edited
Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but implementation defined, since C99. From the [cppreference page on C unions][1]:- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- [1]: https://en.cppreference.com/w/c/language/union
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but the value of the extra bytes is unspecified, since C99. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- Since the value of the bytes is unspecified, the implementation may use any value in any instace, and does not need to document the behavior. That said, [GCC *does* document this behavior][2] (and clang tries to follow GCC's implementation defined behavior on Linux):
- > The relevant bytes of the representation of the [union] object are treated as an object of the type used for the access.
- [1]: https://en.cppreference.com/w/c/language/union
- [2]: https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
#3: Post edited
Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but implementation defined since C99. From the [cppreference page on C unions][1]:- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- [1]: https://en.cppreference.com/w/c/language/union
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but implementation defined, since C99. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- [1]: https://en.cppreference.com/w/c/language/union
#2: Post edited
This behavior is explicityly allowed, but implementation defined since C99. From the [cppreference page on C unions][1]:- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- [1]: https://en.cppreference.com/w/c/language/union
- Reading from a union member who's size is larger than that of the last written member is explicitly allowed, but implementation defined since C99. From the [cppreference page on C unions][1]:
- > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way.
- [1]: https://en.cppreference.com/w/c/language/union
#1: Initial revision
This behavior is explicityly allowed, but implementation defined since C99. From the [cppreference page on C unions][1]: > If the size of the new type is larger than the size of the last-written type, the contents of the excess bytes are unspecified (and may be a trap representation). Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way. [1]: https://en.cppreference.com/w/c/language/union