Modules and variable scope

geacc022%timevx at CIT-HAMLET.ARPA geacc022%timevx at CIT-HAMLET.ARPA
Wed May 29 08:23:54 AEST 1985

    I'm a little confused about how adding 'modules' to C would stop
programs from accessing global data they really weren't supposed to
access.  You can't stop global data from being accessed, only hide
it hoping the accessor doesn't know where it is.

    If we're talking about pascal's "import" and "export" (or
"interface" and "implementation"), then C already has it:

extern int his_global;		/* to import a global variable	*/
       int my_global;		/* to export a global variable	*/
static int my_local;		/* to declare a local var.	*/

    In fact, this is more versatile since you only declare the
variables you use.  But nothing can stop a C program from declaring
and using any global data it wants to.

    The example commonly given is programs that know about the
structure of _iob.  The reason that _iob must be global is so that
MACROS like getchar(), putchar(), fileno(), etc. can access it.  If
we replace the macros with the appropriate function calls, there is
no need to declare _iob in stdio.h and, if all the functions that use
it are in one file, it could be static within that file.  If it is
external, then any program that wants to know about the structure of
_iob can do so.  (I agree that it's a bad idea, for several reasons).

			Gary Ansok

"All the world loves a straight man."

More information about the Comp.lang.c mailing list