| Deutsch English Français Italiano |
|
<864izooz0w.fsf@linuxsc.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch <tr.17687@z991.linuxsc.com> Newsgroups: comp.lang.c Subject: Re: int a = a (Was: Bart's Language) Date: Thu, 20 Mar 2025 02:02:39 -0700 Organization: A noiseless patient Spider Lines: 60 Message-ID: <864izooz0w.fsf@linuxsc.com> References: <vracit$178ka$1@dont-email.me> <vrc2d5$1jjrf$1@paganini.bofh.team> <vrc4eb$2p28t$1@dont-email.me> <vrc75b$2r4lt$1@dont-email.me> <vrccjb$b3m6$1@news.xmission.com> <vrcef2$33076$1@dont-email.me> <vrelvn$12ddq$1@dont-email.me> <aqCCP.590465$SZca.348513@fx13.iad> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 20 Mar 2025 10:02:42 +0100 (CET) Injection-Info: dont-email.me; posting-host="7c01e97b5d1836204810413d59dbf156"; logging-data="2981858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OQI7CNktFWUHlt103qPzvsGptiFR9P1Q=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:foWIzk22tXGHeH1l4sFVuBPEctY= sha1:iWjScrByS2qEI+lzvdP1SHGw9NY= Bytes: 3872 scott@slp53.sl.home (Scott Lurndal) writes: > David Brown <david.brown@hesbynett.no> writes: > >> On 18/03/2025 19:36, Janis Papanagnou wrote: >> >>> On 18.03.2025 19:04, Kenny McCormack wrote: >>> >>>> In article <vrc75b$2r4lt$1@dont-email.me>, >>>> David Brown <david.brown@hesbynett.no> wrote: >>>> ... >>>> >>>>>> gcc won't warn until you say '-Wextra', and then only for: >>>>>> >>>>>> int a = a + 1; >>>>> >>>>> People would not normally write "int a = a;". It is used as a >>>>> common idiom meaning "I know it is not clear to the compiler >>>>> that the variable is always initialised before use, but /I/ know >>>>> it is - so disable the use-without-initialisation warnings for >>>>> this variable". >>> >>> Wow! - It would never have occurred to me that "int a = a;" being >>> considered an idiom, let alone a "common idiom". >> >> It is certainly an idiom, and certainly viewed by gcc as a way to >> avoid an "uninitialized" warning (unless "-Winit-self" is also >> enabled), and it is an idiom I have seen documented in at least one >> other compiler (though I can't remember which - I've read many >> compiler manuals over the decades). >> >> But judging from the posts here, it may not be a "common" idiom. >> >> (And I am not suggesting it is a /good/ idiom. It's not one I use >> myself, and I have "-Winit-self" in my list of standard warning >> flags because it is conceivable that I write "int a = a;" in error. >> But there are lots of idioms and common practices in C that I don't >> like personally.) >> >> >> As far as I understand it (and I hope to be corrected if I am >> wrong), "int a = a;" is not undefined behaviour as long as the >> implementation does not have trap values for "int". It simply >> leaves "a" as an unspecified value - just like "int a;" does. Thus >> it is not in any way "worse" than "int a;" as far as C semantics >> are concerned. Any difference is a matter of implementation - and >> the usual implementation effect is to disable "not initialised" >> warnings. >> >> It is in much the same category as "(void) x;", which is an idiom >> for skipping an "unused variable" or "unused parameter" warning. > > I would disagree with this last statement. (void)x is genuinely > useful and has no ill side effects. 'int a = a;' is exactly > the opposite - not useful and has potenial bad side effects. I concur except that I recommend using (void)&x rather than (void)x, because (void)&x is safer. If x is volatile qualified, for example, the expression (void)x actually does something, and must not be compiled away, whereas (void)&x does not have these properties.