Structure initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CBFalconer

    #16
    Re: Structure initialization

    John Bode wrote:
    jb.si...@lmco.c om wrote:
    >
    >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.
    >>
    .... 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 ?
    >
    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."
    A little more readable if taken from N869.txt:

    [#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

    [#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.

    See <http://cbfalconer.home .att.net/download/n869_txt.bz2>

    --
    [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

    • santosh

      #17
      Re: Structure initialization

      pete wrote:
      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?
      I mean that here, outside of transnational companies, it's very unusual
      in the middle to low end jobs here for your "boss" to let you take time
      off, even if it might benefit your work efficiency in the long run.

      Specifically, with regards to C, clc's standards are, IME _very_ high.
      For example in the code bases I have had the opportunity to examine I
      have never once come across the form of structure initialisation
      demonstrated in this thread.

      Comment

      • none

        #18
        Re: Structure initialization

        Harald van Dijk wrote:
        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.
        Thanks for all of the replies.

        I forwarded the relevant parts of the standard to the person who
        commented, indicating it *is* standard, and then I also changed my code
        to add functions (4 of them) to explicitly initialize all fields to 0.
        It was not a big deal, however, now if any of those structures change
        the appropriate initialization function also needs to change.

        In a way this is really not so bad, because this supports an setting the
        variables to non zero at initialization if desired.

        P.S. The reason I put these in a function is these structures can be
        kinda large (50-100 members) and i didn't want to clutter the caller
        with all of the initialization. To me it makes it harder to read the
        function if you have lines and lines of initializaiton code.

        Thanks All !
        Joe

        Comment

        Working...