C++ Primer - Chapter 2 (Variables & Basic Types) Flashcards
True or false? When we define a const with the same name in multiple files, it is as if we had written definitions for separate variables in each file.
True.
What is a “reference”?
A compound type that “refers to” another type.
A reference defines an alternative name for an object.
Name the primitive data types in C++.
The arithmetic types (i.e. boolean value, characters, integers, and floating-point numbers) and the special type, void.
What is wrong with the following code?
int i=0;
int *p=i;
int i=0; int *p=i; // Error; an integral value cannot be assigned to a pointer.
Is this code legal? If not, why not? const double pi = 3.14; double *ptr = pi; const double *cptr = π *cptr = 42;
The code is not legal.
const double pi = 3.14; double *ptr = pi; // Error, as ptr is a plain pointer. const double *cptr = π // Okay, as cptr can point to a double that is const. *cptr = 42; // Error, as we cannot assign to *cptr.
Are arithmetic expressions involving a signed and unsigned integer typically cast to a signed or unsigned integer?
Further, what is the result of i+u in the following?
int i=-42;
unsigned u=10;
Unsigned.
The sum i+u yields (2^32)-32. Note, that i is converted to an unsigned before the expression is evaluated.
A ________ is a name that is a synonym for another type.
A type alias is a name that is a synonym for another type.
Explain each of the following definitions. Indicate whether any are illegal and, if so, why. int i = 0; (a) double* dp = &i; (b) int *ip = i; (c) int *p = &i;
int i = 0; (a) double* dp = &i; // Invalid, the types do not match. (b) int *ip = i; // Invalid, cannot assign an integral value to a pointer.. (c) int *p = &i; // Valid, the types match.
What form does the declarator of a reference take?
We define a reference type by writing a declarator of the form &d, where d is the name being declared.
Which of the following initializations are legal? Explain why. int i=0; (a) const int *p1 = &i; (b) const int &const r2; (c) const int i2 = i, &r = i;
int i=0; (a) const int *p1 = &i; // Legal, but p1 can't be used to change the value of i. (b) const int &const r2; // Illegal; a reference is not an object, so it can't be const, and it must be initialised. (c) const int i2 = i, &r = i; // Legal if i is an int or const int.
Can you define a reference to a reference?
No, references are not objects so you cannot create a reference to a reference.
What is the result of:
int *ip1, *ip2;
double dp, *dp2;
Note that the * must be repeated for each pointer variable:
int *ip1, *ip2;
Both ip1 and ip2 are pointers to int.
double dp, *dp2;
Here, dp2 is a pointer to double and dp is a double.
Which, if any, of the following assignments are invalid? If they are valid, explain what they do. int i = 0, &r1 = i; double d = 0, &r2 = d; (a) r2 = 3.14159; (b) r2 = r1; (c) i = r2; (d) r1 = d;
int i = 0, &r1 = i; double d = 0, &r2 = d; (a) r2 = 3.14159; // Valid, and also changes the value of d as r2 is bound to d. (b) r2 = r1; // Valid, the value of r2 and d are changed to 0. (c) i = r2; // Valid. (d) r1 = d; // Valid, i and r1 will have value 0.
What is a nonprintable character? Give an example.
A character with no visible representation, such as a control character, a backspace, newline, and so on.
What can constexpr references or pointers be initialised with?
A constexpr pointer can be initialised with the nullptr literal or literal 0.
A constexpr pointer or reference can also point to, or bind to, an object that remains at a fixed address.
True or false? (a) and (b) below are equivalent.
typedef char *pstring;
(a) const pstring cstr = 0;
(b) const char *cstr = 0;
False.
typedef char *pstring; (a) const pstring cstr = 0; (b) const char *cstr = 0; // In (a), because of the typedef, when pstring is used in a declaration, the base type is a pointer to char. // In (b) the base type is a char and * is part of the declarator.
What is a “compound type”? Give two examples of a compound type.
A type that is defined in terms of another type. Examples include references and pointers.
How can the type of a integer literal be specified? Is this prefixed or suffixed?
u or U (unsigned)
l or L (long)
ll or LL (long long)
The specification is applied as a suffix.
N.B.: Use capital letters for best practice, e.g. ‘l’ can be mistaken for a ‘1’.
What is a type alias?
A synonym for another type.
Is the following program legal? If so, what values are printed?
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
{
sum += i;
}
std::cout «_space;i «_space;” “ «_space;sum «_space;std::endl;
The program:
int i = 100, sum = 0; for (int i = 0; i != 10; ++i) { sum += i; } std::cout << i << " " << sum << std::endl;
is legal, and the output is: 100 45.
Name the problems, if any, of the following code: double dval; double *pd = &dval; double *pd2 = pd; int *pi = pd; pi = &dval;
Note that the types of the pointer and the object to which it points must match:
double dval; double *pd = &dval; // This is okay as the initializer is the address of a double. double *pd2 = pd; // This is okay as the initializer is a pointer to double. int *pi = pd; // This is an error as the types of pi and pd differ. pi = &dval; // This is an error; cannot assign the address of a double to a pointer to int.
What do uninitialised pointers typically use (under most compilers) as an address?
Under most compilers, the bits in memory where the pointer resides are used as an address.
When is decltype((variable)) a reference type?
decltype((variable)) is always a reference type.
What is wrong with the following code?
const int a;
const variables must have explicit initialisers.
What is the difference between the following?
extern int i;
int j;
extern int i; // declares but does not define i int j; // declares and defines j
What is a “literal”? What is the general syntax for a literal character and a literal string?
A value such as a number, a character, or a string of characters; a literal’s value is self-evident.
Literal characters are enclosed in single quotes and literal strings in double quotes.
Name the two subcategories of arithmetic types.
Integral types and floating-point types.
How many bits does a char, wchar, char16_t and char32_t take up?
char = 8-bit
wchar (wide) = 16-bit
char16_t (fixed width) = 16-bit
char32_t (fixed width) = 32-bit
What is an “object” defined as in C++ Primer?
Something that occupies a region of memory and has a type.
What happens when an out-of-range value is assigned to a signed type?
Further, what is the value of “ofr_char” in the following expression?
signed char ofr_char=256;
The result is undefined behaviour (UB); the program may work, crash, or produce garbage values.
What does a definition do?
It makes a name or identifier known to the program and allocates storage for the variable. It may also assign an initial value (initialisation).
What is the minimum size of a float, double and long double in bits?
float = 32-bit
double = 64-bit
long double = 64-bit (but can be 96- or 128-bit)
Describe the following code: int i = 42; int *p; int *&r = p; r = &i; *r = 0;
int i = 42; int *p; // The variable p is a pointer to int. int *&r = p; // Here, r is a reference to the pointer p. r = &i; // The reference r refers to a pointer; assigning &i to r makes p point to i. *r = 0; // Dereferencing r yields i (the object to which p points) and changes i to 0.
True or false? Pointers and references to const are top-level consts.
False, they’re low-level consts.
Can the type of a reference and the object to which it refers differ?
In general, no.
However, there are two particular cases where they can:
(1) When the const specifier is applied, and;
(2) When the object is of a derived class type.
How many octal digits can be used in an escape sequence?
Up to 3.
What can a plain reference not be bound to?
A reference cannot be bound to a literal or the result of a general expression.
What is a “declarator”?
The part of a declaration that includes the name being defined and an optional type modifier.
How can a variable be declared but not defined?
By adding the extern keyword.
Given the variable definitions: int i, *p1; const int ic=1; const int *const p3=&ic; Which of the following assignments are legal? Explain why. (a) i = ic; (b) p1 = p3; (c) p1 = &ic;
Define the variables: int i, *p1; const int ic=1; const int *const p3=&ic;
(a) i = ic;
Legal, i is a plain int and copying doesn’t affect a const object.
(b) p1 = p3;
Illegal, otherwise p1 would be able to change the data pointed to by p3.
(c) p1 = ⁣
Illegal; p1 must be a pointer to const.
What is a “temporary” variable?
An unnamed object created by the compiler while evaluating an expression.
A temporary exists until the end of the largest expression that encloses the expression for which it was created.
What is the problem with the following code? int ci=1; auto &g = ci; auto &h = 42; const auto &j = 42;
const int ci=1; auto &g = ci; // This is fine; top-level consts in the initializer are not ignored. auto &h = 42; // This is an error; we can’t bind a plain reference to a literal. const auto &j = 42; // This is okay; we can bind a const reference to a literal.
Will the compiler complain about the following functions being the same? Why? int func(int i) {...} int func(const int i) {...}
Yes, because top-level consts are ignored when copying objects (to initialise the function parameters).
Notice here that the values are not passed by reference.
Do pointers need to be initialised?
No.
What type of const is not ignored when copying?
Low-level const.
What is wrong with the following code?
int &refVal2;
A reference must be initialised.
What is the term given for the scope that is outside all other scopes?
Global scope.
Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:
(a) ’a’, L’a’, “a”, L”a”
(b) 10, 10u, 10L, 10uL, 012, 0xC
(c) 3.14, 3.14f, 3.14L
(d) 10, 10u, 10., 10e-2
(a) char, wchar, string (‘a’+’\0’) and a wide character array of size 2.
(b) int, unsigned, long int, unsigned long, int (=10), int (=12)
(c) double, float, long double
(d) int, unsigned, double, double.
What happens when an initialiser is provided to a variable defined as extern outside a function? What about when the variable is defined as extern inside a function?
When a variable defined as extern is initialised outside a function, the extern keyword is overridden. When the variable is defined as extern inside a function, it is an error to provide an initialiser.
What does it mean for an object to be top-level const?
That an object itself is const and cannot be altered.
What digit does 0xe or 0xE represent?
14.
What does the following code produce?
unsigned u = 10, u2 = 42;
std::cout «_space;u2 - u «_space;std::endl;
std::cout «_space;u - u2 «_space;std::endl;
u2-u = 32 u-u2 = 2^32 - 32
When can a const variable use the same operations as a nonconst variable?
When the operation does not alter the variable.
An __________ starts with the keyword using followed by the alias name and an =.
An alias declaration starts with the keyword using followed by the alias name and an =.
What is the term given for the initialisation of a variable when no explicit initializer is given?
Default initialisation.
Is it possible to have a const reference? Is it possible to have a const pointer?
References are not objects, so they cannot be made const. Pointers, on the other hand, are, so they can be made const.
How can we make the compiler deduce the type of a variable for us?
Using the “auto” keyword.
What type does a have in the following code?
int i = 0, &r = i;
auto a = r;
int i = 0, &r = i;
auto a = r;
Here, a is an int (r is an alias for i, which has type int).
What does the following code produce? unsigned u = 10; int i = 10, i2 = 42; std::cout << i2 - i << std::endl; std::cout << i - i2 << std::endl; std::cout << i - u << std::endl; std::cout << u - i << std::endl;
i2-i = 32
i-i2=-32
i-u=0
u-i=0
Given the variable definitions: const int ic=1, &r = ic; const int *const p3=&ic; int *p1, *const p2=&ic; Which of the following assignments are legal? Explain why. (a) p3 = &ic; (b) p2 = p1; (c) ic = *p3;
Define the variables: const int ic=1, ic2=1, &r = ic; const int *const p3=&ic; int *p1, *const p2=&ic;
(a) p3 = &ic2;
// Illegal, p3 is a const pointer and cannot be assigned to after initialisation.
(b) p2 = p1;
// Illegal, p2 is a const pointer and cannot be assigned to after initialisation.
(c) ic = *p3;
// Illegal, ic is a const variable and cannot be assigned to after initialisation.
Name up to four levels of scope.
Global — names defined outside any other scope.
Class — names defined inside a class.
Namespace — names defined inside a namespace.
Block — names defined inside a block.
What is a constant expression?
An expression whose value cannot change and one that can be evaluated at compile time.
What does a variable definition consist of?
A type specifier followed by one or more variable names separated by a comma and terminated with a semicolon.
Can a reference be defined without an initialiser?
No, there is no way to rebind a reference so it must be initialised.
What happens when we define a reference with an initialiser?
Instead of copying the initialiser’s value, we bind the reference to its initialiser.
What four states can a pointer be in?
- It can point to an object.
- It can point to the location just immediately past the end of an object.
- It can be a null pointer, indicating that it is not bound to any object.
- It can be invalid; values other than the preceding three are invalid.
For each of the following declarations indicate whether the object being declared has top-level or low-level const.
(a) const int v2 = 0;
(b) int v1 = v2;
(c) int *p1 = &v1, &r1 = v1;
(d) const int *p2 = &v2, *const p3 = &i, &r2 = v2;
(a) const int v2 = 0; Top-level const; v2 is a const object. (b) int v1 = v2; v1 is nonconst; top-level const is ignored when copying. (c) int *p1 = &v1, &r1 = v1; Both p1 and r1 are nonconst. (d) const int *p2 = &v2, *const p3 = &i, &r2 = v2; p2 is low-level const, p3 is both low-level and top-level const, and r2 is low-level const.
What is the value of j in the following program? int i = 42; int main() { int i = 100; int j = i; }
100.
Does a constexpr impose a top-level or low-level const?
Top-level.
What is the difference between ‘a’ and “a”?
The compiler appends the null character ‘\0’ to a string literal (so “a” is corresponds to ‘a’ and ‘\0’).
How can the type of a floating-point literal be specified? Is this prefixed or suffixed?
f or F (float)
l or L (long)
The specification is applied as a suffix.
N.B.: Use capital letters for best practice, e.g. ‘l’ can be mistaken for a ‘1’.
Explain the following definitions. For those that are illegal, explain what is wrong and how to correct it.
(a) std::cin»_space; int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14;
(a) std::cin >> int input_value; // The right-hand operand of the input operator must be an object. Correction: int input_value; std::cin >> input_value; (b) int i = { 3.14 }; // Error; leads to a loss of information. (c) double salary = wage = 9999.99; // Wage has not been declared. Correction: double wage; double salary = wage = 9999.99; (d) int i = 3.14; // Okay, but will lead to a loss of information.