"extern" inside a block

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

    "extern" inside a block

    I've seen some code with extern modifiers in front of variables
    declared inside blocks. Are these purely definitions (no definition)
    or are they definitions with static duration but external linkage?

    Not much on this in the FAQ or tutorials.

  • G Patel

    #2
    Re: "extern&qu ot; inside a block


    G Patel wrote:[color=blue]
    > I've seen some code with extern modifiers in front of variables
    > declared inside blocks. Are these purely definitions (no definition)[/color]

    correction: purely declarations
    [color=blue]
    > or are they definitions with static duration but external linkage?
    >
    > Not much on this in the FAQ or tutorials.[/color]

    Comment

    • Eric Sosman

      #3
      Re: "extern&qu ot; inside a block



      G Patel wrote:[color=blue]
      > G Patel wrote:
      >[color=green]
      >>I've seen some code with extern modifiers in front of variables
      >>declared inside blocks. Are these purely definitions (no definition)[/color]
      >
      >
      > correction: purely declarations
      >
      >[color=green]
      >>or are they definitions with static duration but external linkage?
      >>
      >>Not much on this in the FAQ or tutorials.[/color][/color]

      They are declarations of objects or functions
      defined elsewhere with external linkage. (IMHO they
      are also stylistically repugnant, but that's another
      discussion.)

      --
      Eric.Sosman@sun .com

      Comment

      • E. Robert Tisdale

        #4
        Re: "extern&qu ot; inside a block

        G Patel wrote:
        [color=blue]
        > I've seen some code with extern modifiers
        > in front of variables declared inside blocks.
        > Are these purely [declarations] (no definition)[/color]

        Yes.
        [color=blue]
        > or are they definitions with static duration but external linkage?[/color]

        No.
        [color=blue]
        > Not much on this in the FAQ or tutorials.[/color]

        Not surpizing.
        There is seldom call for this.
        I use it when I want to keep declarations private:
        [color=blue]
        > cat main.c[/color]
        #include <stdio.h>

        int main(int argc, char* argv[]) {
        extern void* malloc(size_t);
        extern void free(void*);
        const size_t n = 13;
        int* p = (int*)malloc(n* sizeof(int));
        free((void*)p);
        return 0;
        }

        When, for example, I want to use a private helper function.

        Comment

        • G Patel

          #5
          Re: &quot;extern&qu ot; inside a block

          Eric Sosman wrote:[color=blue]
          > G Patel wrote:[color=green]
          > > G Patel wrote:
          > >[color=darkred]
          > >>I've seen some code with extern modifiers in front of variables
          > >>declared inside blocks. Are these purely definitions (no[/color][/color][/color]
          definition)[color=blue][color=green]
          > >
          > >
          > > correction: purely declarations
          > >
          > >[color=darkred]
          > >>or are they definitions with static duration but external linkage?
          > >>
          > >>Not much on this in the FAQ or tutorials.[/color][/color]
          >
          > They are declarations of objects or functions
          > defined elsewhere with external linkage. (IMHO they
          > are also stylistically repugnant, but that's another
          > discussion.)[/color]

          I've seen things like this too:

          {
          extern int foo = 10;

          /* rest of block */
          }

          So if extern means it's a declaration, would that line really mean:

          {
          extern in foo; /* declare an already defined object */
          foo = 10; /* assign to that object */

          /* rest of block */
          }

          Comment

          • Eric Sosman

            #6
            Re: &quot;extern&qu ot; inside a block



            G Patel wrote:[color=blue]
            >
            > I've seen things like this too:
            >
            > {
            > extern int foo = 10;
            >
            > /* rest of block */
            > }[/color]

            If you've seen them and the compiler didn't complain,
            the compiler was being operated in a non-conforming mode:

            6.7.8 Initialization
            /5/ If the declaration of an identifier has block
            scope, and the identifier has external or internal
            linkage, the declaration shall have no initializer
            for the identifier.

            If you've seen `extern int foo = 10;' at file scope,
            outside a block, that's fine: it's a definition of `foo'
            with an initializer, specifying (unnecessarily) external
            linkage. But it's not permitted to do this inside a block.

            --
            Eric.Sosman@sun .com

            Comment

            • G Patel

              #7
              Re: &quot;extern&qu ot; inside a block

              Eric Sosman wrote:[color=blue]
              > G Patel wrote:[color=green]
              > >
              > > I've seen things like this too:
              > >
              > > {
              > > extern int foo = 10;
              > >
              > > /* rest of block */
              > > }[/color]
              >
              > If you've seen them and the compiler didn't complain,
              > the compiler was being operated in a non-conforming mode:
              >
              > 6.7.8 Initialization
              > /5/ If the declaration of an identifier has block
              > scope, and the identifier has external or internal
              > linkage, the declaration shall have no initializer
              > for the identifier.
              >
              > If you've seen `extern int foo = 10;' at file scope,
              > outside a block, that's fine: it's a definition of `foo'
              > with an initializer, specifying (unnecessarily) external
              > linkage. But it's not permitted to do this inside a block.
              >[/color]

              You're right, I've only seen extern with initializer at the top of a
              file. This is weird, so with an intializer it's a definition, without
              an initializer it's a declaraction.

              I have one more question. In multi source file programs, I put extern
              declaractions for variables I want to share amongst many source files
              in a headder, and include that in all those source files. I never used
              to include that in the file I defined the file-scope variable. I
              tried it after a suggestion and it works. But is this 100% proper (not
              undefined)?

              Technically, after preprocessing that source file has this at the top:


              extern T var; /* came in from #include */

              T var; /* was in the .c file */



              I know the behaviour when extern declaration follows a non-extern
              declaraction is okay, because the extern takes the
              linkage/characteristics of the non-extern declaration (either static or
              implicity external). But how does this work when extern declaration
              PRECEDES a non-extern declaration?

              Am I safe to include the header like this?

              Comment

              • Beta What

                #8
                Re: &quot;extern&qu ot; inside a block


                G Patel wrote:[color=blue]
                >[/color]

                --snipped--
                [color=blue]
                >
                > I know the behaviour when extern declaration follows a non-extern
                > declaraction is okay, because the extern takes the
                > linkage/characteristics of the non-extern declaration (either static[/color]
                or[color=blue]
                > implicity external). But how does this work when extern declaration
                > PRECEDES a non-extern declaration?
                >
                > Am I safe to include the header like this?[/color]

                You need to get yourself a copy of K&R2.

                Comment

                • Luke Wu

                  #9
                  Re: &quot;extern&qu ot; inside a block

                  G Patel wrote:
                  [color=blue]
                  >
                  > I have one more question. In multi source file programs, I put[/color]
                  extern[color=blue]
                  > declaractions for variables I want to share amongst many source files
                  > in a headder, and include that in all those source files. I never[/color]
                  used[color=blue]
                  > to include that in the file I defined the file-scope variable. I
                  > tried it after a suggestion and it works. But is this 100% proper[/color]
                  (not[color=blue]
                  > undefined)?
                  >[/color]

                  No. This is perfectly fine to do. This is actually more convenient
                  (add the header in every source file that requires the use of the
                  identifier) and allows the compiler to better catch descrepancies
                  between the single definition and the form of the declarations.
                  [color=blue]
                  >
                  > Technically, after preprocessing that source file has this at the[/color]
                  top:[color=blue]
                  >
                  >
                  > extern T var; /* came in from #include */
                  >
                  > T var; /* was in the .c file */
                  >
                  >
                  >
                  > I know the behaviour when extern declaration follows a non-extern
                  > declaraction is okay, because the extern takes the
                  > linkage/characteristics of the non-extern declaration (either static[/color]
                  or[color=blue]
                  > implicity external).
                  >[/color]

                  Yes. The standard says...

                  6.2.2 Linkage of Identifiers
                  +++++++++++++++ ++++++++++++++
                  (4) For an identifier with the storage-class specifier extern in a
                  scope in which a prior declaration of that identifier is visible [and
                  has internal or external linkage] ... the linkage of that later
                  declaration is the same ... (continued below)


                  So this explains a situation such as:

                  T var; /* has external linkage */
                  extern T var; /* has same linkage - external */

                  OR

                  static T var; /* has internal linkage */
                  extern T var; /* has same linkage - internal */

                  Note: "NO LINKAGE" doesn't have the same affect on later extern
                  declarations.

                  You said you already knew this, which is good because it will help
                  understand what happens when the order is switched.
                  [color=blue]
                  >
                  > But how does this work when extern declaration
                  > PRECEDES a non-extern declaration?
                  >[/color]

                  The standard says...

                  6.2.2 Linkage of Identifiers
                  +++++++++++++++ ++++++++++++++
                  (4 continued from above)... If no prior declaration is visible [for
                  declaration with extern storage-class specifier], or if the prior
                  declaration specifies no linkage, then the identifier has external
                  linkage.

                  (5) ... If the declaration of an identifier for an object has file
                  scope and no storage-class specifier, its linkage is external.


                  This gives you the answer to your question. The first sentence tells
                  us that extern declaraed identifiers have external linkage if there is
                  no previous identical identifier with external/internal linkage. The
                  second sentence tells us that a declaration at file scope without a
                  storage class specifier has external linkage.

                  So in your question:

                  /* external linkage - no previous internal/external linkage */
                  extern T var;

                  T var; /* external linkage - no storage class specifier & file scope */


                  So both become declaration with external linkage, and specify the same
                  objects (linkage works when identifiers are in the same scope , or
                  different scopes.)

                  Comment

                  • Luke Wu

                    #10
                    Re: &quot;extern&qu ot; inside a block


                    Beta What wrote:[color=blue]
                    > G Patel wrote:[color=green]
                    > >[/color]
                    >
                    > --snipped--
                    >[color=green]
                    > >
                    > > I know the behaviour when extern declaration follows a non-extern
                    > > declaraction is okay, because the extern takes the
                    > > linkage/characteristics of the non-extern declaration (either[/color][/color]
                    static[color=blue]
                    > or[color=green]
                    > > implicity external). But how does this work when extern[/color][/color]
                    declaration[color=blue][color=green]
                    > > PRECEDES a non-extern declaration?
                    > >
                    > > Am I safe to include the header like this?[/color]
                    >
                    > You need to get yourself a copy of K&R2.[/color]

                    K&R2 wouldn't help the OP much in this case (appendix *might* provide
                    *some* help).

                    Comment

                    • Richard Bos

                      #11
                      Re: &quot;extern&qu ot; inside a block

                      "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                      [color=blue]
                      > #include <stdio.h>
                      >
                      > int main(int argc, char* argv[]) {
                      > extern void* malloc(size_t);[/color]

                      This loses (use the headers, damn it!)...
                      [color=blue]
                      > extern void free(void*);[/color]

                      ....this loses...
                      [color=blue]
                      > const size_t n = 13;
                      > int* p = (int*)malloc(n* sizeof(int));[/color]

                      ....this loses majorly (and you know it)...
                      [color=blue]
                      > free((void*)p);[/color]

                      ....and now you're just being silly.
                      [color=blue]
                      > return 0;
                      > }
                      >
                      > When, for example, I want to use a private helper function.[/color]

                      There is no such thing in C.

                      Richard

                      Comment

                      • Giorgos Keramidas

                        #12
                        Re: &quot;extern&qu ot; inside a block

                        On 2005-02-23 09:56, E. Robert Tisdale wrote:[color=blue]
                        > G Patel wrote:[color=green]
                        >> I've seen some code with extern modifiers
                        >> in front of variables declared inside blocks.
                        >> Are these purely [declarations] (no definition)[/color]
                        >
                        > Yes.
                        >[color=green]
                        >> or are they definitions with static duration but external linkage?[/color]
                        >
                        > No.
                        >[color=green]
                        >> Not much on this in the FAQ or tutorials.[/color]
                        >
                        > Not surpizing.
                        > There is seldom call for this.[/color]

                        You mean 'extremely rarely' or 'never', of course.
                        [color=blue]
                        > I use it when I want to keep declarations private:
                        >[color=green]
                        > > cat main.c[/color]
                        > #include <stdio.h>
                        >
                        > int main(int argc, char* argv[]) {
                        > extern void* malloc(size_t);
                        > extern void free(void*);
                        > const size_t n = 13;
                        > int* p = (int*)malloc(n* sizeof(int));
                        > free((void*)p);
                        > return 0;
                        > }
                        >
                        > When, for example, I want to use a private helper function.[/color]

                        All that because it is easier to type two extra lines with 'prototypes'
                        for malloc() and free(), just for the fun of making your life difficult?

                        You could have just included <stdlib.h> you know :P

                        Comment

                        • CBFalconer

                          #13
                          Re: &quot;extern&qu ot; inside a block

                          Giorgos Keramidas wrote:[color=blue]
                          > On 2005-02-23 09:56, E. Robert Tisdale wrote:
                          >[/color]
                          .... snip ...[color=blue]
                          >[color=green]
                          >> I use it when I want to keep declarations private:
                          >>[color=darkred]
                          >> > cat main.c[/color]
                          >> #include <stdio.h>
                          >>
                          >> int main(int argc, char* argv[]) {
                          >> extern void* malloc(size_t);
                          >> extern void free(void*);
                          >> const size_t n = 13;
                          >> int* p = (int*)malloc(n* sizeof(int));
                          >> free((void*)p);
                          >> return 0;
                          >> }
                          >>
                          >> When, for example, I want to use a private helper function.[/color]
                          >
                          > All that because it is easier to type two extra lines with 'prototypes'
                          > for malloc() and free(), just for the fun of making your life difficult?
                          >
                          > You could have just included <stdlib.h> you know :P[/color]
                          _______________ ______
                          /| /| | |
                          ||__|| | Please do not |
                          / O O\__ | feed the |
                          / \ | Trolls |
                          / \ \|_____________ ________|
                          / _ \ \ ||
                          / |\____\ \ ||
                          / | | | |\____/ ||
                          / \|_|_|/ | _||
                          / / \ |____| ||
                          / | | | --|
                          | | | |____ --|
                          * _ | |_|_|_| | \-/
                          -- _--\ _ \ | ||
                          / _ \\ | / `
                          / \_ /- | | |
                          * ___ c_c_c_C/ \C_c_c_c_______ _____

                          --
                          "If you want to post a followup via groups.google.c om, don't use
                          the broken "Reply" link at the bottom of the article. Click on
                          "show options" at the top of the article, then click on the
                          "Reply" at the bottom of the article headers." - Keith Thompson


                          Comment

                          Working...