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

L

Lu

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
 
C

Chris Spiegel

[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
 
M

Mark McIntyre

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top