Engineering Science 1036A/B Lecture Notes - Lecture 13: Static Variable, Function Overloading, Global Variable
Data Storage and Memory Diagram
Four sections of the memory areas store four different types data:
Stack
Stores Local variables and formal parameters
§
Managed by the compiler
§
○
Heap
Stores dynamic variables
§
Managed by storage allocator
§
○
Global Data
Stores global variables
§
○
Program code
Stores other program data
§
○
•
Scope
Object/Variable Scope
Scope (visibility): region of the program in which an object/variable can be legally used or
visible
○
•
Local Scope/Visibility (Block Scope)
Scope of an object that is declared inside a block (includes function parameters)•
Extends from the point of declaration to the end of the block•
Can be accessed only within the block that declares it•
This includes:
An object declared within ‘{‘ and ‘}’
○
An object declared within a function
○
Formal parameters of a function
○
Any parameter declared in the for-header has the scope of that for-structure
○
•
Global Scope (File scope)
Scope of an object that is declared outside of all namespaces, functions (including main
function) and classes
•
Extends from point of declaration to the end of the entire file containing the program
code
•
Can be accessed from any part within the program file•
Any object with global scope is initialized with a default value
zero for numerical ones
○
null for non-numerical ones
○
•
Local variables are also initialized with default values, which is not accepted grammatically•
•
Scope of a Function
Functions are objects too•
They are declared outside main function•
They have global scope only from point of declaration onwards
Use prototypes to get around this limitation
○
•
Storage Class of an Object
Storage class refers to the lifetime of an object•
The lifetime of an object is the time during program execution in which an identifier
actually has memory allocated to it
•
Automatic Storage Class
Default for local variables, their storage is created (allocated) when control enters the
function or a block
•
Local variable are alive while function (or a block) is executing•
Their storage is destroyed (deallocated) when function (or a block) exits •
Static/Global Storage Class
Any object/variable with global scope, its lifetime is the lifetime of the entire program•
Any object/variable with the static modifier/qualifier has a global storage
Its storage remains allocated throughout the execution of the entire program
In this case, these objects/variables are initialized once when their
declarations are encountered
by default this initialized value is 0 for numerical variables and NULL (‘ ’)
for non-numerical variables
□
§
Retain their values when the function returns to its caller
§
does not affect the scope of the variable
§
○
•
•
Static Modifier
The static variable yin function f1 and the static
variable yin function f2 are different from each other
because of their scopes
•
Both of these two variables will be given different spaces
in the RAM with two different flags and there will be no
conflict between these two
•
Accessing the Global Variable
A global variable can be accessed anywhere in the program from the point of declaration•
If a local variable name is same as a global variable name, one can access the global
variable using ::globalVariable
•
The :: operator is known as the unary scope resolution operator
○
○
•
Scope and Storage Tips
It is easy to declare a variable globally once and use it in all functions without re-declaring
it
However, this is a bad practice because it could lead to errors that are hard to debug
○
•
Commonly acceptable to declare a local variable with the same name multiple times in
different no-nesting blocks in a function
•
Not a good practice to declare a local variable twice in nested blocks
The program can compile and run, but it is easy to make mistakes
○
•
Common mistakes in code writing with functions
Misspelled function name in the function call or function header•
Calling a void-function from the cout statement•
No return statement for a non-void function•
Returning a value from a void-function•
Using data-types in the function call•
Function overloading
If two or more functions have the same name but different parameter lists, these
functions are called overloaded functions, and this concept is known as function
overloading
•
In this case, the return-data type can be same or different in the function signature.•
void myFunction(int);
int myFunction(int, double);
double myFunction(string);
void myFunction(void);
Ambiguous Invocation in overloaded functions
Sometimes there may be two or more possible matches for an invocation (call) of an
overloaded function
•
In this case, the compiler cannot determine the most specific match
Referred to as ambiguous invocation
○
•
Results in a compilation error•
Scope & Storage of Objects/Variables
Data Storage and Memory Diagram
Four sections of the memory areas store four different types data:
Stack
Stores Local variables and formal parameters
§
Managed by the compiler
§
○
Heap
Stores dynamic variables
§
Managed by storage allocator
§
○
Global Data
Stores global variables
§
○
Program code
Stores other program data
§
○
•
Scope
Object/Variable Scope
Scope (visibility): region of the program in which an object/variable can be legally used or
visible
○
•
Local Scope/Visibility (Block Scope)
Scope of an object that is declared inside a block (includes function parameters)•
Extends from the point of declaration to the end of the block•
Can be accessed only within the block that declares it•
This includes:
An object declared within ‘{‘ and ‘}’
○
An object declared within a function
○
Formal parameters of a function
○
Any parameter declared in the for-header has the scope of that for-structure
○
•
Global Scope (File scope)
Scope of an object that is declared outside of all namespaces, functions (including main
function) and classes
•
Extends from point of declaration to the end of the entire file containing the program
code
•
Can be accessed from any part within the program file•
Any object with global scope is initialized with a default value
zero for numerical ones
○
null for non-numerical ones
○
•
Local variables are also initialized with default values, which is not accepted grammatically•
•
Scope of a Function
Functions are objects too•
They are declared outside main function•
They have global scope only from point of declaration onwards
Use prototypes to get around this limitation
○
•
Storage Class of an Object
Storage class refers to the lifetime of an object•
The lifetime of an object is the time during program execution in which an identifier
actually has memory allocated to it
•
Automatic Storage Class
Default for local variables, their storage is created (allocated) when control enters the
function or a block
•
Local variable are alive while function (or a block) is executing•
Their storage is destroyed (deallocated) when function (or a block) exits •
Static/Global Storage Class
Any object/variable with global scope, its lifetime is the lifetime of the entire program•
Any object/variable with the static modifier/qualifier has a global storage
Its storage remains allocated throughout the execution of the entire program
In this case, these objects/variables are initialized once when their
declarations are encountered
by default this initialized value is 0 for numerical variables and NULL (‘ ’)
for non-numerical variables
□
§
Retain their values when the function returns to its caller
§
does not affect the scope of the variable
§
○
•
•
Static Modifier
The static variable yin function f1 and the static
variable yin function f2 are different from each other
because of their scopes
•
Both of these two variables will be given different spaces
in the RAM with two different flags and there will be no
conflict between these two
•
Accessing the Global Variable
A global variable can be accessed anywhere in the program from the point of declaration•
If a local variable name is same as a global variable name, one can access the global
variable using ::globalVariable
•
The :: operator is known as the unary scope resolution operator
○
○
•
Scope and Storage Tips
It is easy to declare a variable globally once and use it in all functions without re-declaring
it
However, this is a bad practice because it could lead to errors that are hard to debug
○
•
Commonly acceptable to declare a local variable with the same name multiple times in
different no-nesting blocks in a function
•
Not a good practice to declare a local variable twice in nested blocks
The program can compile and run, but it is easy to make mistakes
○
•
Common mistakes in code writing with functions
Misspelled function name in the function call or function header•
Calling a void-function from the cout statement•
No return statement for a non-void function•
Returning a value from a void-function•
Using data-types in the function call•
Function overloading
If two or more functions have the same name but different parameter lists, these
functions are called overloaded functions, and this concept is known as function
overloading
•
In this case, the return-data type can be same or different in the function signature.•
void myFunction(int);
int myFunction(int, double);
double myFunction(string);
void myFunction(void);
Ambiguous Invocation in overloaded functions
Sometimes there may be two or more possible matches for an invocation (call) of an
overloaded function
•
In this case, the compiler cannot determine the most specific match
Referred to as ambiguous invocation
○
•
Results in a compilation error•
Scope & Storage of Objects/Variables
Data Storage and Memory Diagram
Four sections of the memory areas store four different types data:
Stack
Stores Local variables and formal parameters
§
Managed by the compiler
§
○
Heap
Stores dynamic variables
§
Managed by storage allocator
§
○
Global Data
Stores global variables
§
○
Program code
Stores other program data
§
○
•
Scope
Object/Variable Scope
Scope (visibility): region of the program in which an object/variable can be legally used or
visible
○
•
Local Scope/Visibility (Block Scope)
Scope of an object that is declared inside a block (includes function parameters)
•
Extends from the point of declaration to the end of the block
•
Can be accessed only within the block that declares it
•
This includes:
An object declared within ‘{‘ and ‘}’
○
An object declared within a function
○
Formal parameters of a function
○
Any parameter declared in the for-header has the scope of that for-structure
○
•
Global Scope (File scope)
Scope of an object that is declared outside of all namespaces, functions (including main
function) and classes
•
Extends from point of declaration to the end of the entire file containing the program
code
•
Can be accessed from any part within the program file
•
Any object with global scope is initialized with a default value
zero for numerical ones
○
null for non-numerical ones
○
•
Local variables are also initialized with default values, which is not accepted grammatically
•
•
Scope of a Function
Functions are objects too
•
They are declared outside main function
•
They have global scope only from point of declaration onwards
Use prototypes to get around this limitation
○
•
Storage Class of an Object
Storage class refers to the lifetime of an object•
The lifetime of an object is the time during program execution in which an identifier
actually has memory allocated to it
•
Automatic Storage Class
Default for local variables, their storage is created (allocated) when control enters the
function or a block
•
Local variable are alive while function (or a block) is executing•
Their storage is destroyed (deallocated) when function (or a block) exits •
Static/Global Storage Class
Any object/variable with global scope, its lifetime is the lifetime of the entire program•
Any object/variable with the static modifier/qualifier has a global storage
Its storage remains allocated throughout the execution of the entire program
In this case, these objects/variables are initialized once when their
declarations are encountered
by default this initialized value is 0 for numerical variables and NULL (‘ ’)
for non-numerical variables
□
§
Retain their values when the function returns to its caller
§
does not affect the scope of the variable
§
○
•
•
Static Modifier
The static variable yin function f1 and the static
variable yin function f2 are different from each other
because of their scopes
•
Both of these two variables will be given different spaces
in the RAM with two different flags and there will be no
conflict between these two
•
Accessing the Global Variable
A global variable can be accessed anywhere in the program from the point of declaration•
If a local variable name is same as a global variable name, one can access the global
variable using ::globalVariable
•
The :: operator is known as the unary scope resolution operator
○
○
•
Scope and Storage Tips
It is easy to declare a variable globally once and use it in all functions without re-declaring
it
However, this is a bad practice because it could lead to errors that are hard to debug
○
•
Commonly acceptable to declare a local variable with the same name multiple times in
different no-nesting blocks in a function
•
Not a good practice to declare a local variable twice in nested blocks
The program can compile and run, but it is easy to make mistakes
○
•
Common mistakes in code writing with functions
Misspelled function name in the function call or function header•
Calling a void-function from the cout statement•
No return statement for a non-void function•
Returning a value from a void-function•
Using data-types in the function call•
Function overloading
If two or more functions have the same name but different parameter lists, these
functions are called overloaded functions, and this concept is known as function
overloading
•
In this case, the return-data type can be same or different in the function signature.•
void myFunction(int);
int myFunction(int, double);
double myFunction(string);
void myFunction(void);
Ambiguous Invocation in overloaded functions
Sometimes there may be two or more possible matches for an invocation (call) of an
overloaded function
•
In this case, the compiler cannot determine the most specific match
Referred to as ambiguous invocation
○
•
Results in a compilation error•
Scope & Storage of Objects/Variables
Document Summary
Four sections of the memory areas store four different types data: Scope (visibility): region of the program in which an object/variable can be legally used o visible ed or. Scope of an object that is declared inside a block (includes function parameters) Extends from the point of declaration to the end of the block. Can be accessed only within the block that declares it. Any parameter declared in the for-header has the scope of that for-structure. Scope of an object that is declared outside of all namespaces, functions (including main function) and classes. Extends from point of declaration to the end of the entire file containing the program code. Can be accessed from any part within the program file. Any object with global scope is initialized with a default value zero for numerical ones null for non-numerical ones. Local variables are also initialized with default values, which is not accepted grammaticall. They are declared outside main function ain atically.