C/C++ Programming
Language
CS205 Spring
Feng Zheng
2020.04.09
Content
• Brief Review
• Separate Compilation
• Storage Duration, Scope, and Linkage
Ø Scope and Linkage
Ø 1. Automatic Storage Duration
Ø 2. Static Duration Variables: External, Internal and No Linkage
Ø 3. Storage Schemes and Dynamic Allocation
• Namespaces
• Summary
Brief Review
Content of Last Class
• Adventures in functions
Ø Inline function
Ø Reference variables
Ø Default arguments
Ø Function overloading
Ø Function template
Separate Compilation
Separate Compilation
• C++ allows to locate the component functions of a program in separate
files
Ø Modify one file and then recompile just that one file
Ø Make it easier to manage large programs
Ø Most IDEs provide additional facilities to help with the management (The make
programs in Unix and Linux systems)
• Divide the original program into three parts
Ø A header file that contains the structure (type) declarations and prototypes
for functions that use those structures (types)
Ø A source code file that contains the code that define the structure (type) -
related functions
Ø A source code file that contains the code that calls the structure (type) -
related functions
A Header File
• Shouldn’t put function definitions or variable declarations into a
header file
• Commonly found in header files
Ø Function prototypes
Ø Symbolic constants defined using #define or const (special linkage)
Ø Structure declarations (not variable)
Ø Class declarations
Ø Template declarations (not code to be compiled)
Ø Inline functions (special linkage)
• Don’t add header files to the project list in IDEs
• Don’t use #include to include source code files in other source
code files
Header File Management
• You should include a header file
just once in a file
• Most of the standard C and C++
header files use this guarding
scheme
Storage Duration, Scope,
and Linkage
Storage
• Three plus one separate schemes for storing data
Ø Automatic storage duration
ü Variables declared inside a function definition
ü They are created when program execution enters the function or block
ü The memory used for them is freed when execution leaves the function or block
Ø Static storage duration
ü Using the keyword static to have static storage duration
ü Persist for the entire time a program is running
Ø Dynamic storage duration
ü Memory allocated by the new operator persists until it is freed with the delete
operator or until the program ends
Ø Thread storage duration (C++11)
ü Allow a program to split computations into separate threads
ü Variables declared with the thread_local keyword have storage that persists for as
long as the containing thread lasts
Scope and Linkage
• Scope: describe how widely visible a name is in a file
Ø Local scope: within the block
Ø Global (file) scope: throughout the file after the point
Ø A function prototype scope: within the parentheses
Ø Class scope
Ø Namespace scope
• Linkage: describe how a name can be shared in different units
Ø A name with external linkage can be shared across files
Ø A name with internal linkage can be shared within a single file
Ø Names of automatic variables and static variables within a block have no
linkage
1. Automatic Storage Duration
• Function parameters and
variables declared inside a
function have, by default,
automatic storage duration
• Have local scope and no linkage
1. Two Variables: Automatic and
Register
• Initialization of automatic variables
Ø You can initialize an automatic variable with any expression whose value
will be known when the declaration is reached
• Automatic variables and the stack (first-in-last-out)
Ø Set aside a section of memory and treat it as a stack for managing the
flow and ebb of variables
Ø New data is figuratively stacked atop old data
Ø Remove from the stack when a program is finished with it
• Register Variables
Ø C originally introduced the register keyword to suggest that the compiler
use a CPU register to store an automatic variable
1. Passing Arguments by Using a
Stack
• Remember the address
Ø Stack: first-in-last-out
2. Static Duration Variables
• C++, like C, provides static storage duration variables with three
kinds of linkage
Ø External linkage (accessible across files)
Ø Internal linkage (accessible to functions within a single file)
Ø No linkage (accessible to just one function or to one block)
• Properties of static variables
Ø The number of static variables doesn’t change
Ø Doesn’t need a special device such as a stack to manage them
Ø Allocate a fixed block of memory to hold all the static variables
Ø Stay present as long as the program executes
2. Declaring and Initializing Static
Variables
• Examples of declaration
• Initialization
Ø Zero-initialized,
Ø Constant expression initialization
Ø Dynamic initialization
• What determines which form of initialization takes place?
Ø All static variables are zero-initialized
Ø Do simple calculations if needed
2.1 Static Duration, External
Linkage
• Static variables with external
linkage
Ø Have static storage duration and
file scope
Ø Be defined outside any function
• The one definition rule
Ø Have to be declared in each file
Ø Two kinds of variable declarations
ü Defining declaration
ü Referencing declaration
Ø A referencing declaration uses the
keyword extern
• See program example 1
2.2 Static Duration, Internal
Linkage
• Static variables with internal linkage
Ø Applying the static modifier to a file-scope variable gives it internal linkage
Ø A variable with internal linkage is local to the file that contains it
Ø What if you want to use the same name to denote different variables in
different files?
ü Add codes (int a_int = 1;) in program example 1
• See program example 2
2.3 Static Storage Duration, No
Linkage
• Static variables with no linkage
Ø Applying the static modifier to a variable defined inside a block
Ø Exist even while the block is inactive
Ø Preserve values between function calls
Ø Subsequent calls to the function don’t reinitialize the variable
• See program example 3
Specifiers and Qualifiers
• Specifiers:
Ø auto (eliminated as a specifier in C++11) automatic type deduction
Ø register: make the data access fast
Ø static: file-scope declaration and local declaration
Ø extern: external linkage
Ø thread_local (added by C++11)
Ø mutable: a particular member of a structure (or class) can be altered
even if a particular structure (or class) variable is a const
• CV-Qualifiers (cv stands for const and volatile):
Ø const
Ø volatile: the value in a memory location can be altered even though
nothing in the program code modifies the contents (A pointer to a
hardware location. Or two programs may interact, sharing data)
The Five Kinds of Variable Storage
• Comparison: summarize the storage class features as used in the
pre-namespace era
More Linkage
• Functions
Ø C/C++ does NOT allow you to define one function inside another
Ø External linkage: all functions automatically have static storage
duration
• Function linkage
Ø Use the keyword extern in a function prototype (optional)
ü Check the program example 1 without use of extern
Ø Use the keyword static to give a function internal linkage, confining
its use to a single file
3. Storage Schemes and Dynamic
Allocation
• Dynamic memory
Ø Controlled by the new and delete operators
Ø Not by scope and linkage rules
Ø Can be allocated from one function and freed from another function
• Initialization with the new operator
Ø Built-in types:
ü Using ()
Ø Structure or an array
ü Using {}
Ø Remember [] ?
3. new: Operators, Functions, and
Replacement Functions
• The new and new[] operators call upon two allocation functions
• The placement new operator
Ø Allow to specify the location to be used
Ø Deal with hardware that is accessed via a particular address or to
construct objects in a particular memory location
• See program example 4
Ø Include the new header file
Ø Use new with an argument that provides the intended address
Namespaces
Names
• Names in C++
Ø Variables
Ø functions
Ø Structures
Ø Enumerations
Ø Classes
Ø Class and
structure
members
• Namespace
problems Declarative regions Potential scope and scope
Ø Name conflicts A region in which Begin at its point of
declarations can be made declaration and extends to the
end of its declarative region.
Namespaces
• Main purpose is to provide an area in which to declare names
Ø The names in one namespace don’t conflict with the same names
declared in other name- spaces
Ø There are mechanisms for letting other parts of a program use items
declared in a namespace
Ø Namespaces can be located at the global level or inside other
namespaces, but they cannot be placed in a block
Ø A name declared in a namespace has external linkage by default
• Global namespace
Ø Correspond to the file-level declarative region,
Ø Global variables are now described as being part of the global
namespace
Namespaces
• using Declarations and using Directives
Ø Declarations make particular identifiers available
Ø Using directive makes the entire namespace accessible
• See program example 5
Summary
• A header file
• Header File Management (guarding scheme)
• Scope and Linkage
Ø 1. Automatic Storage Duration
Ø 2. Static Duration Variables: External, Internal and No Linkage
Ø 3. Storage Schemes and Dynamic Allocation
• namespaces
Thanks
zhengf@sustech.edu.cn