Re: int a = a (Was: Bart's Language)

Liste des GroupesRevenir à cl c 
Sujet : Re: int a = a (Was: Bart's Language)
De : wyniijj5 (at) *nospam* gmail.com (wij)
Groupes : comp.lang.c
Date : 20. Mar 2025, 16:13:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <9b5f3e86dd67128bb6612523ae17a7368b97284a.camel@gmail.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : Evolution 3.54.3 (3.54.3-1.fc41)
On Thu, 2025-03-20 at 15:46 +0100, David Brown wrote:
On 20/03/2025 12:59, bart wrote:
On 20/03/2025 08:39, David Brown wrote:
On 19/03/2025 22:29, Chris M. Thomasson wrote:
 
The scares me a bit:
_____________________
#include <stdio.h>
 
int main() {
 
     int a = 666;
 
     {
         // Humm... Odd to me...
         int a = a;
 
         printf("%d", a);
     }
 
     return 0;
}
 
Yes, that is an unfortunate consequence of the scoping in C - on the
line "int a = a;", the new "a" is initialised with its own unspecified
value, not with the value of the outer "a".
 
So the inner 'a' wasn't supposed to mysteriously inherit the outer a's
value? (Perhaps due to sharing the same location.) Since the compilers I
tried just showed zero not 666.
 
No, the inner "a" is completely new and is initialised with itself.  As
Keith has pointed out, this is UB - but of course compilers can choose
how to implement the code in question.  gcc seems to pick "initialise to
0" as a default when you write "int a;" and then read "a", and does the
same for "int a = a;".  Other compilers could choose to do something
different, such as pick a register for "a" and leave the value in the
register unchanged - in which case it might, by luck, pick up the value
of the outer "a".

Yes, it (self-reference) should an UB like divide by zero error.
https://sourceforge.net/projects/cscall/files/MisFiles/ClassGuidelines.txt/download
 .....[cut]
 This guideline has no strong opinion on how this self-ops should be handled,
 yet (it is like divided by zero error). Implement may check the self-ops (not
 generally doable) and return ELOOP. Working around might be needed since the
 intent mostly assumes the argument is passed by value, nonetheless a
 theoretical bug might be thus hidden.

IMO, if the compiler sees it, it should issue at least a warning.

 
If the scope of the new variable did not start until after the
initialisation, then it would have allowed a number of possibilities
that would be a lot more useful than the "disable warnings about
uninitialised variables" effect or initialisers which refer to their
own address.  For example :
 
     int a = 123;
     {
         int a = a;
         // local copy that does not affect the outer one
     ...
 
 
     int a = 123;
     {
         long long int a = a;
         // Local copy that with expanded size
     ...
 
     for (int a = 0; a < 100; a++) {
         const int a = a;
         // "Lock" the loop index to catch errors
 
As it is, you need to do something like :
 
     for (int a = 0; a < 100; a++) {
         const int _a = a;
         const int a = _a;
         // "Lock" the loop index to catch errors
 
You mean locking the loop index so it can't be modified?
 
Yes.
 



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