Chapter 6 Flashcards
Compound Statement
Also called block, is a group of zero or more statements that is treated by the compiler as a single statement
Block structure
Begins and ends with curly braces {}
Statement to be executed in between braces
No semicolon is needed
Nesting blocks
Enclosing block is called outer block, the enclosed block is called inner or nested block
Use of blocks
In conjunction with if statements, if we want multiple statements to execute when the condition evaluates to true
Nesting level (depth)
The maximum number of blocks you can have in a function, inuding outer block
Defining a namespace
Via namespace keyword
User defined namespaces
Namespaces you create for your own declarations
Scope resolution operator (::)
Used to look for an identifier in a particular namespace, identifier specified by the right operand should be looked for in the scope of the left operand
Scope resolution with no prefex
The identifier is looked for in the global namespace
Multiple namespace blocks
You can declare namespace blocks in multiple locations, all declarations within the namespace are considered part of the namespace
Nested namespaces
We can acces as ex. foo::goo::add
Namespace aliases
Allow us to temporarily shorten a long sequience of namespaces into something shorter
Use of namespaces
- in applications, to seperate application-specific code from code that can be reused
- when you want to distribute the code
- to allow the user to see the contents of your library
Identifier’s Scope
Determines where an identifier can be accessed within the source code, compile time property
Block scope
Variables are in in scope from their point of definition to the end of the block the variables are defined within
Variables names
Must be unique within a given scope
Variables storage duration
Determines what rules govern when and how a variable will be created and destroyed
Automatic storage duration
In local variables, which means they are created at the point of definition and destroyed at the end of the block they are defined in
Local variables in nested blocks
Limited to the inner block, not accessible in the outer block, variables defined in outer block can be accessed in the inner blocks
Linkage
Property of identifiers, it determines whether other declarations of that name refers to the same object or not, local variables don’t have linkage
Defining variables
Should be defined in the most limited scope
Global variables
Variables declared outside of a function
Declaring global variables
At the top of a file, below the includes, above any code
Naming global variables
Using a g or g_ prefix
File scope (global scope)
They are visible from the point of declaration till the end of the file in which are declared
Static duration
Variables created when the program starts and it is destroyed when it ends
Initialization of global variables
Default zero-initialized, can be initialized optioanally, can be constants but they must be initialized
Name hiding/ shadowing
When we have a variable inside a nested block that has the same name as a variable in an outer block, nested one hides the outer when they both are in scope
Shadowing global variables
Local variables will shadow the global variable wherever the local variable is in scope
Using global instead of local variable
With using the scope operator (::), with no prefix
Avoiding shadowing
Shadowing of global variables can be avoided by using a g_ prefix on all global names
Internal linkage identifier
Can be seen and used within a single file, but not from other files, if two files have identifiers with the same name will be treated as independent
Internal variables
Global variables with internal linkage
Making non-constant variables internal
By using static keyword
Storage class specifier
Sets both the names linkage abd its storage duration
Common storage class specifiers
Static, extern and mutable
Independent entities
Internal objects and functions that are defined in different files
Linkage property
Of identifier, function identifiers have the same linkage property as variable identifiers
Functions linkage
Default is external linkage, but it can be set to internal via the static keyword
Identifier with external linkage
Can be seen and used bpth from the file in which ate defined and from other files via forvard declaration
External variables
Global variables with external linkage
Making global variables external
By using the extern keyword
External keyword
Has 2 meanings
- gives a variable external linkage
- acts as forward declaration for external variable defined somwhere else
Defining non-const global variables
Do not use extern
Function forward declaration
It does not need the extern keyword
Variable forward declaration
It needs extern keyword to differentiate variables definitions and forward declarations
Static initialization of global variables
With const_expr initializers are initialized to those values, those without are zero initialized
Dynamic initialization of global variables
Are initialized with non-constexpr initializers
Order of initialization of global variables
They are initialized as they are defined
Declaring global constants as internal variables
- create a header file to hold the constants * inside the file define a namespace
- add the constants (constexpr) inside the namespace
- include file wherever
Optimazing away
Refers to any process where the compiler optimizes the performance of your program by removing things in a way that doesn’t affect the output of the program
Downsides of global constants as internal variables
- changing a single constant value would require recompiling every file that includes the constant header
- if a constants are large in size and can’t be optimized away, it can use a lot of memory
Turning constants into extern variables
Use const instead constexpr, with the use of a g_ prefix
Downsides of external variables
- the constants are compile-time constants
* the compiler may not be able to optimize this
Inline variable
Variable that is allowed to be dedined in multiple files without violating the one definition rule, it has external linkage
Restrictions of inline variable
- all definitions of the inline variable must be identical
* the inline variable definitionmust be present in any file that uses the variable
Downsides of non-const global variables
- their value can be changed by any function that is called
- you might have to look through the entire program to understand their usage
- they make your program less modular and flexible
Advantiges of local variables
- other functions can’t affect them directly * declaring them as close to where are used as possible (minimizes the amount of code you need to look trough)
Modularity
Function that utilizes nothing but its parameters and has no side effects
When to use non- const global variables
- a log file, where you dump error or debug information
- when there should be only one of the thing the variable represents or it’s use should be umbigous troughout the program
How to use global variables
- prefix all non-namespaced global variables with g or g_ or put them in namespace
- variable should be only accesed from within the file its declared in
- provide external global “access functions” to work with the variable
- pass the variable as argument, instead using it directly into the function