Storage Linkage Effects on Scope Rudra Dutta CSC
- Slides: 19
Storage & Linkage: Effects on Scope Rudra Dutta CSC 230 - Spring 2007, Section 001 Copyright Rudra Dutta, NCSU, Spring, 2007
Main Ideas l Storage – l Linkage – l Variables all have to be stored some“where” 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 – l Each of the above creates categories Unfortunately, there is some overlap between them – Need to think of memory to appreciate – Copyright Rudra Dutta, NCSU, Spring, 2007 2
Storage Where the memory is allocated l Two keywords: l auto and register – auto is default – l However, even auto variables go on either Stack, or – Heap – l Where do global variables go? Programmer need not care – “On the stack, but never goes away” - process lifetime – Copyright Rudra Dutta, NCSU, Spring, 2007 3
Scope - Basics l Global variables - outside all functions – l 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 – l Variables can be local to a block – BUT not inside statement (C 99 usage is exception) More specific variable always “hides” less specific l Variables only visible AFTER definition l Issue of multiple files - later (linkage) l Copyright Rudra Dutta, NCSU, Spring, 2007 4
Linkage l What parts of one compilation unit can another see/use? – l 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 – l l 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” l Bad practice - should declare explicitly l – Undefined variables are treated pessimistically l l 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” – Copyright Rudra Dutta, NCSU, Spring, 2007 5
Funny Things 1. What happens? #include <stdio. h> int i; int main() int do_something() { { while (i-=1)�{ i = 10; /* some code */ do_something(); printf ("%dn", i); } return (0); } Copyright Rudra Dutta, NCSU, Spring, 2007 } Consider what happens to the memory/storage 6
Funny Things 2. What happens? #include <stdio. h> int i; extern int i; int main() int do_something() { { while (i-=1)�{ i = 10; /* some code */ do_something(); printf ("%dn", i); } return (0); } Copyright Rudra Dutta, NCSU, Spring, 2007 } Consider what happens to the memory/storage 7
Funny Things 3. What happens? #include <stdio. h> int i; int do_something() { int main() while (i-=1)�{ { /* some code */ i = 10; do_something(); } printf ("%dn", i); return (0); } } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 8
Funny Things 4. What happens? #include <stdio. h> extern int i; int main() int do_something() { { while (i-=1)�{ i = 10; /* some code */ do_something(); printf ("%dn", i); } return (0); } Copyright Rudra Dutta, NCSU, Spring, 2007 } Consider what happens to the memory/storage 9
Funny Things 5. What happens? #include <stdio. h> int i = 20; /* call this i. G */ int main() { int i; /* call this i. M */ i = 10; { int i; /* call this i. B */ printf ("A: %dn", i=50); } printf ("B: %dn", i); do_something (); } int do_something () { printf ("C: %dn", i); } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 10
Funny Things 6. What happens? #include <stdio. h> int i = 20; /* call this i. G */ int main() { int i; /* call this i. M */ i = 10; { extern int i; /* call this i. B */ printf ("A: %dn", i=50); } printf ("B: %dn", i); do_something (); } int do_something () { printf ("C: %dn", i); } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 11
Funny Things 7. What happens? #include <stdio. h> int i = 20; /* call this i. G */ int main() { extern int i; /* call this i. M */ i = 10; { int i; /* call this i. B */ printf ("A: %dn", i=50); } printf ("B: %dn", i); do_something (); } int do_something () { printf ("C: %dn", i); } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 12
Funny Things 8. What happens? #include <stdio. h> extern int i = 20; /* call this i. G */ int main() { int i; /* call this i. M */ i = 10; { int i; /* call this i. B */ printf ("A: %dn", i=50); } printf ("B: %dn", i); do_something (); } int do_something () { printf ("C: %dn", i); } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 13
Funny Things 9. What happens? #include <stdio. h> extern int i; int main() { i = 10; { int i; /* call this i. B */ printf ("A: %dn", i=50); } printf ("B: %dn", i); do_something (); } int i; /* call this i. G */ int do_something () { printf ("C: %dn", i); } Copyright Rudra Dutta, NCSU, Spring, 2007 Consider what happens to the memory/storage 14
Summary of External Variables l Variable Definition l Use keyword extern – Finds global variable defined in another file – Or same file – Global automatically has external linkage – Local automatically has internal linkage – extern int i; �� int i; int j; do_thing() { int k; } Copyright Rudra Dutta, NCSU, Spring, 2007 Variable Declaration l l Declared variable has scope – l Before, or after Determined by position of declaration inside compilation unit Can NOT use declaration to pull in local variables of another compilation unit – Cannot override scope 15
Persistence of Storage Usually, scope (file) determines lifetime (process) l All local variables have l Scope only within block e. g. function – Lifetime only while the block code e. g. function call is on the stack – l 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 – Copyright Rudra Dutta, NCSU, Spring, 2007 16
Effect on Linkage l 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 – l This also applies to functions – Can create functions that are private to a file Copyright Rudra Dutta, NCSU, Spring, 2007 17
Initialization An initialization statement is a definition and an assignment statement combined l Automatic initialization: l If only definition is present, – Global and static variables automatically initialized to zero – Local variables initialized to nothing in particular – Concept of garbage – l Initialization occurs only when variable (storage) is created – Therefore only once for static variables Copyright Rudra Dutta, NCSU, Spring, 2007 18
Read: Section 1. 10 l Sections 4. 3, 4. 4, 4. 6, 4. 7, 4. 9 l Copyright Rudra Dutta, NCSU, Spring, 2007 19
- Design of an absolute loader
- Ahb hprot
- Niloy dutta uconn
- Dipanwita dutta
- Geeta dutta wsu
- Dr s k dutta
- Anup dutta hcl
- Partha dutta rpi
- Keshav sita memorial foundation trust
- Type of tilak
- Bhawana rudra
- Rudra environmental solutions
- Secondary storage vs primary storage
- Primary storage and secondary storage
- Unified storage vs traditional storage
- Secondary storage provides temporary or volatile storage
- Scope management pmp
- Io2br3
- How is a linkage map related to crossovers
- Sex linkage