Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #401426

Re: Question about struct initializers

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c
Subject Re: Question about struct initializers
Date 2026-08-22 14:16 -0700
Organization A noiseless patient Spider
Message-ID <116d3ij$1f9qm$1@dont-email.me> (permalink)
References (4 earlier) <1166psa$375be$1@news.xmission.com> <1167kdh$3oo4a$2@dont-email.me> <1168uba$2168$4@dont-email.me> <116a8ek$ishu$1@dont-email.me> <116c176$1274o$2@dont-email.me>

Show all headers | View raw


On 8/22/2026 4:29 AM, David Brown wrote:
> On 21/08/2026 21:20, Chris M. Thomasson wrote:
>> On 8/21/2026 12:22 AM, David Brown wrote:
>>> On 20/08/2026 21:26, Chris M. Thomasson wrote:
>>>> On 8/20/2026 4:53 AM, Kenny McCormack wrote:
>>>>> In article <1166ost$3dg4d$1@dont-email.me>,
>>>>> David Brown  <david.brown@hesbynett.no> wrote:
>>>>> ...
>>>>>> (It's nice to see that your reputation as group cynic has not been
>>>>>> totally ruined by your clear, topical C question starting this 
>>>>>> thread!)
>>>>>
>>>>> Heh heh.  Well, somebody's got to do it.
>>>>>
>>>>> This actually came up in real code that I was working on, and it 
>>>>> seemed odd
>>>>> that it worked (*).  Unless it was actually part of the language, 
>>>>> as it now
>>>>> seems it is.
>>>>>
>>>>> (*) Worked in the sense that it was always 0.  Not that the code was
>>>>> relying on that, of course.
>>>>>
>>>>
>>>> simply never assume that a in say:
>>>>
>>>> int a;
>>>>
>>>> equals zero. Its fairly simple.
>>>
>>> That only applies to non-static local variables.  Program lifetime 
>>> data is always initialised.  Code can happily rely on initialisation 
>>> happening as required by the rules of the C language.
>>>
>>
>> Shit. Well, still, I have a habit of trying to initialize everything. 
>> So, yes, I tend to write:
>>
>> static int g_a = 0;
> 
> Write that if you like - and if it makes code clearer, it can be a good 
> thing.  It is unlikely to be helpful to the reader to have an explicit 
> "0" initialiser, but it certainly could be if it is an enumeration 
> value, constexpr value, macro, or something else that is not obviously 0.
> 
> Contrary to Lawrence's answer, it typically /will/ make a difference to 
> generated code - just one that is completely negligible in all but the 
> smallest of embedded systems.  In most of the toolchains I have used, 
> "static int g;" goes in the ".bss" segment and gets zeroed as part of a 
> loop, while "static int g = 0;" goes in the ".data" segment and is 
> initialised by copying from a read-only segment of the executable.  Thus 
> the explicit initialisation is marginally less efficient.


> On the other hand, there are a few embedded toolchains (primarily those 
> provided by Texas Instruments) which do not zero-initialise program 
> lifetime data that has no explicit initialisation - they do not clear 
> the ".bss" and "static int g;" variables before main().  The misguided 
> fools at TI call this a "feature" leading to faster startup - users call 
> it a painful and deceptive flaw and non-conformity which has lead to 
> countless problems and wasted debugging time.  It has also lead some 
> providers of portable embedded code to use explicit initialisation to 0 
> as you have done, giving less efficient code on platforms with more 
> conforming tools.
> 
>>
>> Shit happens.

Indeed. It's just a habit I got into several decades ago.


> I get the impression that you don't understand what that phrase means. 
> You should stop using it - it makes no sense in the contexts you write it.


:^) I wrote that because according to C std, I never needed to get into 
the habit of:

static int g_a = 0;

I could have left it as:

static int g_a;

sigh... So as the phrase goes.

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


Thread

Question about struct initializers gazelle@shell.xmission.com (Kenny McCormack) - 2026-08-17 20:48 +0000
  Re: Question about struct initializers bart <bc@freeuk.com> - 2026-08-17 22:21 +0100
    Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:46 +0800
  Re: Question about struct initializers bixbox <noreply@example.invalid> - 2026-08-17 23:23 +0200
    Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:47 +0800
    Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-18 10:39 +0200
  Re: Question about struct initializers scott@slp53.sl.home (Scott Lurndal) - 2026-08-17 21:44 +0000
    Re: Question about struct initializers Michael S <already5chosen@yahoo.com> - 2026-08-18 01:06 +0300
      Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-18 11:11 +0200
        Re: Question about struct initializers Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-18 03:58 -0700
    Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-17 22:34 +0000
      Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:39 +0800
        Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-17 23:03 +0000
          Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 11:24 +0800
          Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-18 11:19 +0200
      Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-18 11:16 +0200
      Re: Question about struct initializers Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-18 22:45 +0000
        Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-19 00:08 +0000
          Re: Question about struct initializers Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-19 04:32 +0000
            Re: Question about struct initializers Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-18 22:34 -0700
            Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-19 12:21 +0000
              Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-19 14:33 +0200
                Re: Question about struct initializers scott@slp53.sl.home (Scott Lurndal) - 2026-08-19 14:53 +0000
              Re: Question about struct initializers Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-19 23:29 +0000
                Re: Question about struct initializers Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-19 17:43 -0700
                Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-20 09:56 +0000
                Re: Question about struct initializers scott@slp53.sl.home (Scott Lurndal) - 2026-08-20 14:53 +0000
                Re: Question about struct initializers Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-20 13:34 -0700
                Re: Question about struct initializers scott@slp53.sl.home (Scott Lurndal) - 2026-08-20 21:00 +0000
                Re: Question about struct initializers tTh <tth@none.invalid> - 2026-08-21 05:43 +0200
                Re: Question about struct initializers scott@slp53.sl.home (Scott Lurndal) - 2026-08-21 14:56 +0000
                Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-22 00:31 +0800
              Re: Question about struct initializers gazelle@shell.xmission.com (Kenny McCormack) - 2026-08-20 02:00 +0000
                Why are you picking on Lawrence? (was: Re: Question about struct initializers) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-20 13:16 +0800
                Re: Why are you picking on Lawrence? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-20 12:24 -0700
                Re: Question about struct initializers richard@cogsci.ed.ac.uk (Richard Tobin) - 2026-08-20 10:14 +0000
                Re: Question about struct initializers gazelle@shell.xmission.com (Kenny McCormack) - 2026-08-20 11:54 +0000
                Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-20 13:37 +0200
                Re: Question about struct initializers gazelle@shell.xmission.com (Kenny McCormack) - 2026-08-20 11:53 +0000
                Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-20 14:42 +0200
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-20 12:26 -0700
                Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-21 09:22 +0200
                Compiler bugs (was: Re: Question about struct initializers) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-22 00:26 +0800
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-21 12:20 -0700
                Re: Question about struct initializers Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-22 05:55 +0000
                Re: Question about struct initializers David Brown <david.brown@hesbynett.no> - 2026-08-22 13:29 +0200
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-22 14:16 -0700
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-22 14:17 -0700
                Re: Question about struct initializers Michael S <already5chosen@yahoo.com> - 2026-08-23 01:23 +0300
            Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-19 12:25 -0700
              Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-20 04:07 +0800
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-19 13:17 -0700
                Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-20 04:27 +0800
                Re: Question about struct initializers "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-20 12:21 -0700
                Fuck you, and fuck you too (was: Re: Question about struct initializers) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-21 12:51 +0800
              Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-20 04:10 +0800
        Re: Question about struct initializers Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-18 17:13 -0700
    Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:45 +0800
  Re: Question about struct initializers Michael S <already5chosen@yahoo.com> - 2026-08-18 01:02 +0300
    Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:35 +0800
  Re: Question about struct initializers Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-18 06:32 +0800

csiph-web