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
Q

File scope (global scope)

A

They are visible from the point of declaration till the end of the file in which are declared

26
Q

Static duration

A

Variables created when the program starts and it is destroyed when it ends

27
Q

Initialization of global variables

A

Default zero-initialized, can be initialized optioanally, can be constants but they must be initialized

28
Q

Name hiding/ shadowing

A

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
Q

Shadowing global variables

A

Local variables will shadow the global variable wherever the local variable is in scope

30
Q

Using global instead of local variable

A

With using the scope operator (::), with no prefix

31
Q

Avoiding shadowing

A

Shadowing of global variables can be avoided by using a g_ prefix on all global names

32
Q

Internal linkage identifier

A

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
Q

Internal variables

A

Global variables with internal linkage

34
Q

Making non-constant variables internal

A

By using static keyword

35
Q

Storage class specifier

A

Sets both the names linkage abd its storage duration

36
Q

Common storage class specifiers

A

Static, extern and mutable

37
Q

Independent entities

A

Internal objects and functions that are defined in different files

38
Q

Linkage property

A

Of identifier, function identifiers have the same linkage property as variable identifiers

39
Q

Functions linkage

A

Default is external linkage, but it can be set to internal via the static keyword

40
Q

Identifier with external linkage

A

Can be seen and used bpth from the file in which ate defined and from other files via forvard declaration

41
Q

External variables

A

Global variables with external linkage

42
Q

Making global variables external

A

By using the extern keyword

43
Q

External keyword

A

Has 2 meanings

  • gives a variable external linkage
  • acts as forward declaration for external variable defined somwhere else
44
Q

Defining non-const global variables

A

Do not use extern

45
Q

Function forward declaration

A

It does not need the extern keyword

46
Q

Variable forward declaration

A

It needs extern keyword to differentiate variables definitions and forward declarations

47
Q

Static initialization of global variables

A

With const_expr initializers are initialized to those values, those without are zero initialized

48
Q

Dynamic initialization of global variables

A

Are initialized with non-constexpr initializers

49
Q

Order of initialization of global variables

A

They are initialized as they are defined

50
Q

Declaring global constants as internal variables

A
  • 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
Q

Optimazing away

A

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
Q

Downsides of global constants as internal variables

A
  • 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
Q

Turning constants into extern variables

A

Use const instead constexpr, with the use of a g_ prefix

54
Q

Downsides of external variables

A
  • the constants are compile-time constants

* the compiler may not be able to optimize this

55
Q

Inline variable

A

Variable that is allowed to be dedined in multiple files without violating the one definition rule, it has external linkage

56
Q

Restrictions of inline variable

A
  • all definitions of the inline variable must be identical

* the inline variable definitionmust be present in any file that uses the variable

57
Q

Downsides of non-const global variables

A
  • 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
Q

Advantiges of local variables

A
  • 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
Q

Modularity

A

Function that utilizes nothing but its parameters and has no side effects

60
Q

When to use non- const global variables

A
  • 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
Q

How to use global variables

A
  • 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