Memory Models and Namespaces
Separate Compilation of Programs
- C++ allows and encourages you to locate component functions of a program in separate files.
- Compile files separately and then link them into the final executable program.
- header files:
- function prototypes
- symbolic constants defined using
#define or const - structure declarations: no variables created
- class declaration
- template declaration
- inline function
- How to include header file ?
- use "coordin.h"
- NOT use <coordin.h>
< > : C++ looks at system’s file system" " : C++ first looks at current working directory or source code director, after that, looks at system’s file. - Example of header file
#ifndef COORDIN_H_
#define COORDIN_H_
...
#endif
Storage duration, Scope, and Linkage
Storage Duration
- 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.
int main()
{
int a = 5;
{
int b = 6;
int a = 7;
}
...
...
return 0
}
- To manage automatic variables, C++ uses the Stack, following last-in-first-out
Static Duration Variables
#include <cmath>
int x;
int y=5;
long z=13*13;
int w = 2 * sizeof(long) +1;
const double pi = 4.0 * atan(1.0);
Static Duration, External Linkage
Static Duration, Internal Linkage
static modifier: file-scope variable with internal linkage.
int error = 20;
static int error = 5;
int error = 5;
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;
void fetch();
int pal;
struct Well {...};
}
namespace Jill {
double bucket (double n) {...}
double fetch;
int pal;
struct Hill {...};
}
- Name declared inside a namespace has external linkage by default.
using Declaration and using Directives
using Jill::fetch;
using namespace Jill;
using declaration: make a single name availableusing directives: make all names inside the namespace available.- It is safer to
using declaration.
namespace Jill {
double bucket(dounle n) {...}
double fetch;
struct Hill {...}
}
char fetch;
int main() {
using namespace Jill;
Hill Thrill;
double fetch;
cin >> fetch;
cin >> ::fetch;
cin >> Jill::fetch;
}
- DON’T use
using directives in the header file !!!
- the order of the header files may affect behavior!!!
|