A Declared Variable Is Not Created/allocated: Definition ! Initialization (Part of Definition)
A Declared Variable Is Not Created/allocated: Definition ! Initialization (Part of Definition)
Definition of a variable
Declaration :
- States the type + name of the variable
- Doesn't automatically allocate storage
=> A Declared Variable is not created/allocated
Definition :
- Causes storage -memory- to be allocated by compiler
- Initializes allocated memory with a value
=> A Defined Variable is created/allocated
-- <type> <varName>;
=> [declaration]
=> no memory allocated
-- <type> <varName>;
[varName used i.e. varName++];
=> [declaration] + [implicit definition : variable
used]
=> memory allocated implicitly
=> memory initialized implicitly with garbage
(previous content of allocated memory)
[usage requires allocation to manipulate variable]
b) External/Global variable : (defined outside of any block of data)
-- <type> <varName>;
=> [definition]
=> memory allocated explicitly
memory initialized implicitly with 0s
2) Variables
* Come into existence when the function is called, and disappear/are destroyed when the function
is exited
Do not retain their value from one function call to another
=> Lifetime = Function execution
* Can be accessed only inside the block where it was declared
=> Scope/Visibility = Inside block (None)
* Are allocated on the Stack [Part of the Process], at execution-time
* Don't have a label
* Come into existence from the start of the Program Execution, remain in existence permanently
until
the program exits
Retain their values between multiple functions that access/use them
=> Lifetime = Program execution
* With 'static' attribute : Accessible by name, usable and redefinable by only functions in the
same module
=> Scope/Visibility with static attribute = Internal
* Without 'static' attribute : Accessible by name, usable and redefinable by:
* all function in the same module
* all functions in external modules provided they are declared in them using extern
declaration
=> Scope/Visibility without static attribute = External
* Are allocated and stored in data section [part of the Executable File], at compilation-time
* Have a label
* Defined exactly once outside of any function (extern definition), in a module (source file, not
header)
(either initialized explicitly or implicitly)
-> <type> <varName>; or <type> <varName>= <value>;
=> Storage allocated + initialization + label defined (= varName)
1-Gather all Global Variables Extern declarations in a separate file (called header, .h, .hpp)
2-Define these Global variables once in exactly one module - source file -(init explicitly or
implicitly)
3-Include the header into all modules that need it
4-Use the Global Variables directly in functions (Implicit declaration)
3) Functions
- In C, all functions are external by default (unless static attribute used)
=> Scope/Visibility = External (no static attribute)
(accessible to all modules, provided they use extern declaration
otherwise => Implicit declaration warning)
Internal (static attribute)
(accessible only inside the module)
=> Lifetime = Program Execution
4) Storage Classes in C :
* auto
- Default storage class for local variables
- Can only be used with Local Variables - within blocks {} : i.e. inside functions
-> Local variable var1 declared + defined (see 1-a) inside func1 using auto
(following is done by compiler)
* is Created/Allocated only when func1 is called
is Destroyed/Deallocated only when func1 exists
=> Lifetime = Function Execution
* is Allocated/Stored in Program's Stack Section/Segment
in Memory (RAM) at Execution-Time : Automatic Allocation
* Has a memory location
=> can be referenced using & to access location
* is Visible only inside func1
=> Scope/Visibility = None, inside block
* If func1 is called multiple times, var1 doesn't keep its value between
the calls : it is recreated and reinitialized at each function
call
* register
- Can only be used with Local Variables - within blocks {} : i.e. inside functions
- Can only be used for variables that have a maximum size = register size
- Usually used for variables that require quick access
-> Local variable var1 declared + defined (see 1-a) inside func1 using register
(following is done by compiler)
* is Created/Allocated only when func1 is called
is Destroyed/Deallocated only when func1 exists
=> Lifetime = Function Execution
* hints the compiler to Allocate/Store/Cache the variable in a register
at Execution-Time (not an obligation) i.e. mov ebx, <value>
* Doesn't have a memory location
=> cannot be referenced using & to access location
* is Visible only inside func1
=> Scope/Visibility = None, inside block
* If func1 is called multiple times, var1 doesn't keep its value
between the calls : it is recreated and reinitialized at each
function call
* static
- Can be used for Local Variables, Global Variables and Functions
Static Allocation : variable occupies the same memory location throughout all
program's execution (in data section/segment),
determined at Compilation
variable keeps its value between multiple function calls : not
reinitialized at each call
* Static Function
-- is Created/Allocated at Program Execution
is Destroyed/Deallocated when all the program terminates
=> Lifetime = Program Execution
-- is Allocated/Stored in Program's Text Section/Segment at Compilation-Time
-- is Visible only inside the module (source file) where it was decl.
=> Scope/Visibility = Internal
(inside the module only, invisible to external modules)
* extern (see 2-b)
- Can be used for Global Variables and Functions
- Used in "extern declaration" syntax
* extern Function :
-- called 'extern declaration'
-- used in a module to declare a function that has been defined in another
module
-- informs that the function has been defined elsewhere
-- doesn't allocate memory for the function
(memory already allocated by the definition of function)
5) Symbols :
= Names of Variables + Functions in a Module (Source File -> Assembly File -> Relocatable Obj. File)
-> Global Symbol => Visible to all modules
-Global Variables without static attribute
-Functions without static attribute
-> Local Symbol => Visible only inside the module itself
-Static { Global Variables + Local Variables }
-Static Functions
3 types of Symbols :
-> C Functions without 'static' attribute are visible to external modules (who use extern
decl.)
=> in .text section or _TEXT SEGMENT
using .globl or PUBLIC directive
Function Name as a Label:
-> C Global Variables without 'static' attribute are visible to external modules (who use
extern de)
-> C Functions with 'static' attribute are visible only inside the module itself
=> in .text section or _TEXT SEGMENT
no use of .globl or PUBLIC directives
Function Name as a Label:
-> C Global Variables with 'static' attribute are visible only inside the module itself
-> C Local Variables with 'static' attribute are allocated for the whole program
execution
---> Without 'const' attribute
N.B : - 'Non-static' Local Variables (to a Static and Non-Static Func) (initialized
and not) are allocated in the Stack and never in data section
=> Can be referenced by any other module, provided it uses extern declaration
All these symbols will be allocated memory locations by linker and other tools in Text or Data section of the
resulting binary
6) Sections in Assembly File (.s, .asm...)
- Depend on the :
- Compiler Driver in use (i.e. Assembler)
- File Format in use (ELF for Unix-Like and BSD-Like OSes, PE for Windows, Mach-O for Mac-OS X)
Additional sections :
* Symbol Table Section (.symtab) => Contains information about all symbols in the Module
* Common (.comm/COMM) Section => Contains symbols created with .comm or COMM directives
* Named sections => Sections created using .section directive
(i.e. .rodata, CONST SEGMENT and other sections)
- Sections are described in the Section Header Table of Assembly File (.s, .asm)
- Depend on the :
- Compiler Driver in use (i.e. Assembler)
- File Format in use (ELF for Unix-Like and BSD-Like OSes, PE for Windows, Mach-O for Mac-OS X)
- Same sections as in the corresponding assembly file
<=> same content of the Assembly file, but in binary form
Sections : exist before linking, in each module (Assembly File : in assembly language / Relocatable Object
File : in binary)
Each Section has a Section Header
All the Sections Headers form the Section Header Table
+-------------------+
| ELF header |---+
+---------> +-------------------+ | e_shoff
| | |<--+
| Section | Section header 0 |
| | |---+ sh_offset
| Header +-------------------+ |
| | Section header 1 |---|--+ sh_offset
| Table +-------------------+ | |
| | Section header 2 |---|--|--+
+---------> +-------------------+ | | |
| Section 0 |<--+ | |
+-------------------+ | | sh_offset
| Section 1 |<-----+ |
+-------------------+ |
| Section 2 |<--------+
+-------------------+
- All sections are organized into 3 loadable segments which make up the Relocatable Object File
-> Text Segment
-> Data Segment
-> BSS Segment
Each segment contains 1 or more sections
* Stack Segment:
* Heap Segment :