Chapter 6 Flashcards

1
Q

Compound Statement

A

Also called block, is a group of zero or more statements that is treated by the compiler as a single statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Block structure

A

Begins and ends with curly braces {}
Statement to be executed in between braces
No semicolon is needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Nesting blocks

A

Enclosing block is called outer block, the enclosed block is called inner or nested block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Use of blocks

A

In conjunction with if statements, if we want multiple statements to execute when the condition evaluates to true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Nesting level (depth)

A

The maximum number of blocks you can have in a function, inuding outer block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Defining a namespace

A

Via namespace keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

User defined namespaces

A

Namespaces you create for your own declarations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Scope resolution operator (::)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Scope resolution with no prefex

A

The identifier is looked for in the global namespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Multiple namespace blocks

A

You can declare namespace blocks in multiple locations, all declarations within the namespace are considered part of the namespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Nested namespaces

A

We can acces as ex. foo::goo::add

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Namespace aliases

A

Allow us to temporarily shorten a long sequience of namespaces into something shorter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Use of namespaces

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Identifier’s Scope

A

Determines where an identifier can be accessed within the source code, compile time property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Block scope

A

Variables are in in scope from their point of definition to the end of the block the variables are defined within

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Variables names

A

Must be unique within a given scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Variables storage duration

A

Determines what rules govern when and how a variable will be created and destroyed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Automatic storage duration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Local variables in nested blocks

A

Limited to the inner block, not accessible in the outer block, variables defined in outer block can be accessed in the inner blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Linkage

A

Property of identifiers, it determines whether other declarations of that name refers to the same object or not, local variables don’t have linkage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Defining variables

A

Should be defined in the most limited scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Global variables

A

Variables declared outside of a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Declaring global variables

A

At the top of a file, below the includes, above any code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Naming global variables

A

Using a g or g_ prefix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
File scope (global scope)
They are visible from the point of declaration till the end of the file in which are declared
26
Static duration
Variables created when the program starts and it is destroyed when it ends
27
Initialization of global variables
Default zero-initialized, can be initialized optioanally, can be constants but they must be initialized
28
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
29
Shadowing global variables
Local variables will shadow the global variable wherever the local variable is in scope
30
Using global instead of local variable
With using the scope operator (::), with no prefix
31
Avoiding shadowing
Shadowing of global variables can be avoided by using a g_ prefix on all global names
32
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
33
Internal variables
Global variables with internal linkage
34
Making non-constant variables internal
By using static keyword
35
Storage class specifier
Sets both the names linkage abd its storage duration
36
Common storage class specifiers
Static, extern and mutable
37
Independent entities
Internal objects and functions that are defined in different files
38
Linkage property
Of identifier, function identifiers have the same linkage property as variable identifiers
39
Functions linkage
Default is external linkage, but it can be set to internal via the static keyword
40
Identifier with external linkage
Can be seen and used bpth from the file in which ate defined and from other files via forvard declaration
41
External variables
Global variables with external linkage
42
Making global variables external
By using the extern keyword
43
External keyword
Has 2 meanings * gives a variable external linkage * acts as forward declaration for external variable defined somwhere else
44
Defining non-const global variables
Do not use extern
45
Function forward declaration
It does not need the extern keyword
46
Variable forward declaration
It needs extern keyword to differentiate variables definitions and forward declarations
47
Static initialization of global variables
With const_expr initializers are initialized to those values, those without are zero initialized
48
Dynamic initialization of global variables
Are initialized with non-constexpr initializers
49
Order of initialization of global variables
They are initialized as they are defined
50
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
51
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
52
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
53
Turning constants into extern variables
Use const instead constexpr, with the use of a g_ prefix
54
Downsides of external variables
* the constants are compile-time constants | * the compiler may not be able to optimize this
55
Inline variable
Variable that is allowed to be dedined in multiple files without violating the one definition rule, it has external linkage
56
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
57
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
58
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)
59
Modularity
Function that utilizes nothing but its parameters and has no side effects
60
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
61
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