Variables Flashcards
What is a block?
Also known as a compound statement, it is a group of statements that are surrounded by {}, and are treated by the compiler as a single statement
.
What is the recommended maximum depth for nesting blocks, and what should be done when it is reached?
3 - 4 blocks deep, anymore and it’s time for multiple functions.
What is a local variable?
A variable inside a code block that has local (also known as block) scope.
What happens to local variables when the block ends?
They are destroyed.
T/F: Nested blocks are considered part of the outer block in which they are defined
T; variables declared in the outer block can be seen inside the nested block
What is the best practice when deciding the scope of a variable?
Give it the smallest possible use depending on where it is used. This avoids confusion and can help with efficiency.
What is the scope of a global variable?
Program scope; can be accessed anywhere.
T/F: global variables can be accessed across multiple files.
T; they have program scope.
What has to be done in order to share a global variable across files?
The ‘extern’ keyword, which tells the compiler you are not declaring a new variable but rather using a variable defined in an external source.
How can a global variable be declared in a header file?
Declare the variable in a .h file with the ‘extern’ keyword, then define it in the corresponding .cppfile, WITHOUT the extern keyword
In the case that you are working in a block where the a local variable with the same name as a global variable replaces it, what is the operator to access the global version of a variable?
The unary scope operator, although the need to do this indicates poor organization/naming.
What are two reasons global variables are dangerous?
They increase complexity because it means a bug can be literally anywhere in the program, since the global variable is in effect everywhere, and they can be changed by any function anywhere, which can lead to unexpected results.
What does file scope mean, and how is it different from global scope?
A variable with file scope can be accessed by any function or block within a single file. File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files)
How is a file scoped variable declared?
with the ‘static’ keyword before the typename
What are the different meanings of the static keyword, and what determines its function?
Its location in the program; outside of a block it gives a variable file scope, within a block it gives a variable fixed duration, meaning it is NOT destroyed at the end of the block. This is in contrast to the usual automatic duration of a local variable, which is destroyed at the end of the block.
include
What does the following program print?
void IncrementAndPrint() { using namespace std; static int s_nValue = 1; // fixed duration \++s_nValue; cout
2
3
4
b/c a static variable in a block has fixed scope, meaning it is only initialized ONCE and persists throughout the program
What is one common use for fixed duration (static local) variables?
For generating unique id’s across a file:
int GenerateID() { static int nNextID = 0; return nNextID++; }
Because nNextID is a local variable, it can not be “tampered with” by other functions.