This is an example of some C code that will be used to review name scopes in C and to look at where in a process's virtual memory segments the various parts of a C program will be represented. Although there is some freedom in a compiler's implementation of the details here, most are fairly well determined and consistant across compilers. C has four types of "names" for functions and memory, each type indicates both: 1) in which virtual memory segment their "representation" will be 2) the scope of the name. Functions Memory Name Scope (variables, arrays, Name Type structures ...) __________________ _________ __________________ _______ external x-text x-static Program static external x-text x-static File internal automatic x-stack Block internal static x-static Block There are three scopes that names can have: Program - visable in any function, in any file. File - visable only in any function in the same file where the name is defined. Block - visable only in the block where the name is defined. Note in C, you can only define variables at the top of a block, before any. executable statements. Names are only valid after they are declared/defined in a file, from top to bottom. The C Programming Style Standard already presented, has a strict specification of where declarations and definitions should be placed, and it also specifies that no implicit declarations should be used! 1 /* 2 * File: sample.c 3 * Used to consider scopes in C and mapping of 4 * a program to a process. 5 */ 6 #include 7 #define ATUQ 42 8 int f(char a); /* Program - declaration */ 9 static int g(char b); /* File - declaration */ 10 extern int x; /* File - declaration */ 11 int r[ATUQ]; /* Program/external */ 12 static int s[ATUQ]; /* File/extenal static */ 13 static char *v0="2001.a"; /* File/extenal static */ 14 static char v1[]="2001.a"; /* File/extenal static */ 15 static char v2[42]="2001.a"; /* File/extenal static */ 16 17 int main(argc, argv) /* Program/external */ 18 int argc; /* Block/automatic-arguement */ 19 char *argv[]; /* Block/automatic-arguement */ 20 { 21 int i; /* Block/automatic */ 22 int j[1000]; /* Block/automatic */ 23 int *pj; /* Block/automatic */ 24 int *p; /* Block/automatic */ 25 char r; /* Block/automatic, hides global r!*/ 26 static char q[ATUQ]; /* Block/static */ 27 i = argc * 72.1; 28 pj = j; 29 p = malloc(123); 30 if( p == NULL ) { 31 fprintf(stderr,"ERROR malloc failure\n"); 32 exit(1); 33 } ... Here is a Table showing the type of each statement above Pre - C Preprocessor Directive (should "never" allocate space)( Dec - C Declaration (will never allocate space) Def - C Definition (will always allocate space) and where each statement in the program above is "resprsented" in the virtual memory of a process running the program. Virtual Memory Area ----------- ------------------------------------------------- Pre Dec Def Text Static-RO Statis-RW Heap Stack ___ ___ ___ _________ _________ _________ _________ _________ 6 x 7 x 8 x-f() 9 x-g() 10 x-x 11 x-r 4*42 12 x-s 4*42 13 x-v0 7? 4 14 x-v1 7? 4 15 x-v2 42 16 17 x-main() n? 4+4 18 x-argc 19 x-argv 20 21 x-i 4 22 x-j 4*1000 23 x-pj 4 24 x-p 4 25 x-r 1(4?) 26 x-q 42 27 x n? 8?(72.1) 28 x n? 29 x n? 4?(123) 123 30 x n? 31 x n? 22?("Error...") 32 x n? 33 x 0? Notes Commnets, preprocessor directives, and declarations never "require space" in the virtual memory image of a process "running" the program. We are assuming a 32 bit representation where: int -- 4 byets pointer -- 4 bytes "strings" -- in "abcd" a trailing '/0' is automatically added ("abcd" requires 5 bytes). 13-14 7? might be in Static-RW 17... (and others) n? an unknown number of machine instruction words. 25 1(4?) although only a byte is needed, on many architectures/compilers a full word will be assigned to preserve allignemnt. 27 8? the literal (double constant 72.1) might be in any of the text, static-RO or static-RW areas (architecture/compiler dependent). 29 4? the literal (integer constant 123) might be in any of the text, static-RO or static-RW areas (architecture/compiler dependent). 30 22? the literal (string constant "ERROR malloc failure\n") might be in any of the text, static-RO or static-RW areas (architecture/compiler dependent). 33 0? for an if without an "else block", there may be no code needed for the if construct at the end of the "if block". If there is an "else block", then some machine code would typically be used at the end of the "if block" to jump past the "else block". Exercise typedefs do not fit into the C primitives discussed so far. How should they be fit in wrt locations in memory alocated and scope?