473,288 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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

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


Nov 13 '05 #1
2 9054
On Tue, 8 Jul 2003 11:06:45 +0800, Lu <jx**@fudan.edu.cn> 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
Nov 13 '05 #2
On Tue, 8 Jul 2003 11:06:45 +0800, in comp.lang.c , "Lu"
<jx**@fudan.edu.cn> 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 =---
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nobody | last post by:
Hi All, In my application I have the following implementation for ClassA and ClassC invoking a call on ClassB. void ClassA::DoSomething() { int value = m_pClassB->Read(enum1); }
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
0
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user...
6
by: candy | last post by:
hi all, I just want to know that whether the C header files( like stdio.h,etc which the compiler provides) just contains the function declarations or they also contain some additionalinformation...
0
by: gilad | last post by:
If you have a C/C++ source that defines a global variable and compiles as a DLL, what are the ramifications when the exported functions are called from a language like C#, especially in regard to...
4
by: C_Programmer | last post by:
Question#1: ========== ========== What is the default value for any Static Global variable? Example: A.c: =====
2
by: yoavip | last post by:
Hi, I'm using VC++ 6.0, and I have the following problem: when I change a header file, and compile, the compiler does not recognize the change, and does not compile the files that use this...
9
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.