|
|
|
MAIN CALLS PROCEDURE
Use of STATIC
Example 1![](../mainProc_1.gif)
|
|
|
|
|
|
|
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i after return from myadd();i=main_stat;?
Example 2
![](../mainProc_2.gif)
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i after return from myadd();i=main_stat;?
![](../mainProc_2_Watch2.gif)
Example 3
![](../mainProc_3.gif)
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i after return from myadd();i=main_stat;?
![](../mainProc_3_Watch3.gif)
CONCLUSION:
internal static variables provide
private, permanent storage
within a single function.
Example 4
![](../mainProc_4.gif)
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i,j after return from myadd(); i=main_stat;?
![](../mainProc_4_Watch4.gif)
Example 5
![](mainProc_5.gif)
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i,j, main_stat after
return from myadd(); i=main_stat; ?
![](mainProc_5_Watch5.gif)
Main and Procedure are in different files
Example L1
Main is in file
main.ccp
![](mainDiff1.gif)
Will it compile without errors?
YES!
Myaad.ccp is given in a separate file
![](MyAddDiff1.gif)
Will it compile without errors?
YES!
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i,j, main_stat after
return from myadd(); i=main_stat; ?
Click to see the ouput window in .NET.
Example L2
We can fix it! We leave the main.cpp unchanged and add extern to MyAdd.cpp file:
![](mainDiff1.gif)
and in a separate file the procedure MYADD() we add extern:
![](MyAddDiff2.gif)
Will the code compile and run with no errors?
If yes, then WHAT is the value of variable i,j, main_stat after
return from myadd(); i=main_stat; ?
CLICK HERE TO SEE !
IN DEBUG MODE
NOTE: extern keyword in C++ (or C) declares a variables without
allocating memory (without defininig it!).
It is assumed that the variable will be defined somewhere else.
Initialization of the extern int i= 3;
will allocate memory. any other definition of the same variable in same program (in different files)will result in an error.
Q.: who will detect the error? compiler, linker, processor at run-time,
good programmer, average programmer.
main_stat is a global variable.
Example L2
static local variable
is used when the value of that variable must
persists across different invocations.
Question: why regular local variables can not be used?
answer: static local variables
funct()
{
....
static int variable = -1;
.....
}
Example L2
GLOBAL name
- is defined in one file and used in multiple files
static GLOBAL name
- is used when we want the value of that variable be
invisible ( private to) the file in which it is defined.
(S.B. Lippman, C++ Primer, page 125.)
So there is no NAME conflict if it is used in other files!!
Question: why regular ( not static) variables can not be used?
answer: Please look previous example L2.
Lets look into the next example:
Example L 3
Now we want to make the int main_stat private to the main.cpp file only. Can we do it??? In other words can we make main_stat invisible to the precedure myaddd() which is in another file???
YES!!! we can!
static int main_stat
in The file
MyMain.cpp
![](linker3MAIN_3.gif)
Now we try to use it in function mayadd() which is in the file MyAdd.cpp
![](linkerError_3_Myadd.gif)
Now we ask the same question
Will the code compile, link and run with no errors?
If yes, then WHAT is the value of variable i,j, main_stat after
return from myadd(); i=main_stat; ?
Example L4
To fix the Linker problem: remove extern in MyAdd.cpp file
![](linker4inMyadd.gif)
Now Compile and Link the Main.cpp
![](linker4inMAIN.gif)
Will the code compile, link and run with no errors?
If yes, then WHAT is the value of variable i,j, main_stat after
return from myadd(); i=main_stat; ?
CLICK HERE!