Re: int a = a

Liste des GroupesRevenir à cl c 
Sujet : Re: int a = a
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 20. Mar 2025, 11:20:05
Autres entêtes
Organisation : None to speak of
Message-ID : <87cyect356.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Gnus/5.13 (Gnus v5.13)
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
[how to indicate a variable not being used is okay]
[some quoted text rearranged]
>
Unless I'm missing something, `(void)x` also has undefined beahvior
if x is uninitialized,
>
Right.  Using (void)&x is better.

I'm not convinced -- and it's far less idiomatic.  I don't think
I've ever seen (void)&x in code, and if I did I'd wonder what the
author's intent was.

(void)x is a common idiom for hinting to the compiler that it
doesn't need to complain about x being unused.  (void)&x doesn't
tell the compiler that the *value* of x is used.  I'm not sure how
much difference that makes.

Even with (void)x and/or (void)&x, a compiler *could* still warn
about x being unused, or about the programmer's use of an ugly font.

though it's very likely to do nothing in practice.
>
Unless x is volatile qualified, in which there must be an access
to x in the generated code.
>
The behavior [of int a = a;] is undefined.  In C11 and later
(N1570 6.3.2.1p2):
>
    Except when [...] an lvalue that does not have array type is
    converted to the value stored in the designated object (and is
    no longer an lvalue); this is called lvalue conversion.
    [...]
    If the lvalue designates an object of automatic storage
    duration that could have been declared with the register
    storage class (never had its address taken), and that object
    is uninitialized (not declared with an initializer and no
    assignment to it has been performed prior to use), the
    behavior is undefined.
>
Long digression follows.
>
The "could have been declared with the register storage class"
seems quite odd.  And in fact it is quite odd.
>
I don't have the same reaction.  The point of this phrase is that
undefined behavior occurs only for variables that don't have
their address taken.  The phrase used describes that nicely.
Any questions related to "registerness" can be ignored, because
'register' in C really has nothing to do with hardware registers,
despite the name.

DR 338 is explicitly motivated by an IA-64 feature that applies only to
CPU registers.  An object whose address is taken can't be stored (only)
in a register, so it can't have a NaT representation.

The phrase used is "could have been declared with register storage class
(never had its address taken)".  Surely "never had its address taken"
would have been clear enough if CPU registers weren't a big part of the
motivation.

[SNIP]

https://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_338.htm
 
So the "could have been declared with the register storage class"
wording was added in C11 specifically to cater to the IA64.  This
change would have been superfluous in C90, where the behavior was
undefined anyway, but is a semantically significant change between
C99 and C11.  (If some future CPU has something like NaT that can
be stored in memory, the wording might need to be updated yet again.)
>
My takeaway is that if it requires this much research to determine
whether accessing the value of an uninitialized object has undefined
behavior (in which circumstances and which edition of the standard),
I'll just avoid doing so altogether.  I'll initialize objects
when they're defined whenever practical.  If it's not practical
for some reason, I won't initialize it with some dummy value; I'll
leave it uninitialized so the compiler has a chance to warn me if
I accidentally use it before assigning a value to it.
>
I think you are overthinking the question.  In cases where it's
important to give an initial value to a variable, and can be done
so at the point of its declaration, use an initializer;  otherwise
don't.

My overthinking led me to essentially the same conclusion, so I don't
see the problem.  And I also found it to be an interesting exploration
of how certain aspects of the C standard have evolved over time.

        We don't have to read several different C standards, or
even only one, to reach that conclusion.

No, but we do have to read one or more C standards to counter an
argument that `int a = a;` is well defined.

                                          If someone wants to know
exactly which border cases are safe and which cases are not, then
reading the relevant version(s) of the C standard is needed, but
in most situations it isn't.  It's important for the C standard to
be precise about what it prescribes, but as far as initialization
goes it's easy to write code that doesn't need that level of
detail.  Compiler writers need to know such things;  in the
particular case of when and where to initialize, most developers
don't.

Most developers don't read this newsgroup.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
18 Mar 25 * Bart's Language57bart
18 Mar 25 `* Re: Bart's Language56Waldek Hebisch
18 Mar 25  `* Re: Bart's Language55bart
18 Mar 25   `* Re: Bart's Language54Waldek Hebisch
18 Mar 25    +* Re: Bart's Language46bart
18 Mar 25    i+* Re: Bart's Language36David Brown
18 Mar 25    ii`* int a = a (Was: Bart's Language)35Kenny McCormack
18 Mar 25    ii +* Re: int a = a (Was: Bart's Language)25Janis Papanagnou
18 Mar 25    ii i+- Re: int a = a (Was: Bart's Language)1Kaz Kylheku
19 Mar 25    ii i`* Re: int a = a (Was: Bart's Language)23David Brown
19 Mar 25    ii i +- Re: int a = a (Was: Bart's Language)1Kaz Kylheku
19 Mar 25    ii i +* Re: int a = a14Keith Thompson
20 Mar 25    ii i i+* Re: int a = a12Tim Rentsch
20 Mar 25    ii i ii`* Re: int a = a11Keith Thompson
20 Mar 25    ii i ii +* Re: int a = a8David Brown
20 Mar 25    ii i ii i`* Re: int a = a7Keith Thompson
21 Mar 25    ii i ii i `* Re: int a = a6David Brown
21 Mar 25    ii i ii i  `* Re: int a = a5Keith Thompson
21 Mar 25    ii i ii i   +- Re: int a = a1David Brown
22 Mar 25    ii i ii i   `* Re: int a = a3Tim Rentsch
22 Mar 25    ii i ii i    `* Re: int a = a2Keith Thompson
28 Apr 25    ii i ii i     `- Re: int a = a1Tim Rentsch
29 Apr 25    ii i ii `* Re: int a = a2Tim Rentsch
29 Apr 25    ii i ii  `- Re: int a = a1Keith Thompson
20 Mar 25    ii i i`- Re: int a = a1David Brown
19 Mar 25    ii i +* Re: int a = a (Was: Bart's Language)5Chris M. Thomasson
20 Mar 25    ii i i`* Re: int a = a (Was: Bart's Language)4David Brown
20 Mar 25    ii i i `* Re: int a = a (Was: Bart's Language)3bart
20 Mar 25    ii i i  `* Re: int a = a (Was: Bart's Language)2David Brown
20 Mar 25    ii i i   `- Re: int a = a (Was: Bart's Language)1wij
20 Mar 25    ii i `* Re: int a = a (Was: Bart's Language)2Tim Rentsch
20 Mar 25    ii i  `- Re: int a = a (Was: Bart's Language)1David Brown
18 Mar 25    ii +* Re: int a = a (Was: Bart's Language)3David Brown
18 Mar 25    ii i`* Re: int a = a (Was: Bart's Language)2Janis Papanagnou
19 Mar 25    ii i `- Re: int a = a (Was: Bart's Language)1David Brown
19 Mar 25    ii `* Re: int a = a (Was: Bart's Language)6Tim Rentsch
19 Mar 25    ii  +* Re: int a = a2Keith Thompson
27 Apr 25    ii  i`- Re: int a = a1Tim Rentsch
19 Mar 25    ii  +- Re: int a = a (Was: Bart's Language)1David Brown
19 Mar 25    ii  `* Re: int a = a (Was: Bart's Language)2Rosario19
20 Mar 25    ii   `- Re: int a = a (Was: Bart's Language)1Tim Rentsch
20 Mar 25    i`* Re: Bart's Language9Waldek Hebisch
21 Mar 25    i `* Re: Bart's Language8Keith Thompson
22 Mar 25    i  +* Re: Bart's Language5Waldek Hebisch
22 Mar 25    i  i`* Re: Bart's Language4James Kuyper
22 Mar 25    i  i +* Re: Bart's Language2Waldek Hebisch
23 Mar 25    i  i i`- Re: Bart's Language1James Kuyper
23 Mar 25    i  i `- By definition... (Was: Bart's Language)1Kenny McCormack
27 Apr 25    i  `* Re: Bart's Language2Tim Rentsch
27 Apr 25    i   `- Re: Bart's Language1Keith Thompson
18 Mar 25    `* Re: Bart's Language7bart
20 Mar 25     `* Re: Bart's Language6Waldek Hebisch
21 Mar 25      +* Re: Bart's Language4Kaz Kylheku
21 Mar 25      i`* Re: Bart's Language3bart
21 Mar 25      i `* Re: Bart's Language2Kaz Kylheku
22 Mar 25      i  `- Re: Bart's Language1Tim Rentsch
21 Mar 25      `- Re: Bart's Language1bart

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal