Static variables

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

    #1

    Static variables

    If I have the following setup

    void Vdlog_SetVport( int reason)
    {
    static double lx,ly,hx,hy;

    Vmath_GetBox(&l x,&ly,&hx,&hy) ;
    ....
    }

    void Vmath_GetBox(do uble *lx,double *ly,double *hx,double *hy)
    {
    .....
    }

    Will the pointers passed to Vmath_GetBox be out of scope (I have tried and
    it causes a crash) ?

    Ie are static variables only visible within the routine to which they are
    declared ?

    --
    David Buck
    2D CAD for RISC OS at www.risccad.freeuk.com


  • Keith Thompson

    #2
    Re: Static variables

    "David Buck" <david.buck@fre euk.com> writes:[color=blue]
    > If I have the following setup
    >
    > void Vdlog_SetVport( int reason)
    > {
    > static double lx,ly,hx,hy;
    >
    > Vmath_GetBox(&l x,&ly,&hx,&hy) ;
    > ...
    > }
    >
    > void Vmath_GetBox(do uble *lx,double *ly,double *hx,double *hy)
    > {
    > ....
    > }
    >
    > Will the pointers passed to Vmath_GetBox be out of scope (I have tried and
    > it causes a crash) ?
    >
    > Ie are static variables only visible within the routine to which they are
    > declared ?[/color]

    If you declare a static variable within a block, its name is visible
    only within that block, but it has static storage duration, meaning
    that it exists for the entire execution of the program. There should
    be no problem with the code you posted. Even if the variables weren't
    static, accessing them indirectly from a called function should be ok;
    their lifetime doesn't end until Vdlog_SetVport( ) terminates, which
    won't happen until *after* Vmath_GetBox() terminates. (In the latter
    case, you could have problems if Vmath_GetBox() stashes away the
    addresses of its parameters for use after Vdlog_SetVport( ) has
    terminated, but even that's not a problem if the variables are static
    -- though it would be horrible style.)

    Is there a prototype for Vmath_GetBox() visible at the point of the
    call? Even that probably shouldn't cause any visible problems, since
    the types of the arguments match the expected types of the parameters,
    unless your actual code differs from what you posted.

    Can you trim your program to a small but complete program that
    exhibits the problem?

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Emmanuel Delahaye

      #3
      Re: Static variables

      David Buck a écrit :[color=blue]
      > If I have the following setup
      >
      > void Vdlog_SetVport( int reason)
      > {
      > static double lx,ly,hx,hy;
      >
      > Vmath_GetBox(&l x,&ly,&hx,&hy) ;
      > ...
      > }
      >
      > void Vmath_GetBox(do uble *lx,double *ly,double *hx,double *hy)
      > {
      > ....
      > }
      >
      > Will the pointers passed to Vmath_GetBox be out of scope[/color]

      No. And additionally, the variables in Vdlog_SetVport( ) don't need to be
      static (at least for what we know about them)
      [color=blue]
      > (I have tried and
      > it causes a crash) ?[/color]

      Sounds to be for another reason.
      [color=blue]
      > Ie are static variables only visible within the routine to which they are
      > declared ?[/color]

      Yes, but their durations are program life. Hence you can pass their
      addresses to anyone.

      Design issue:

      When you have more than one pointer to 'similary' data as parameters of
      a function, it probably means that you'd better use some structure and a
      pointer to a structure. It makes interfaces and coding less clumsy.

      struct my_data
      {
      double lx;
      double ly;
      double hx;
      double hy;
      };

      void Vdlog_SetVport( int reason)
      {
      /* you probably don't need this 'static' */
      static my_data data;

      Vmath_GetBox(&d ata);
      ...
      }

      void Vmath_GetBox(my _data *p_data)

      see my point ?

      --
      A+

      Emmanuel Delahaye

      Comment

      • David Buck

        #4
        Re: Static variables

        Emmanuel Delahaye wrote:[color=blue]
        > David Buck a écrit :[color=green]
        >> If I have the following setup
        >>
        >> void Vdlog_SetVport( int reason)
        >> {
        >> static double lx,ly,hx,hy;
        >>
        >> Vmath_GetBox(&l x,&ly,&hx,&hy) ;
        >> ...
        >> }
        >>
        >> void Vmath_GetBox(do uble *lx,double *ly,double *hx,double *hy)
        >> {
        >> ....
        >> }
        >>
        >> Will the pointers passed to Vmath_GetBox be out of scope[/color]
        >
        > No. And additionally, the variables in Vdlog_SetVport( ) don't need to
        > be static (at least for what we know about them)
        >[color=green]
        >> (I have tried and
        >> it causes a crash) ?[/color]
        >
        > Sounds to be for another reason.
        >[color=green]
        >> Ie are static variables only visible within the routine to which
        >> they are declared ?[/color]
        >
        > Yes, but their durations are program life. Hence you can pass their
        > addresses to anyone.
        >
        > Design issue:
        >
        > When you have more than one pointer to 'similary' data as parameters
        > of a function, it probably means that you'd better use some structure
        > and a pointer to a structure. It makes interfaces and coding less
        > clumsy.
        > struct my_data
        > {
        > double lx;
        > double ly;
        > double hx;
        > double hy;
        > };
        >
        > void Vdlog_SetVport( int reason)
        > {
        > /* you probably don't need this 'static' */
        > static my_data data;
        >
        > Vmath_GetBox(&d ata);
        > ...
        > }
        >
        > void Vmath_GetBox(my _data *p_data)
        >
        > see my point ?[/color]

        Thanks everyone, I have re-coded with a structure as suggested.

        The problem is that the program operates on a system whereby the routines
        are re-entered with various reason codes, in a multitasking WIMP
        environment, so Vdlog_SetVport does terminate after setting up the
        Vmath_GetBox function (which waits for the user to enter co-ordinates using
        the mouse). Vmath_GetBox then fills in the entered co-ordinates in the *lx
        pointer, which, I presume, as Vdlog_SetVport is not current, cannot be
        accessed.

        --
        David Buck
        2D CAD for RISC OS at www.risccad.freeuk.com


        Comment

        • Old Wolf

          #5
          Re: Static variables

          David Buck wrote:
          [color=blue]
          > void Vdlog_SetVport( int reason)
          > {
          > static double lx,ly,hx,hy;
          > Vmath_GetBox(&l x,&ly,&hx,&hy) ;
          >
          > Will the pointers passed to Vmath_GetBox be out of scope[/color]

          Pointers don't have scope. Identifiers have scope, and variables have
          lifetime.

          The scope of the identifiers "lx", "ly", "hx", and "hy" is local to
          that function. Writing "lx" somewhere else in your program will
          say "undefined identifier" or some similar error.

          But the variables identified by those identifiers, have a permanent
          lifetime (ie. they always exist). You can access those variables at
          any point in your program -- as long as you have some way of
          knowing whereabouts in memory they are, if you are accessing
          them from somewhere where their names are not in scope.
          [color=blue]
          > (I have tried and it causes a crash) ?[/color]

          Your crash is for some other reason. Try posting a complete
          compilable program that demonstrates the crash.

          (NB. Google ran out of hard disk space when i was trying to
          post this. Apologies if it ends up as a double-post.)

          Comment

          Working...