How to protect a global variable in a header file from external access? ("static int i" does not wor

Discussion in 'C Programming' started by Lu, Jul 8, 2003.

  1. Lu

    Lu Guest

    Hello, I am wondering how to protect a global variable in a header file from
    external access. So I googled and found:

    "The keyword 'static' has two different uses, depending on whether it is
    applied to an external variable or function or to an automatic variable.
    When applied to an external variable (global) variable the scope of that
    variable to the file in which it is declared. This is useful to hide buffers
    and variables that are used only by functions in a particular file."

    However, when I tried to teach my self and do the following exercise, it
    shows me that static global variables are still available in "main.c".

    [jxlu@edusrv jxlu]$ cat static_i.c
    #include <stdio.h>
    #include "static_i.h"

    int main(int argc,char *argv[])
    {
    printf("i=%d\n",i);
    return 0;
    }
    [jxlu@edusrv jxlu]$ cat static_i.h
    static int i=0;
    [jxlu@edusrv jxlu]$ gcc -Wall -o static_i static_i.c
    [jxlu@edusrv jxlu]$ ./static_i
    i=0
    [jxlu@edusrv jxlu]$

    It really puzzles me! Any suggestions?

    Sincerely,
    Lu
     
    Lu, Jul 8, 2003
    #1
    1. Advertising

  2. On Tue, 8 Jul 2003 11:06:45 +0800, Lu <> wrote:
    > [jxlu@edusrv jxlu]$ cat static_i.c
    > #include <stdio.h>
    > #include "static_i.h"
    >
    > int main(int argc,char *argv[])
    > {
    > printf("i=%d\n",i);
    > return 0;
    > }
    > [jxlu@edusrv jxlu]$ cat static_i.h
    > static int i=0;
    > [jxlu@edusrv jxlu]$ gcc -Wall -o static_i static_i.c
    > [jxlu@edusrv jxlu]$ ./static_i
    > i=0
    > [jxlu@edusrv jxlu]$


    Your problem is, in part, a misunderstanding of how headers work. When
    you say:
    #include "static_i.h"

    all you're doing is, in essence, copying and pasting static_i.h into
    whatever file is including it. So your static_i.c actually starts off
    like:
    #include <stdio.h>
    static int i=0;
    ....

    static_i.c is getting its own copy of i. Any file that includes
    static_i.h gets its own copy of i, because you will have essentially
    typed "static int i;" into each source file.

    First, let me tell you not to put definitions in header files. In this
    case it actually works (as in, compiles), but unless you have some real
    need for each source file to have its own object named "i", it's not
    good style (and even then I'd argue it's not good style).

    Here's how you can see static in action:
    $ cat > i.c
    static int i;
    ^D
    $ cat > main.c
    /* Here's the important part: you're telling main.c that someone else
    * has defined an object named "i" and you want to use that object.
    * Hopefully the static-ness of "i" won't let this happen.
    */
    extern int i;

    int main(void)
    {
    i = 0;
    return 0;
    }
    ^D
    $ cc main.c i.c
    /tmp/cc0Iyqp6.o: In function `main':
    /tmp/cc0Iyqp6.o(.text+0x12): undefined reference to `i'

    So as you see, main.c wasn't able to find "i", as was expected.

    When you're trying to give stuff internal linkage, it doesn't make sense
    to do it in headers; headers are included in various source files, but
    objects/functions with internal linkage are tied, necessarily, to one
    source file.

    Chris
     
    Chris Spiegel, Jul 8, 2003
    #2
    1. Advertising

  3. On Tue, 8 Jul 2003 11:06:45 +0800, in comp.lang.c , "Lu"
    <> wrote:

    >Hello, I am wondering how to protect a global variable in a header file from
    >external access.


    You can't. A variable in a header file is accessible in every source
    file which includes that variable.

    You do it the other way round. Declare the variable as static in one
    file, and write accessor functions to read the data. Put the accessor
    declarations in some header.

    static int x;
    int getx(){ return x;}
    int setx(int v) { int tmp = x; x = v; return tmp;}

    and so forth.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jul 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. knutivar
    Replies:
    2
    Views:
    340
    bruce barker
    Dec 9, 2004
  2. Replies:
    1
    Views:
    509
    Victor Bazarov
    Aug 17, 2005
  3. mlt
    Replies:
    2
    Views:
    880
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Jochen Friedmann

    Search more than one wor in a string

    Jochen Friedmann, Jun 30, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    85
    Jochen Friedmann
    Jun 30, 2004
  5. Pugi!
    Replies:
    2
    Views:
    207
    Randy Webb
    Mar 18, 2006
Loading...

Share This Page