'static' to make object accessible by file

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

    'static' to make object accessible by file


    Say we have a static external object (object defined outside of any
    block with static qualifier) in a file.
    We are telling the compiler that we intend that object to be accessed
    by functions in the current file that are below the definition.

    Now what if one of these functions passes a pointer value to this
    object to a function in another file, and that function uses the
    pointer to access this static object? Is this 'normal' or dependent on
    the specific environment?

    This works on my system but I am not sure if this is a normal thing I
    should be doing or it's just because my environment/compiler isn't
    picking up on the bad move. If it's okay, is it bad style?

    So, does the C standard say that such a move is allowed (either
    directly or indirectly by not saying that it is disallowed)?

  • Ian Collins

    #2
    Re: 'static' to make object accessible by file

    Lax wrote:
    Say we have a static external object (object defined outside of any
    block with static qualifier) in a file.
    We are telling the compiler that we intend that object to be accessed
    by functions in the current file that are below the definition.
    >
    Now what if one of these functions passes a pointer value to this
    object to a function in another file, and that function uses the
    pointer to access this static object? Is this 'normal' or dependent on
    the specific environment?
    Normal. A static object does not have global linkage, but it still has
    to exist somewhere.

    --
    Ian Collins.

    Comment

    • Walter Roberson

      #3
      Re: 'static' to make object accessible by file

      In article <78b1bedd-a17a-4c65-9a28-cbab26648da7@e3 9g2000hsf.googl egroups.com>,
      Lax <Lax.Clarke@gma il.comwrote:
      >Say we have a static external object (object defined outside of any
      >block with static qualifier) in a file.
      >We are telling the compiler that we intend that object to be accessed
      >by functions in the current file that are below the definition.
      >Now what if one of these functions passes a pointer value to this
      >object to a function in another file, and that function uses the
      >pointer to access this static object? Is this 'normal' or dependent on
      >the specific environment?
      That is normal and allowed. There are lurking dangers, such as if
      the static data is declared const but the routines do not consistantly
      use the const qualifier and ending up modifying the data.
      --
      'Roberson' is my family name; my given name is 'Walter'.

      Comment

      Working...