Storage categories affect how information can be shared across files.
C++ has 4 separate schemes for storing data:
Automatic storage duration:
Variables declared inside a function definition.
Created when program execution enters the function or block.
Freed from memory when execution leaves the function or block.
Static storage duration:
Variables defined outside a function definition or using the keyword static.
Persist for the entire time a program is running.
Thread storage duration(under C++ 11):
Variables declared with the keyword thread_local.
Concurrent programming.
Dynamic storage duration:
Memory allocated by the new operator persists until freed with delete operator or the program ends, whichever comes first.
Scope and Linkage
Automatic Storage Duration
Function parameters and variables declared inside a function have automatic storage duration by default.
Automatic storage duration means variables can only be seen inside a block or function.
variables with the same name, the new definition inside the block will hide the prior definition.
prior definition comes out after block or function ends.
intmain(){int a =5;{int b =6;// b only valid in the blockint a =7;// a is 7 inside the block}...// a is 5 and b does not exist...return0}
To manage automatic variables, C++ uses the Stack, following last-in-first-out
Static Duration Variables
Three types:
external linkage
int global =100;
internal linkage
staticint one_file =10;
no linkage
voidfunc1(int n){staticint count =0;}
Notes:
global can be used in this file and other linked files.
one_file can only be used in this file.
count is defined inside the block and only used by func1. It is like automatic variables but it lasts in the memory longer.
All types of static duration variables last for the entire duration of the program.
Compiler does not need any special tools to manage static duration variables and just to use a fixed block of memory to hold all of them. If no explicit declaration, the compiler sets it to 0.
Initialization: zero-initialization by default.
#include<cmath>int x;// zero-initint y=5;// constant-expression initlong z=13*13;// constant-expression initint w =2*sizeof(long)+1;// constant-expression initconstdouble pi =4.0*atan(1.0);// dynamic init, because of callin atan()
Static Duration, External Linkage
External Variable:
variables with external linkage.
defined above main() function or in header file.
also called global variable, compared with automatic variables (local variable)
One Definition Rule
there can be only one definition of a variable.
defining declaration
it causes storage for the variable to be allocated.
referencing declaration
not cause storage to be allocated and only refers to an existing variable.
referencing declaration: extern:
double up;// definition, up is 0externint blem;// blem is defined elsewhere.externchar gr ='z';// definition (initialized)
Static Duration, Internal Linkage
static modifier: file-scope variable with internal linkage.
// file 1int error =20;// external declaration// file 2staticint error =5;// known only in file 2// wrong!!!int error =5;// violate one definition rule
Placement new: Dynamic Allocation
new & delete
The compiler uses 3 separate memory chunks:
static variable
automatic variable
dynamic storage
Namespaces
Create Named Namespace
The same name defined in 2 namespace will not conflict with each other
namespace Jack {double pail;// variable declarationvoidfetch();// function declarationint pal;// variable declarationstructWell{...};// struct declaration}namespace Jill {doublebucket(double n){...}double fetch;int pal;structHill{...};}
Name declared inside a namespace has external linkage by default.
using Declaration and using Directives
using Jill::fetch;// declarationusingnamespace Jill;// directive
using declaration: make a single name available
using directives: make all names inside the namespace available.
It is safer to using declaration.
namespace Jill {doublebucket(dounle n){...}double fetch;structHill{...}}char fetch;// global namespaceintmain(){usingnamespace Jill;// import all namespace names
Hill Thrill;// create a type Jill::Hilldouble fetch;// hide Jill::fetch// local fetch
cin >> fetch;// global fetch
cin >>::fetch;// Jill::fetch
cin >> Jill::fetch;}
DON’T use using directives in the header file !!!
the order of the header files may affect behavior!!!