Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to protect a global variable in a header file from external access? ("static int i" does not work!)

Reply
Thread Tools

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

 
 
Lu
Guest
Posts: n/a
 
      07-08-2003
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




 
Reply With Quote
 
 
 
 
Chris Spiegel
Guest
Posts: n/a
 
      07-08-2003
On Tue, 8 Jul 2003 11:06:45 +0800, Lu <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      07-08-2003
On Tue, 8 Jul 2003 11:06:45 +0800, in comp.lang.c , "Lu"
<(E-Mail Removed)> 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 =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why does "template<typename T> ... function<T(int)>" not match "int(&)(int)" implicitly? Steve Hicks C++ 2 09-28-2009 05:24 PM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments