Structure initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jb.simon@lmco.com

    Structure initialization

    Recently I was pinged in a code review about my use of the
    initialization method
    AStruct myStruct = { 0 } ;

    Which initializes all elements of the myStruct to 0.

    I was questioned on it because no one had seen this construct.
    So i looked it up as best i could in the pointers to the Specification
    that I find here and could not find any specific reference to this
    construct. What i did find was reference to (paraphrasing) any values
    not specified in the initializer are initialized to zero.

    so it seems that the construct ={0}, sets the fisrst element to 0 and
    since there are no more initializers present, all other structure
    members are set to zero by default. Is this correct ?

    Thanks
    Joe
  • John Bode

    #2
    Re: Structure initialization

    On Jun 10, 12:53 pm, jb.si...@lmco.c om wrote:
    Recently I was pinged in a code review about my use of the
    initialization method
    AStruct myStruct = { 0 } ;
    >
    Which initializes all elements of the myStruct to 0.
    >
    I was questioned on it because no one had seen this construct.
    So i looked it up as best i could in the pointers to the Specification
    that I find here and could not find any specific reference to this
    construct. What i did find was reference to (paraphrasing) any values
    not specified in the initializer are initialized to zero.
    >
    so it seems that the construct ={0}, sets the fisrst element to 0 and
    since there are no more initializers present, all other structure
    members are set to zero by default. Is this correct ?
    >
    Thanks
    Joe
    Sort of. Here's the language from n1124:

    "6.7.8. 21 If there are fewer initializers in a brace-enclosed list
    than there are
    elements or members of an aggregate, or fewer characters in a string
    literal used
    to initialize an array of known size than there are elements in the
    array, the
    remainder of the aggregate shall be initialized implicitly the same
    as objects that
    have static storage duration."

    And the section on initialization of static objects:

    "10 If an object that has automatic storage duration is not
    initialized explicitly,
    its value is indeterminate. If an object that has static storage
    duration is not
    initialized explicitly, then:
    — if it has pointer type, it is initialized to a null pointer;
    — if it has arithmetic type, it is initialized to (positive or
    unsigned) zero;
    — if it is an aggregate, every member is initialized (recursively)
    according to these rules;
    — if it is a union, the first named member is initialized
    (recursively) according to
    these rules."

    Comment

    • Walter Roberson

      #3
      Re: Structure initialization

      In article <13f6f4be-d45e-48e6-a203-4aa8f66b9ccc@26 g2000hsk.google groups.com>,
      <jb.simon@lmco. comwrote:
      >Recently I was pinged in a code review about my use of the
      >initializati on method
      >AStruct myStruct = { 0 } ;
      >Which initializes all elements of the myStruct to 0.
      >I was questioned on it because no one had seen this construct.
      It does what you expected, and is, I believe, recognized by
      experienced C programmers.

      On the other hand, code reviews are often useful to catch constructs
      that might be unclear to the student programmer who is going to
      be asked to work on the code several years from now.

      In my opinion, the code is correct and there is no value in changing
      the code to explicitly initialize all the values "just in case" someone
      doesn't understand the code; the explicit initialization would just
      become a source of potential errors. But for future code maintenance,
      it wouldn't hurt to put in a comment indicating that the entire
      structure was being initialized; the comment code include a reference
      to the specific section of the Standard for those who might be
      a little too convinced that the code is mistaken.
      --
      "Let me live in my house by the side of the road --
      It's here the race of men go by.
      They are good, they are bad, they are weak, they are strong
      Wise, foolish -- so am I;" -- Sam Walter Foss

      Comment

      • Yunzhong

        #4
        Re: Structure initialization

        On Jun 10, 1:53 pm, jb.si...@lmco.c om wrote:
        Recently I was pinged in a code review about my use of the
        initialization method
        AStruct myStruct = { 0 } ;
        >
        Which initializes all elements of the myStruct to 0.
        >
        I was questioned on it because no one had seen this construct.
        So i looked it up as best i could in the pointers to the Specification
        that I find here and could not find any specific reference to this
        construct. What i did find was reference to (paraphrasing) any values
        not specified in the initializer are initialized to zero.
        >
        so it seems that the construct ={0}, sets the fisrst element to 0 and
        since there are no more initializers present, all other structure
        members are set to zero by default. Is this correct ?
        >
        Thanks
        Joe

        I think you are right. The only "exception" I can think of is that
        pointer members will be initialized to a null pointer constant which
        may not be all-bit zero, but maybe a null pointer constant can be
        considered as just a special kind of zero.

        Comment

        • Andrey Tarasevich

          #5
          Re: Structure initialization

          jb.simon@lmco.c om wrote:
          Recently I was pinged in a code review about my use of the
          initialization method
          AStruct myStruct = { 0 } ;
          >
          Which initializes all elements of the myStruct to 0.
          >
          I was questioned on it because no one had seen this construct.
          So i looked it up as best i could in the pointers to the Specification
          that I find here and could not find any specific reference to this
          construct. What i did find was reference to (paraphrasing) any values
          not specified in the initializer are initialized to zero.
          >
          so it seems that the construct ={0}, sets the fisrst element to 0 and
          since there are no more initializers present, all other structure
          members are set to zero by default. Is this correct ?
          ...
          Yes, that's absolutely correct (with the exception of _unnamed_ members,
          which are left uninitialized). The construct you used is a
          well-recognized zero-initialization idiom in C.

          The fact that "no one had seen this construct" in your team simply means
          that other members haven't learned it yet.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          • Harald van =?UTF-8?b?RMSzaw==?=

            #6
            Re: Structure initialization

            On Tue, 10 Jun 2008 10:53:14 -0700, jb.simon wrote:
            so it seems that the construct ={0}, sets the fisrst element to 0 and
            since there are no more initializers present, all other structure
            members are set to zero by default. Is this correct ?
            That is correct. And when the first member is an array or structure (or a
            union starting with one of these), it applies to the remaining members or
            elements of the first member as well. That is, when X is a simple integer
            constant, possibly 0, possibly something else,

            struct A {
            struct B {
            int a, b, c;
            } s;
            int d;
            } a = {X};

            initialises a.s.a to X, while a.s.b, a.s.c, and a.d are all given their
            default values of 0.

            Comment

            • Keith Thompson

              #7
              Re: Structure initialization

              John Bode <john_bode@my-deja.comwrites:
              [snip]
              Sort of. Here's the language from n1124:
              [snip]

              You're still using n1124? n1256 is more current.



              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • pete

                #8
                Re: Structure initialization

                Walter Roberson wrote:
                In article <13f6f4be-d45e-48e6-a203-4aa8f66b9ccc@26 g2000hsk.google groups.com>,
                <jb.simon@lmco. comwrote:
                >
                >Recently I was pinged in a code review about my use of the
                >initializati on method
                >AStruct myStruct = { 0 } ;
                >
                >Which initializes all elements of the myStruct to 0.
                >
                >I was questioned on it because no one had seen this construct.
                >
                It does what you expected, and is, I believe, recognized by
                experienced C programmers.
                >
                On the other hand, code reviews are often useful to catch constructs
                that might be unclear to the student programmer who is going to
                be asked to work on the code several years from now.
                >
                In my opinion, the code is correct and there is no value in changing
                the code to explicitly initialize all the values "just in case" someone
                doesn't understand the code; the explicit initialization would just
                become a source of potential errors. But for future code maintenance,
                it wouldn't hurt to put in a comment indicating that the entire
                structure was being initialized; the comment code include a reference
                to the specific section of the Standard for those who might be
                a little too convinced that the code is mistaken.
                If the only reason that a programmer
                can't understand a particular C code construct,
                is because the programmer doesn't know enough C,
                then it's time to learn more C.

                What kind of a programmer needs to be told that?
                Are child labor laws no longer being enforced?

                --
                pete

                Comment

                • santosh

                  #9
                  Re: Structure initialization

                  pete wrote:

                  <snip>
                  If the only reason that a programmer
                  can't understand a particular C code construct,
                  is because the programmer doesn't know enough C,
                  then it's time to learn more C.
                  Only if your boss gives you time off to do so.

                  <snip>

                  Comment

                  • Walter Roberson

                    #10
                    Re: Structure initialization

                    In article <W8Gdne8CXaxGSd PVnZ2dnUVZ_sTin Z2d@earthlink.c om>,
                    pete <pfiland@mindsp ring.comwrote:
                    >If the only reason that a programmer
                    >can't understand a particular C code construct,
                    >is because the programmer doesn't know enough C,
                    >then it's time to learn more C.
                    >What kind of a programmer needs to be told that?
                    >Are child labor laws no longer being enforced?
                    When it comes to programming... No, they aren't.

                    Some of my programming-related skills are -relatively- rare
                    even amongst programmers, and as a result I earn a decent
                    (but not rich) living from my work. But I grew up in an era
                    when my father literally maintained *mechanical* calculators
                    (and manual typewriters and non-programmable electric typewriters),
                    together with a few early electronic calculators with very basic
                    functionality and Nixie tubes (the mechanical calculators had
                    more functionality!) . My nieces and nephews are growing up in
                    an era when it isn't uncommon for a five-year-old to be better at
                    using computers than their parents.

                    High school computer multi-millionares are a reality: the young have a
                    grasp of the potential (and the likely social desirability) of computer
                    systems that I will likely never have. So Yes, children *are*
                    programming these days, and are producing some amazing things.


                    But beyond that... there will always be a steady stream of the
                    marginally competant who don't know enough to look in a reference book.
                    And if the university students I often see posting in another technical
                    newsgroup are typical of the modern generation, then it appears that
                    we have somehow managed to raise a generation of programmers who
                    consider it to be beneath them to do any research, convinced that
                    they are *owed* complete code to do <whatever>, annoyed that they
                    even have to ask once for it... and yet convinced that whatever they
                    "know" is right and anything contra-indicative of what they "know"
                    is wrong (or was *deliberately* mis-designed.) A generation of
                    Entitlement to other people's skills and labour, and one that
                    too often has an antipathy to learning. It is a generation that
                    is proving surprisingly difficult to teach. :(

                    Thus, including a comment with a reference to the Standard can be
                    form of self-defence: the barbarians likely will not stop to -read-
                    the Standard before complaining about your code, but you can
                    then respond, "And what did that section of the Standard say
                    about the situation??" instead of having to spend your time writing
                    memos pointing out the relevant section, and spending your time
                    defending your programming style, and being written up as
                    "not a team player" and whatever.
                    --
                    "The whole history of civilization is strewn with creeds and
                    institutions which were invaluable at first, and deadly
                    afterwards." -- Walter Bagehot

                    Comment

                    • Richard Heathfield

                      #11
                      Re: Structure initialization

                      Yunzhong said:
                      On Jun 10, 1:53 pm, jb.si...@lmco.c om wrote:
                      <snip>
                      >>
                      >so it seems that the construct ={0}, sets the fisrst element to 0 and
                      >since there are no more initializers present, all other structure
                      >members are set to zero by default. Is this correct ?
                      >
                      >
                      I think you are right. The only "exception" I can think of is that
                      pointer members will be initialized to a null pointer constant which
                      may not be all-bit zero,
                      It doesn't matter - it's not an exception regardless of the representation
                      of a null pointer. The static default initialisers that apply here are 0
                      for integer types, 0.0 for floating point types, and NULL for pointer
                      types.
                      but maybe a null pointer constant can be
                      considered as just a special kind of zero.
                      Yes, sort of.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Keith Thompson

                        #12
                        Re: Structure initialization

                        Richard Heathfield <rjh@see.sig.in validwrites:
                        Yunzhong said:
                        >On Jun 10, 1:53 pm, jb.si...@lmco.c om wrote:
                        <snip>
                        >>>
                        >>so it seems that the construct ={0}, sets the fisrst element to 0 and
                        >>since there are no more initializers present, all other structure
                        >>members are set to zero by default. Is this correct ?
                        >>
                        >>
                        >I think you are right. The only "exception" I can think of is that
                        >pointer members will be initialized to a null pointer constant which
                        >may not be all-bit zero,
                        >
                        It doesn't matter - it's not an exception regardless of the representation
                        of a null pointer. The static default initialisers that apply here are 0
                        for integer types, 0.0 for floating point types, and NULL for pointer
                        types.
                        Yes.

                        Another way to look at it is that the default initializer is simply 0
                        for *all* scalar types, converted in each case to the appropriate
                        type.

                        [...]

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • pete

                          #13
                          Re: Structure initialization

                          Keith Thompson wrote:
                          Richard Heathfield <rjh@see.sig.in validwrites:
                          >Yunzhong said:
                          >>On Jun 10, 1:53 pm, jb.si...@lmco.c om wrote:
                          ><snip>
                          >>>so it seems that the construct ={0}, sets the fisrst element to 0 and
                          >>>since there are no more initializers present, all other structure
                          >>>members are set to zero by default. Is this correct ?
                          >>>
                          >>I think you are right. The only "exception" I can think of is that
                          >>pointer members will be initialized to a null pointer constant which
                          >>may not be all-bit zero,
                          >It doesn't matter - it's not an exception regardless of the representation
                          >of a null pointer. The static default initialisers that apply here are 0
                          >for integer types, 0.0 for floating point types, and NULL for pointer
                          >types.
                          >
                          Yes.
                          >
                          Another way to look at it is that the default initializer is simply 0
                          for *all* scalar types, converted in each case to the appropriate
                          type.
                          Another way to look at it is that the default initializer is simply {0}
                          for *all* types.

                          --
                          pete

                          Comment

                          • pete

                            #14
                            Re: Structure initialization

                            santosh wrote:
                            pete wrote:
                            >If the only reason that a programmer
                            >can't understand a particular C code construct,
                            >is because the programmer doesn't know enough C,
                            >then it's time to learn more C.
                            >
                            Only if your boss gives you time off to do so.
                            Do I understand you to mean
                            that if your boss saw you reading a C reference
                            while you were supposed to be writing C code,
                            that your boss would be displeased?

                            That notion seems bizarre to me.
                            What do you really mean?

                            --
                            pete

                            Comment

                            • CBFalconer

                              #15
                              Re: Structure initialization

                              Walter Roberson wrote:
                              >
                              .... snip ...
                              >
                              Some of my programming-related skills are -relatively- rare
                              even amongst programmers, and as a result I earn a decent
                              (but not rich) living from my work. But I grew up in an era
                              when my father literally maintained *mechanical* calculators
                              (and manual typewriters and non-programmable electric typewriters),
                              together with a few early electronic calculators with very basic
                              functionality and Nixie tubes (the mechanical calculators had
                              more functionality!) . My nieces and nephews are growing up in
                              an era when it isn't uncommon for a five-year-old to be better at
                              using computers than their parents.
                              I deny that functionallity allegation. :-) See:

                              <http://cbfalconer.home .att.net/firstpc/>

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.


                              ** Posted from http://www.teranews.com **

                              Comment

                              Working...