Transcript of Storage & Linkage: Effects on Scope Rudra Dutta CSC 230 - Spring 2007, Section 001.
- Slide 1
- Storage & Linkage: Effects on Scope Rudra Dutta CSC 230 -
Spring 2007, Section 001
- Slide 2
- Copyright Rudra Dutta, NCSU, Spring, 20072 Main Ideas Storage
Variables all have to be stored somewhere Linkage What parts of
entities in a compilation unit (file) is accessible to others which
are linked together Scope At any point, what variables are visible
and accessible? Connected to the runtime issue of lifetime Each of
the above creates categories Unfortunately, there is some overlap
between them Need to think of memory to appreciate
- Slide 3
- Copyright Rudra Dutta, NCSU, Spring, 20073 Storage Where the
memory is allocated Two keywords: auto and register auto is default
However, even auto variables go on either Stack, or Heap Where do
global variables go? Programmer need not care On the stack, but
never goes away - process lifetime
- Slide 4
- Copyright Rudra Dutta, NCSU, Spring, 20074 Scope - Basics
Global variables - outside all functions Is accessible from all
functions within compilation unit Local variable - within a
function Just after function begins - most usual place Accesssible
only inside function Separate variable from any global by same name
Variables can be local to a block BUT not inside statement (C99
usage is exception) More specific variable always hides less
specific Variables only visible AFTER definition Issue of multiple
files - later (linkage)
- Slide 5
- Copyright Rudra Dutta, NCSU, Spring, 20075 Linkage What parts
of one compilation unit can another see/use? External and internal
linkage Linker will link object code produced by compiler Compiler
leaves hooks or links for linker All functions, all global
variables visible from another compilation unit Clearly, not local
variables BUT, compiler has to be able to compile current unit
Undefined functions are always assumed to exist somewhere
Optimistic approach - linker will find Bad practice - should
declare explicitly Undefined variables are treated pessimistically
Must declare explicitly, otherwise stops compiler Keyword extern
allows variable declarations extern int i; The variable i is a
global variable, defined in some other place
- Slide 6
- Copyright Rudra Dutta, NCSU, Spring, 20076 Funny Things 1. What
happens? #include int i; int main() { i = 10; do_something();
printf ("%d\n", i); return (0); } #include int i; int
do_something() { while (i-=1){ /* some code */ } return (0); }
Consider what happens to the memory/storage
- Slide 7
- Copyright Rudra Dutta, NCSU, Spring, 20077 Funny Things 2. What
happens? #include int i; int main() { i = 10; do_something();
printf ("%d\n", i); return (0); } #include extern int i; int
do_something() { while (i-=1){ /* some code */ } return (0); }
Consider what happens to the memory/storage
- Slide 8
- Copyright Rudra Dutta, NCSU, Spring, 20078 Funny Things 3. What
happens? #include int i; int main() { i = 10; do_something();
printf ("%d\n", i); return (0); } #include int do_something() {
while (i-=1){ /* some code */ } return (0); } Consider what happens
to the memory/storage
- Slide 9
- Copyright Rudra Dutta, NCSU, Spring, 20079 Funny Things 4. What
happens? #include extern int i; int main() { i = 10;
do_something(); printf ("%d\n", i); return (0); } #include extern
int i; int do_something() { while (i-=1){ /* some code */ } return
(0); } Consider what happens to the memory/storage
- Slide 10
- Copyright Rudra Dutta, NCSU, Spring, 200710 Funny Things 5.
What happens? Consider what happens to the memory/storage #include
int i = 20; /* call this iG */ int main() { int i; /* call this iM
*/ i = 10; { int i; /* call this iB */ printf ("A: %d\n", i=50); }
printf ("B: %d\n", i); do_something (); } int do_something () {
printf ("C: %d\n", i); }
- Slide 11
- Copyright Rudra Dutta, NCSU, Spring, 200711 Funny Things 6.
What happens? Consider what happens to the memory/storage #include
int i = 20; /* call this iG */ int main() { int i; /* call this iM
*/ i = 10; { extern int i; /* call this iB */ printf ("A: %d\n",
i=50); } printf ("B: %d\n", i); do_something (); } int do_something
() { printf ("C: %d\n", i); }
- Slide 12
- Copyright Rudra Dutta, NCSU, Spring, 200712 Funny Things 7.
What happens? #include int i = 20; /* call this iG */ int main() {
extern int i; /* call this iM */ i = 10; { int i; /* call this iB
*/ printf ("A: %d\n", i=50); } printf ("B: %d\n", i); do_something
(); } int do_something () { printf ("C: %d\n", i); } Consider what
happens to the memory/storage
- Slide 13
- Copyright Rudra Dutta, NCSU, Spring, 200713 Funny Things 8.
What happens? #include extern int i = 20; /* call this iG */ int
main() { int i; /* call this iM */ i = 10; { int i; /* call this iB
*/ printf ("A: %d\n", i=50); } printf ("B: %d\n", i); do_something
(); } int do_something () { printf ("C: %d\n", i); } Consider what
happens to the memory/storage
- Slide 14
- Copyright Rudra Dutta, NCSU, Spring, 200714 Funny Things 9.
What happens? #include int main() { i = 10; { int i; /* call this
iB */ printf ("A: %d\n", i=50); } printf ("B: %d\n", i);
do_something (); } int i; /* call this iG */ int do_something () {
printf ("C: %d\n", i); } Consider what happens to the
memory/storage extern int i;
- Slide 15
- Copyright Rudra Dutta, NCSU, Spring, 200715 Summary of External
Variables Variable Definition Global automatically has external
linkage Local automatically has internal linkage Variable
Declaration Use keyword extern Finds global variable defined in
another file Or same file Before, or after Declared variable has
scope Determined by position of declaration inside compilation unit
Can NOT use declaration to pull in local variables of another
compilation unit Cannot override scope int i; int j; do_thing() {
int k; } extern int i;
- Slide 16
- Copyright Rudra Dutta, NCSU, Spring, 200716 Persistence of
Storage Usually, scope (file) determines lifetime (process) All
local variables have Scope only within block e.g. function Lifetime
only while the block code e.g. function call is on the stack The
keyword static creates persistent storage Storage becomes like
global - with process lifetime BUT scope remains the same Variables
retain value between visits to block Where is storage? Programmer
need not know
- Slide 17
- Copyright Rudra Dutta, NCSU, Spring, 200717 Effect on Linkage
static also changes linkage to internal Local variables were
internal to begin with Global variables (external) are made
internal to the file An extern declaration from another file will
fail to find it This also applies to functions Can create functions
that are private to a file
- Slide 18
- Copyright Rudra Dutta, NCSU, Spring, 200718 Initialization An
initialization statement is a definition and an assignment
statement combined Automatic initialization: If only definition is
present, Global and static variables automatically initialized to
zero Local variables initialized to nothing in particular Concept
of garbage Initialization occurs only when variable (storage) is
created Therefore only once for static variables
- Slide 19
- Copyright Rudra Dutta, NCSU, Spring, 200719 Read: Section 1.10
Sections 4.3, 4.4, 4.6, 4.7, 4.9