Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #391524

Re: int a = a

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: int a = a
Date 2025-03-22 13:59 -0700
Organization A noiseless patient Spider
Message-ID <86a59clr3a.fsf@linuxsc.com> (permalink)
References (9 earlier) <87cyect356.fsf@nosuchdomain.example.com> <vrhbsf$3e7sn$4@dont-email.me> <87msdfscxj.fsf@nosuchdomain.example.com> <vrjcd5$18m5n$1@dont-email.me> <87pliaqjb9.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> David Brown <david.brown@hesbynett.no> writes:
>
>> [...]I believe it would be much simpler and clearer if attempting
>> to read an uninitialised and unassigned local variable were
>> undefined behaviour in every case.
>
> I probably agree (I haven't given it all that much thought), but
> the committee made a specific decision between C90 and C99 to say
> that reading an uninitialized automatic object is *not* undefined
> behavior.  I'm don't know why they did that (though, all else
> being equal, reducing the number of instances of undefined
> behavior is a good thing), but reversing that decision for this
> one issue is not something they decided to do.

Your description of what was done is wrong.  It is still the case in
C99 that trying to access an uninitialized object is undefined
behavior, at least potentially, except for accesses using a type
that either is a character type or has no trap representations (and
all types other than unsigned char may have trap representations,
depending on the implementation).  A statement like

    int a = a;

may still be given a warning as potential undefined behavior, even
in C99.

>> Alternatively, it could have said that the value is unspecified
>> in every case.  Then on the IA-64, the compiler would have to
>> ensure that registers do not have their NaT bit set even if they
>> are not initialised - this would not be a difficult task.
>> Enabling use of the NaT bit for detection of bugs could then be a
>> compiler option if implementations wanted to provide that
>> feature.
>
> The whole point of the NaT bit is to detect accesses to
> uninitialized values.  Requiring the compiler to arbitrarily clear
> that bit doesn't strike me as a good idea.
>
> I dislike the way that wording was added to the standard
> specifically to cater to one specific CPU (which happens to have
> been discontinued later).  I would have been happier with a more
> general solution.  I that making accessing the value of an
> uninitialized automatic object UB would have been much cleaner,
> and it would have allowed for sensible use of NaT by IA-64
> compilers.

I think you may be missing a key point.  In both C99 and C11,
accessing an uninitialized object using a character type is defined
(albeit unspecified) behavior.  But in C11, because of the changes
in 6.3.2.1, even character types are subject to undefined behavior
when uninitialized objects are accessed (provided of course that
they don't fall under an exception because their address was taken).
The C11 rule does more than allowing undefined behavior just for
non-character types;  it extends the possibility of undefined
behavior to character types as well.

> But without knowing *why* the committee removed that UB between
> C90 and C99, I'm hesitant to say it was a mistake.

The mistake is thinking that UB for uninitialized access was
removed in C99.  It wasn't.  Narrowed, yes;  removed, no.  And
later C11 simply widened it back a bit, recovering some of the
territory that had been taken away in C99.  The Itanium may
have been what prompted the change, but the change that was made
is one well worth making.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Bart's Language bart <bc@freeuk.com> - 2025-03-17 23:51 +0000
  Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-18 12:17 +0000
    Re: Bart's Language bart <bc@freeuk.com> - 2025-03-18 13:54 +0000
      Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-18 15:10 +0000
        Re: Bart's Language bart <bc@freeuk.com> - 2025-03-18 15:45 +0000
          Re: Bart's Language David Brown <david.brown@hesbynett.no> - 2025-03-18 17:31 +0100
            int a = a (Was: Bart's Language) gazelle@shell.xmission.com (Kenny McCormack) - 2025-03-18 18:04 +0000
              Re: int a = a (Was: Bart's Language) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-03-18 19:36 +0100
                Re: int a = a (Was: Bart's Language) Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-18 19:11 +0000
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-19 15:56 +0100
                Re: int a = a (Was: Bart's Language) scott@slp53.sl.home (Scott Lurndal) - 2025-03-19 16:38 +0000
                Re: int a = a (Was: Bart's Language) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-03-19 14:29 -0700
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-20 09:39 +0100
                Re: int a = a (Was: Bart's Language) bart <bc@freeuk.com> - 2025-03-20 11:59 +0000
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-20 15:46 +0100
                Re: int a = a (Was: Bart's Language) wij <wyniijj5@gmail.com> - 2025-03-20 23:13 +0800
                Re: int a = a (Was: Bart's Language) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-20 02:02 -0700
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-20 15:57 +0100
                Re: int a = a (Was: Bart's Language) Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-19 17:07 +0000
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-19 13:34 -0700
                Re: int a = a Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-20 02:54 -0700
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-20 03:20 -0700
                Re: int a = a David Brown <david.brown@hesbynett.no> - 2025-03-20 16:22 +0100
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-20 12:46 -0700
                Re: int a = a David Brown <david.brown@hesbynett.no> - 2025-03-21 10:44 +0100
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-21 12:23 -0700
                Re: int a = a David Brown <david.brown@hesbynett.no> - 2025-03-21 21:46 +0100
                Re: int a = a Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-22 13:59 -0700
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-22 15:37 -0700
                Re: int a = a David Brown <david.brown@hesbynett.no> - 2025-03-20 15:42 +0100
              Re: int a = a (Was: Bart's Language) scott@slp53.sl.home (Scott Lurndal) - 2025-03-18 19:37 +0000
              Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-18 20:51 +0100
                Re: int a = a (Was: Bart's Language) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-03-18 23:27 +0100
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-19 11:40 +0100
              Re: int a = a (Was: Bart's Language) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-18 23:52 -0700
                Re: int a = a Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-19 01:55 -0700
                Re: int a = a (Was: Bart's Language) David Brown <david.brown@hesbynett.no> - 2025-03-19 11:43 +0100
                Re: int a = a (Was: Bart's Language) Rosario19 <Ros@invalid.invalid> - 2025-03-19 13:23 +0100
                Re: int a = a (Was: Bart's Language) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-20 01:32 -0700
          Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-20 22:55 +0000
            Re: Bart's Language Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-20 16:22 -0700
              Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-22 14:37 +0000
                Re: Bart's Language James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-03-22 11:41 -0400
                Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-22 16:52 +0000
                Re: Bart's Language James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-03-22 20:12 -0400
                By definition... (Was: Bart's Language) gazelle@shell.xmission.com (Kenny McCormack) - 2025-03-23 17:20 +0000
        Re: Bart's Language bart <bc@freeuk.com> - 2025-03-18 22:19 +0000
          Re: Bart's Language antispam@fricas.org (Waldek Hebisch) - 2025-03-20 22:38 +0000
            Re: Bart's Language Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-20 23:45 +0000
              Re: Bart's Language bart <bc@freeuk.com> - 2025-03-21 00:56 +0000
                Re: Bart's Language Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-21 17:47 +0000
                Re: Bart's Language Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-22 07:12 -0700
            Re: Bart's Language bart <bc@freeuk.com> - 2025-03-21 00:33 +0000

csiph-web