[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Unions / unsigned char[]

Kenneth Brody

9/8/2011 4:28:00 PM

Okay, I can't find C&V, but as I recall, accessing a union via anything
other than the one last written to is UB. For example:

my_union.my_int = i;
f = my_union.my_float;

However, I believe that you can always access "legal" memory via an unsigned
char pointer.

So, what if the union contains an unsigned char array, as in:

union foobar {
unsigned char raw[MY_UNION_SIZE];
some_type foo;
other_type bar;
};

Are all values in the raw[] array guaranteed to be readable? (It "works" on
all platforms I have tried, but is that guaranteed? I'm pretty sure it is,
but I can't find C&V.)

Thanks.

--
Kenneth Brody
4 Answers

James Kuyper

9/8/2011 4:39:00 PM

0

On 09/08/2011 12:28 PM, Kenneth Brody wrote:
> Okay, I can't find C&V, but as I recall, accessing a union via anything
> other than the one last written to is UB. For example:

Footnote 82 indicates that

> If the member used to access the contents of a union object is not the same as the member last used to
> store a value in the object, the appropriate part of the object representation of the value is reinterpreted
> as an object representation in the new type as described in 6.2.6 (a process sometimes called "type
> punning"). This might be a trap representation.

However, there doesn't seem to be any normative text to support this
footnote. At least, I didn't find the term "reinterpret" anywhere else
in the document.

> my_union.my_int = i;
> f = my_union.my_float;
>
> However, I believe that you can always access "legal" memory via an unsigned
> char pointer.
>
> So, what if the union contains an unsigned char array, as in:
>
> union foobar {
> unsigned char raw[MY_UNION_SIZE];
> some_type foo;
> other_type bar;
> };
>
> Are all values in the raw[] array guaranteed to be readable? (It "works" on
> all platforms I have tried, but is that guaranteed? I'm pretty sure it is,
> but I can't find C&V.)

The only potential problem would be if they contained a trap
representation, but unsigned char is not allowed to have trap
representations.

John Temples

9/9/2011 7:09:00 PM

0

On 2011-09-08, Kenneth Brody <kenbrody@spamcop.net> wrote:
> Okay, I can't find C&V, but as I recall, accessing a union via anything
> other than the one last written to is UB.

It was ID (not UB) in C90, but that was removed in C99.

--
John W. Temples, III

ram

9/10/2011 3:54:00 AM

0

John Temples <usenet@xargs-spam.com> writes:
>On 2011-09-08, Kenneth Brody <kenbrody@spamcop.net> wrote:
>>Okay, I can't find C&V, but as I recall, accessing a union via anything
>>other than the one last written to is UB.
>It was ID (not UB) in C90, but that was removed in C99.

Annex J, J.1 »unspecified behavior«, p1, of ISO/IEC
9899:1999 (E) still contains:

»The following are unspecified: (...)

-- The value of a union member other than the last one
stored into (6.2.6.1).«

Jens Gustedt

9/11/2011 8:25:00 AM

0

Am 09/10/2011 05:53 AM, schrieb Stefan Ram:
> John Temples <usenet@xargs-spam.com> writes:
>> On 2011-09-08, Kenneth Brody <kenbrody@spamcop.net> wrote:
>>> Okay, I can't find C&V, but as I recall, accessing a union via anything
>>> other than the one last written to is UB.
>> It was ID (not UB) in C90, but that was removed in C99.
>
> Annex J, J.1 »unspecified behavior«, p1, of ISO/IEC
> 9899:1999 (E) still contains:
>
> »The following are unspecified: (...)
>
> -- The value of a union member other than the last one
> stored into (6.2.6.1).«

This annex is not normative and just refers to the reasons why this
might be UB with the paragraph number.

I think the common understanding is that reading a member to which it
was not written is UB *if* the value is a trap representation for that
member. Since unsigned char may not have a trap representation it is
save to access that member.

Jens