A clarification please

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

    A clarification please

    FAQ 1.7 shows that, amongst other things,

    int i = 0;

    is a definition.

    Page 128 of K&R say:

    "A struct declaration defines a type. The right brace that terminates
    the list of members may be followed by a list of variables, just as
    for any basic type. That is,

    struct {.....} x, y, z:

    is syntactically analogous to

    int x, y, z;

    in the sense that each statement declares x, y, and z to be variables
    of the named type **and causes space to be set aside for them**. {My
    emphasis}

    My understanding was that a definition causes space to be set aside,
    ( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
    in the above example. How can these two ideas be reconciled?

    Thanks as usual.
  • jameskuyper@verizon.net

    #2
    Re: A clarification please

    mdh wrote:
    FAQ 1.7 shows that, amongst other things,
    >
    int i = 0;
    >
    is a definition.
    >
    Page 128 of K&R say:
    >
    "A struct declaration defines a type. The right brace that terminates
    the list of members may be followed by a list of variables, just as
    for any basic type. That is,
    >
    struct {.....} x, y, z:
    >
    is syntactically analogous to
    >
    int x, y, z;
    >
    in the sense that each statement declares x, y, and z to be variables
    of the named type **and causes space to be set aside for them**. {My
    emphasis}
    >
    My understanding was that a definition causes space to be set aside,
    ( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
    in the above example. How can these two ideas be reconciled?
    By noting that your concept of what a definition is isn't quite right.

    A declaration that explicitly initializes the value of a variable is
    always a definition. The same is true of a file scope declaration
    using the keyword 'static', or any other declaration that does not use
    the keyword 'extern'. That last case covers 'int i;' when it occurs at
    block scope.

    However, all other variable declarations are considered "tentative" ,
    including "int i;" if it occurs at file scope, and that's where it
    gets tricky. If a tentative definition is followed by a later
    declaration of the same variable name in the same scope that includes
    an initializer or uses the keyword 'static', the tentative definition
    becomes a real definition. If, by the end of the translation unit,
    there are no non-tentative definitions of a variable, then the
    tentative definition becomes a real one, unless it was declared
    'extern', in which case it becomes a declaration of a variable who's
    actual definition lies elsewhere.

    Comment

    • Barry Schwarz

      #3
      Re: A clarification please

      On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <mdeh@comcast.n et>
      wrote:
      >FAQ 1.7 shows that, amongst other things,
      >
      >int i = 0;
      >
      >is a definition.
      >
      >Page 128 of K&R say:
      >
      >"A struct declaration defines a type. The right brace that terminates
      >the list of members may be followed by a list of variables, just as
      >for any basic type. That is,
      >
      >struct {.....} x, y, z:
      >
      >is syntactically analogous to
      >
      >int x, y, z;
      >
      >in the sense that each statement declares x, y, and z to be variables
      >of the named type **and causes space to be set aside for them**. {My
      >emphasis}
      >
      >My understanding was that a definition causes space to be set aside,
      >( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
      >in the above example. How can these two ideas be reconciled?
      By correcting your understanding.

      int i; is a definition. It causes space to be set aside (in some
      implementation specific sense of the phrase) for object i. If it
      didn't, code such as i = j+k; would have no place to store the result.

      extern int i; is a declaration. It promises that i is in fact defined
      somewhere else and the linker will be able to resolve its location.

      struct t {...}; is a declaration. It doesn't define an object. It
      does "define" a new type. (I prefer to say it declares the type but
      it's hard to argue with K&R.) In any event, defining a type is
      different than defining an object or function. struct t {...} x; is a
      definition. It defines the object x and reserves space for it.

      All of which proves that the word "define" and words derived from it
      mean different things when used to describe the creation of objects or
      used to describe other aspects of the language.

      This is not that unusual. The word "token" means different things
      when talking about the preprocessor, language syntax, or the use of
      the standard function strtok. Context is important when trying to
      decide what things mean.

      --
      Remove del for email

      Comment

      • mdh

        #4
        Re: A clarification please

        On Aug 18, 4:40 pm, jameskuy...@ver izon.net wrote:
        mdh wrote:
        FAQ 1.7 shows that, amongst other things,
        >
        int i = 0;
        >
        is a definition.
        >
        Page 128 of K&R say:
        >
        "A struct declaration defines a type. The right brace that terminates
        the list of members may be followed by a list of variables, just as
        for any basic type. That is,
        >
        struct {.....}  x, y, z:
        >
        is syntactically analogous to
        >
        int x, y, z;
        >
        in the sense that each statement declares x, y, and z to be variables
        of the named type **and causes space to be set aside for them**.  {My
        emphasis}
        >
        My understanding was that a definition causes space to be set aside,
        ( eg int i = 0)  but not a declaration ( eg int i) and the x, y, and z
        in the above example. How can these two ideas be reconciled?
        >
        By noting that your concept of what a definition is isn't quite right.
        >
        A declaration that explicitly initializes the value of a variable is
        always a definition. The same is true of a file scope declaration
        using the keyword 'static', or any other declaration that does not use
        the keyword 'extern'. That last case covers 'int i;' when it occurs at
        block scope.
        >
        However, all other variable declarations are considered "tentative" ,
        including "int i;" if it occurs at file scope, and that's where it
        gets tricky. If a tentative definition is  followed by a later
        declaration of the same variable name in the same scope that includes
        an initializer or uses the keyword 'static', the tentative definition
        becomes a real definition. If, by the end of the translation unit,
        there are no non-tentative definitions of a variable, then the
        tentative definition becomes a real one, unless it was declared
        'extern', in which case it becomes a declaration of a variable who's
        actual definition lies elsewhere.
        My head is spinning!! :-)

        Thank you for that explanation.

        Comment

        • mdh

          #5
          Re: A clarification please

          On Aug 18, 4:48 pm, Barry Schwarz <schwa...@dqel. comwrote:
          On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <m...@comcast.n et>
          wrote:
          >
          >
          >
          FAQ 1.7 shows that, amongst other things,
          >
          int i = 0;
          >
          is a definition.
          >
          >
          struct {.....}  x, y, z:
          >
          t declares x, y, and z to be variables
          of the named type **and causes space to be set aside for them**.  {My
          emphasis}
          >
          How can these two ideas be reconciled?
          >
          By correcting your understanding.

          That, sadly, has been happening a lot lately!! :-)
          >
          int i; is a definition.  It causes space to be set aside (in some
          implementation specific sense of the phrase) for object i.  If it
          didn't, code such as i = j+k; would have no place to store the result.
          >
          extern int i; is a declaration.  It promises that i is in fact defined
          somewhere else and the linker will be able to resolve its location.
          >
          struct t {...}; is a declaration.  It doesn't define an object.  It
          does "define" a new type.  (I prefer to say it declares the type but
          it's hard to argue with K&R.)  In any event, defining a type is
          different than defining an object or function.  struct t {...} x; is a
          definition.  It defines the object x and reserves space for it.  
          >
          All of which proves that the word "define" and words derived from it
          mean different things when used to describe the creation of objects or
          used to describe other aspects of the language.
          >
          This is not that unusual.  The word "token" means different things
          when talking about the preprocessor, language syntax, or the use of
          the standard function strtok.  Context is important when trying to
          decide what things mean.
          >

          thank you Barry. That does clarify it.

          Comment

          • Richard Heathfield

            #6
            Re: A clarification please

            mdh said:

            <snip>
            So, I made up this little bit of code.
            >
            #include <stdio.h>
            >
            int main (int argc, const char * argv[]) {
            >
            >
            int foo (int); /* first declaration of foo */
            >
            int x = foo (7);
            int foo (int); /* 2nd declaration of foo */
            /* no error */
            It's a function declaration. No problem there.
            struct name { /* first declaration of struct */
            int x;
            };
            >
            struct name { /*error : Redefinition of struct name */
            int x;
            };
            I think the reason you're confused may be that you are conflating two
            different meanings of the word "definition ". The meaning we've been
            discussing recently is the definition of an identifier to describe an
            object. But:

            struct name { int x; };

            doesn't define an object - it defines a *type*. Within a given scope, you
            can only define a type once.
            From Ben's note, I expected to get a re-declaration error, (for the
            struct) not a redefinition error.
            Firstly, the Standard doesn't place any limits on the wording of diagnostic
            messages. (Fortunately, most implementations behave reasonably sanely,
            however.)

            Secondly, it /is/ a re-definition error, insofar as you've tried to define
            the same type twice.
            Moreover, if the declaration is
            placed at block level, and a second declaration is placed at file
            level, there is not error generated.
            Different scope.

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

            • mdh

              #7
              Re: A clarification please

              On Aug 18, 10:36 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
              mdh said:
              >
              >
              struct name {  /* first declaration of struct */
              int x;
              };
              >
              struct name {  /*error : Redefinition of struct name */
              int x;
              };
              >
              I think the reason you're confused may be that you are conflating two
              different meanings of the word "definition ". The meaning we've been
              discussing recently is the definition of an identifier to describe an
              object. But:
              >
              struct name { int x; };
              >
              doesn't define an object - it defines a *type*. Within a given scope, you
              can only define a type once.
              >
              Aha...that's what I have been missing. Thank you Richard.

              Comment

              • mdh

                #8
                Re: A clarification please

                On Aug 18, 10:36 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                mdh said:
                >
                <snip>
                >
                So, I made up this little bit of code.
                >
                #include <stdio.h>
                >

                struct name {  /* first declaration of struct */
                int x;
                };
                >
                struct name {  /*error : Redefinition of struct name */
                int x;
                };
                >
                I think the reason you're confused may be that you are conflating two
                different meanings of the word "definition ". The meaning we've been
                discussing recently is the definition of an identifier to describe an
                object. But:
                >
                struct name { int x; };
                >
                doesn't define an object - it defines a *type*. Within a given scope, you
                can only define a type once.


                As I got a good night's sleep, I **think** I see one of the issues
                that has always puzzled me. As soon as one uses the word "definition "
                C is very strict in enforcing the rule of only 1 definition per scope
                and why is this rule in place. If that is indeed true, then perhaps
                the logic for this is as follows?

                A definition sets aside a named space for that object/type.

                So, if more than one definition of the same type/object were to be
                allowed, this will lead to ambiguity when using that definition ie
                which "space" to use when calling that object/using that type?

                Secondly, my thought was , consistent with the error I have often
                seen, "Error. Redefinition of ...etc etc", why not then just set the
                "old" definition to the "new" one. I guess the answer is really
                obvious, but certainly one of them would be...

                What would happen to the previously used data when you simply redefine
                it...it would be unworkable.


                Thanks in advance for something that has always been a puzzle.



                Comment

                • Keith Thompson

                  #9
                  Re: A clarification please

                  mdh <mdeh@comcast.n etwrites:
                  [...]
                  As I got a good night's sleep, I **think** I see one of the issues
                  that has always puzzled me. As soon as one uses the word "definition "
                  C is very strict in enforcing the rule of only 1 definition per scope
                  and why is this rule in place. If that is indeed true, then perhaps
                  the logic for this is as follows?
                  >
                  A definition sets aside a named space for that object/type.
                  [...]

                  A type declaration/definition doesn't set aside any space.

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

                  • mdh

                    #10
                    Re: A clarification please

                    On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgw rote:
                    mdh <m...@comcast.n etwrites:
                    >
                    [...]As I got a good night's sleep, I **think** I see one of the issues
                    that has always puzzled me. As soon as one uses the word "definition "
                    C is very strict in enforcing the rule of only 1 definition per scope
                    and why is this rule in place. If that is indeed true, then perhaps
                    the logic for this is as follows?
                    >
                    A definition sets aside a named space for that object/type.
                    >
                    [...]
                    >
                    A type declaration/definition doesn't set aside any space.
                    >
                    --
                    >
                    Well, there is something about the issue of "definition s" that C
                    strictly enforces. That is what I am trying to figure out.:-)

                    Comment

                    • mdh

                      #11
                      Re: A clarification please

                      On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgw rote:
                      mdh <m...@comcast.n etwrites:
                      >
                      [...]As I got a good night's sleep, I **think** I see one of the issues
                      that has always puzzled me. As soon as one uses the word "definition "
                      C is very strict in enforcing the rule of only 1 definition per scope
                      and why is this rule in place. If that is indeed true, then perhaps
                      the logic for this is as follows?
                      >
                      A definition sets aside a named space for that object/type.
                      >
                      [...]
                      >
                      A type declaration/definition doesn't set aside any space.
                      >
                      --
                      Keith Thompson (The_Other_Keit h) ks...@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"


                      Sorry if my flippant reply to Keith put off further input...if so, it
                      was not meant that way. As i have said, this aspect has always puzzled
                      me.

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: A clarification please

                        mdh <mdeh@comcast.n etwrites:
                        On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgw rote:
                        >mdh <m...@comcast.n etwrites:
                        >>
                        >[...]As I got a good night's sleep, I **think** I see one of the issues
                        that has always puzzled me. As soon as one uses the word "definition "
                        C is very strict in enforcing the rule of only 1 definition per scope
                        and why is this rule in place. If that is indeed true, then perhaps
                        the logic for this is as follows?
                        >>
                        A definition sets aside a named space for that object/type.
                        >>
                        >[...]
                        >>
                        >A type declaration/definition doesn't set aside any space.
                        >>
                        >
                        Sorry if my flippant reply to Keith put off further input...if so, it
                        was not meant that way. As i have said, this aspect has always puzzled
                        me.
                        I doubt it was that. I suspect the silence reflects the fact that
                        questions of the form "Why does C have this rule?" can be very hard to
                        answer. The rules of C evolved over a period of about decade as a
                        compromise between convenience and usability for both compiler writers
                        and compiler users, on a set of machines that most people can't even
                        remember any more.

                        My guess as to the reason why you can't redefine a struct tag in the
                        same scope? There is no significant need, and detecting (and
                        rejecting) a re-definition is easy for the compiler.

                        --
                        Ben.

                        Comment

                        • mdh

                          #13
                          Re: A clarification please

                          mdh <m...@comcast.n etwrites:
                          mdh <m...@comcast.n etwrites:
                          >
                          [...]As I got a good night's sleep, I **think** I see one of the issues
                          that has always puzzled me. As soon as one uses the word "definition "
                          C is very strict in enforcing the rule of only 1 definition per scope
                          and why is this rule in place. If that is indeed true, then perhaps
                          the logic for this is as follows?
                          >
                          A definition sets aside a named space for that object/type.
                          >
                          [...]
                          On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgw rote:
                          >
                          A type declaration/definition doesn't set aside any space.
                          >
                          On Aug 20, 9:18 am, Ben Bacarisse <ben.use...@bsb .me.ukwrote
                          >
                          My guess as to the reason why you can't redefine a struct tag in the
                          same scope?  There is no significant need, and detecting (and
                          rejecting) a re-definition is easy for the compiler.

                          thanks Ben. I guess, for those who like to think there is a logical
                          reason for rules, C has the occasional rule that just is the way it
                          is! I **thought** I had stumbled onto something when it seemed that
                          anything "defined" , be it type of object, seemed to follow this rule
                          about not being allowed more than one definition...an d extended that
                          thought with some logic...which in the end is neither here nor there,
                          as most established programmers just know it.
                          Thanks again for replying.

                          Comment

                          • Keith Thompson

                            #14
                            Re: A clarification please

                            mdh <mdeh@comcast.n etwrites:
                            On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgw rote:
                            >mdh <m...@comcast.n etwrites:
                            >>
                            >[...]As I got a good night's sleep, I **think** I see one of the issues
                            that has always puzzled me. As soon as one uses the word "definition "
                            C is very strict in enforcing the rule of only 1 definition per scope
                            and why is this rule in place. If that is indeed true, then perhaps
                            the logic for this is as follows?
                            >>
                            A definition sets aside a named space for that object/type.
                            >>
                            >[...]
                            >>
                            >A type declaration/definition doesn't set aside any space.
                            >
                            Sorry if my flippant reply to Keith put off further input...if so, it
                            was not meant that way. As i have said, this aspect has always puzzled
                            me.
                            I didn't answer because I didn't really have anything to add, and I
                            though others were answering well enough (and I was too lazy to
                            construct a decent answer anyway). I don't remember what you write,
                            but it didn't strike me as excessively flippant.

                            BTW, please don't quote signatures unless you're actually commenting
                            on them.

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

                            Working...