static variable

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

    #1

    static variable

    Hi there

    I have a question about static variables. There is a function which returns
    a pointer to a structure "AStructure ". The function has a static local
    variable of type AStructure, and it is the address of this variable which is
    returned. Is this good practice?

    See this pseudo code:

    AStructure *my_function()
    {
    static AStructure result;

    // "result" is filled with data...

    return ( &result );
    }

    Thanks,
    Peter

  • jacob navia

    #2
    Re: static variable


    "Peter Kirk" <peter> a écrit dans le message de
    news:40f79255$1 @news.wineasy.s e...[color=blue]
    > Hi there
    >
    > I have a question about static variables. There is a function which[/color]
    returns[color=blue]
    > a pointer to a structure "AStructure ". The function has a static local
    > variable of type AStructure, and it is the address of this variable which[/color]
    is[color=blue]
    > returned. Is this good practice?
    >
    > See this pseudo code:
    >
    > AStructure *my_function()
    > {
    > static AStructure result;
    >
    > // "result" is filled with data...
    >
    > return ( &result );
    > }
    >
    > Thanks,
    > Peter
    >[/color]

    It is 100% correct as far as I see.

    Problems *could* arise in a
    multi-threaded context because of simultaneous access to a single
    memory location. If one thread enters this function during the time when
    another thread is updating the data structure problems would arise.

    This problem can be solved with a semaphore or a similar construct that
    protects the access to the static variable and allows only one thread
    to work on it at a time.

    In a single-thread environment there are no problems


    Comment

    • jota

      #3
      Re: static variable

      > returned. Is this good practice?

      Yes why not!
      If you can bare the fact that the function is not reentrant and the
      static duration makes sizeof(AStructu re) bytes allocated even if its never
      used,
      or only used for a short while!

      For local scope use:
      void my_function(ASt ructure *a)
      {
      a->blahblah=...
      }

      void caller()
      {
      AStructure a;
      my_function(&a) ;
      }

      //jota



      Comment

      • Emmanuel Delahaye

        #4
        Re: static variable

        In 'comp.lang.c', "Peter Kirk" <peter> wrote:
        [color=blue]
        > I have a question about static variables. There is a function which
        > returns a pointer to a structure "AStructure ". The function has a static
        > local variable of type AStructure, and it is the address of this
        > variable which is returned. Is this good practice?
        >
        > AStructure *my_function()
        > {
        > static AStructure result;
        >
        > // "result" is filled with data...
        >
        > return ( &result );
        > }[/color]

        It is technically correct, but is it a good practice or not is debatable.

        The presence of a static variable in the code makes this code non re-entrant.

        Personally, I'm involved into preemptive multithreading telecom projects
        where this practice is simply prohibited. Now, it's up to you.

        --
        -ed- get my email here: http://marreduspam.com/ad672570
        The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
        C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
        FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

        Comment

        • Harti Brandt

          #5
          Re: static variable

          On Fri, 16 Jul 2004, jacob navia wrote:

          jn>
          jn>"Peter Kirk" <peter> a écrit dans le message de
          jn>news:40f7925 5$1@news.wineas y.se...
          jn>> Hi there
          jn>>
          jn>> I have a question about static variables. There is a function which
          jn>returns
          jn>> a pointer to a structure "AStructure ". The function has a static local
          jn>> variable of type AStructure, and it is the address of this variable which
          jn>is
          jn>> returned. Is this good practice?
          jn>>
          jn>> See this pseudo code:
          jn>>
          jn>> AStructure *my_function()
          jn>> {
          jn>> static AStructure result;
          jn>>
          jn>> // "result" is filled with data...
          jn>>
          jn>> return ( &result );
          jn>> }
          jn>>
          jn>> Thanks,
          jn>> Peter
          jn>>
          jn>
          jn>It is 100% correct as far as I see.
          jn>
          jn>Problems *could* arise in a
          jn>multi-threaded context because of simultaneous access to a single
          jn>memory location. If one thread enters this function during the time when
          jn>another thread is updating the data structure problems would arise.
          jn>
          jn>This problem can be solved with a semaphore or a similar construct that
          jn>protects the access to the static variable and allows only one thread
          jn>to work on it at a time.
          jn>
          jn>In a single-thread environment there are no problems

          There may be. If the "filled with data" is different for different
          calls to the function, the following will not do what you think it should:

          foo(my_function (), my_function());

          A typical instance of this problem are formatting functions for user
          defined types that use an internal buffer:

          const char *
          fmt_complex(com plex c)
          {
          static char buf[100];

          sprintf(buf, "%g+%gI", creal(c), cimag(c));
          return (buf);
          }

          then call

          void
          foo(complex a, complex b)
          {
          printf("a=%s b=%s\n", fmt_complex(a), fmt_complex(b)) ;
          }

          harti

          Comment

          • kal

            #6
            Re: static variable

            "jacob navia" <jacob@jacob.re mcomp.fr> wrote in message news:<cd84nf$hs o$1@news-reader4.wanadoo .fr>...[color=blue]
            > "Peter Kirk" <peter> a écrit dans le message de
            > news:40f79255$1 @news.wineasy.s e...[color=green]
            > >
            > > AStructure *my_function()
            > > {
            > > static AStructure result;
            > >
            > > // "result" is filled with data...
            > >
            > > return ( &result );
            > > }[/color]
            >
            > In a single-thread environment there are no problems[/color]

            IMHO this is not quite correct.

            Comment

            • jacob navia

              #7
              Re: static variable

              You are right, I didn't see that possibility.

              Thanks for the correction




              Comment

              • Nils O. Selåsdal

                #8
                Re: static variable

                On Fri, 16 Jul 2004 10:47:43 +0200, jacob navia wrote:
                [color=blue]
                >
                > "Peter Kirk" <peter> a écrit dans le message de
                > news:40f79255$1 @news.wineasy.s e...[color=green]
                >> Hi there
                >>
                >> I have a question about static variables. There is a function which[/color]
                > returns[color=green]
                >> a pointer to a structure "AStructure ". The function has a static local
                >> variable of type AStructure, and it is the address of this variable which[/color]
                > is[color=green]
                >> returned. Is this good practice?
                >>
                >> See this pseudo code:
                >>
                >> AStructure *my_function()
                >> {
                >> static AStructure result;
                >>
                >> // "result" is filled with data...
                >>
                >> return ( &result );
                >> }
                >>
                >> Thanks,
                >> Peter
                >>[/color]
                >
                > It is 100% correct as far as I see.
                >
                > Problems *could* arise in a
                > multi-threaded context because of simultaneous access to a single
                > memory location. If one thread enters this function during the time when
                > another thread is updating the data structure problems would arise.
                >
                > This problem can be solved with a semaphore or a similar construct that
                > protects the access to the static variable and allows only one thread
                > to work on it at a time.
                >
                > In a single-thread environment there are no problems[/color]
                Not other than common "user" bugs, such as calling the function
                twice and expect the pointer from the first call to be what
                one expect. Just make sure users of the function know it isn't reentrant.
                Just thought it was worth mentioning.

                Comment

                • Alan Balmer

                  #9
                  Re: static variable

                  On 16 Jul 2004 10:31:17 +0200, "Peter Kirk" <peter> wrote:
                  [color=blue]
                  >Hi there
                  >
                  >I have a question about static variables. There is a function which returns
                  >a pointer to a structure "AStructure ". The function has a static local
                  >variable of type AStructure, and it is the address of this variable which is
                  >returned. Is this good practice?
                  >
                  >See this pseudo code:
                  >
                  >AStructure *my_function()
                  >{
                  > static AStructure result;
                  >
                  > // "result" is filled with data...
                  >
                  > return ( &result );
                  >}
                  >
                  >Thanks,
                  >Peter[/color]

                  This sort of thing is fairly common, and usable in single-thread
                  environments, so long as it's well documented. Some standard library
                  functions do this. I hesitate to call it "good practice", since this
                  sort of thing is responsible for many programming errors.

                  --
                  Al Balmer
                  Balmer Consulting
                  removebalmercon sultingthis@att .net

                  Comment

                  • Kenneth Brody

                    #10
                    Re: static variable

                    Peter Kirk wrote:[color=blue]
                    >
                    > Hi there
                    >
                    > I have a question about static variables. There is a function which returns
                    > a pointer to a structure "AStructure ". The function has a static local
                    > variable of type AStructure, and it is the address of this variable which is
                    > returned. Is this good practice?
                    >
                    > See this pseudo code:
                    >
                    > AStructure *my_function()
                    > {
                    > static AStructure result;
                    >
                    > // "result" is filled with data...
                    >
                    > return ( &result );
                    > }[/color]

                    With the caveat that each call to my_function() overwrites the result
                    of the previous call, there is nothing "wrong" with the above code.
                    Because the local variable is static, it still exists upon return from
                    the function, and "&result" is still a valid pointer.

                    --
                    +-------------------------+--------------------+-----------------------------+
                    | Kenneth J. Brody | www.hvcomputer.com | |
                    | kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                    +-------------------------+--------------------+-----------------------------+


                    Comment

                    • Peter

                      #11
                      Re: static variable

                      Thanks for all the answers - about what I expected, but I just wasn't sure.

                      I have been trying to maintain some old code, and ran across this "pattern"
                      many places. It is not documented, and I thought it could be related to some
                      problems we have been seeing - but apparently the original programmer was
                      careful in his calls of these functions, as they are never called again
                      before the previous call is finished processing the result.

                      Peter


                      Comment

                      Working...