Scope, Duration and Linkage Flashcards

1
Q

What is meant by a variable’s scope?

A

Scope determines where a variable is accessible.

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

Explain duration of a variable

A

Duration determines when a variable is created and destroyed

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

What is the linkage of a variable?

A

Linkage determines whether the variable can be exported to another file or not

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

What is the scope of global variables?

A

Global scope (a.k.a. file scope), which means they can be accessed from the point of declaration to the end of the file which they are declared

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

What is the duration of a global variable?

A

They have static duration, which means they are created when the program is started, and destroyed when it ends

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

What is the linkage of a global variable?

A

They can have either internal or external linkage, via the static and external keywords respectively

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

How should you define a global constant in C++17

A

pragma once

Define them as inline constexpr global variables in a header file

Constants.h

constexpr int MAX_VALUE = 100;

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

What is static duration?

A

When a variable is created when the program starts and destroyed when it ends

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

What is internal linkage?

A

When an identifier can only be used in the file in which it is defined

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

What is automatic duration?

A

When a local variable is created at point of definition and destroyed when the block is exited

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

What is automatic duration?

A

When a local variable is created at point of definition and destroyed when the block is exited

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

What effect does using keyword static have on a global variable?

A

It gives it internal linkage; meaning the variable cannot be exported to other files

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

What effect does using keyword static have on a local variable?

A

It gives it static duration, meaning the variable will only be created once, and will not be destroyed until the end of the program

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

Variable with block/local scope can only be accessed from point of declaration until the end of the block. Three examples are:

A

Local variables, function parameters and program defined type definitions (such as enums and classes) declared inside a block

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

Variables and functions with global scope can be accessed from the point of declaration to the end of the file. This includes (3)

A

Global variables
functions
program defined type definitions (such as enums or classes) declared inside a namespace or in the global scope

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

Define automatic duration and give two examples

A

Variables which are created at the point of definition, and destroyed when the block they are part of ends.
E.g. local variables and function parameters

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

Define static duration and give two examples of variables with static duration

A

Variables which are created when the program begins and destroyed when it ends.
E.g.
global variables
static local variables

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

Define static duration and give two examples

A

It is when a variable is created when the program begins and is destroyed when it ends. E.g. global variables and static local variables

19
Q

Define dynamic duration and give an example

A

Variables are created and destroyed by programmer request. E.g. dynamically allocated variables

20
Q

Define no linkage and give 2 examples

A

An identifier with no linkage means another declaration of the same identifier refers to a unique entity.

It can also be thought of as block scope

E.g.
local variables
Function parameters
program defined type identifiers (such as classes and enums) declared inside a block

21
Q

Define internal linkage and give 5 examples

A

An identifier with internal linkage means a declaration of the same identifier within the same translation unit refers to the same object or function. Entities whose identifiers have internal linkage include

Static global variables
Static local variables
Static functions
Const global variables
Functions declared in an unnamed namespace
Program defined type definitions (such as enums and classes) declared inside an unnamed namespace

22
Q

Define external linkage and give 4 examples

A

It means a declaration of the same identifier within the same program refers to the same object of function. E.g. functions, non const global variables, external const global variables, inline const global variables

23
Q

What is extern keyword?

A

This means static storage duration and external linkage

24
Q

static keyword

A

Static storage duration and internal linkage

25
Q

thread_local keyword

A

Thread storage duration - which is when the object is alive as long as the thread still exists

26
Q

mutable keyword

A

Object allowed to be modified even if containing class is const

27
Q

auto keyword

A

Automatic storage duration (deprecated in C++11)

28
Q

register keyword

A

Automatic storage duration and hint to the compiler to place in a register (deprecated in C++17)

29
Q

What is a compound statement/block?

A

A group of statements treated by the compiler as a single statement. Begin with { and end with }

30
Q

What is a user defined namespace?

A

Namespaces defined by you for your own declarations. Doesn’t include ones provided by c++ or third party libraries

31
Q

What is the scope resolution operator?

A

It is ::

It tells the compiler that the identifier specified by the right hand operand should be looked for in the scope of the left hand operand. If no left hand operand is provided, global namespace is assumed

32
Q

What is shadowing / name hiding?

A

When a variable in a nested block can obscure an identically named variable in an outer block. This should be avoided

33
Q

Define file scope

A

When a variable is visible from point of declaration until end of the file

34
Q

Why should you initialise static variables on the line they are defined?

A

If you initialise them dynamically other parts of your code might access them before they are defined which could have unexpected results

35
Q

Why should non const global variables be avoided?

A

Hard to know what other parts of program are changing variable, what value is expected, hard to debug when something goes wrong as have to check every place program access global var

36
Q

What is a qualified name?

A

A name that includes an associate scope e.g. std::string

37
Q

What is an unqualified name

A

A name that does not include a scoping qualifier e.g. string vs std::string

38
Q

Why should using statements be avoided?

A

They increase likelihood of naming collisions in your codebase and make it unclear if you are calling internal or external code. Also a using directive imports all identifiers into the scope which can be costly

39
Q

What is an in-line function

A

It exempts a function from the one definition rule, allowing its definition to be imported into multiple code files. Typically defined in header files

40
Q

What is a constexpr function?

A

A function whose return value may be computed at compile time

constexpr int square(int x)
{
return x * x;
}

41
Q

Unnamed namespace

A

Implicitly treats all content of namespace as if it had internal linkage

42
Q

consteval keyword

A

Indicates a function must evaluate at compile time. Otherwise a compile error. Called immediate functions

consteval int square(int x)
{
return x * x;
}

43
Q

What is the cost of creating a variable e.g.

int i {}

A

There is no cost, it is the initialisation that has a cost