Chapter 9 - Pointers
A pointer variable may be initialized with
a valid address in the computer's memory
Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr;
the value stored in the variable whose address is contained in ptr
The ________, also known as the address operator, returns the memory address of a variable
ampersand ( & )
Which of the following can be used as pointers?
array names
The following statement ________. int *ptr = new int;
assigns an address to the variable ptr
Which of the following statements deletes memory that has been dynamically allocated for an array?
delete [] array
With pointer variables you can ________ manipulate data stored in other variables.
indirectly
Select all that apply. Of the following, which statements have the same meaning?
int* ptr = nullptr; int *ptr = nullptr;
In the following statement, what does int mean?int *ptr = nullptr;
ptr is a pointer variable and will store the address of an integer variable
After the code shown executes, which of the following statements is TRUE? int numbers[] = {0, 1, 2, 3, 4}; int *ptr = numbers; ptr++;
ptr now holds the address of numbers[1]
A function may return a pointer but the programmer must ensure that the pointer
still points to a valid object after the function ends
The following statement ________. cin >> *num3;
stores the keyboard input into the variable pointed to by num3
When you work with a dereferenced pointer, you are actually working with
the actual value of the variable whose address is stored in the pointer variable
If a variable uses more than one byte of memory, for pointer purposes its address is
the address of the first byte of storage
To help prevent memory leaks from occurring in C++11, a ________ automatically deletes a chunk of dynamically allocated memory when the memory is no longer being used.
smart pointer
A pointer variable is designed to store
a memory address
The ________ and ________ operators can be used to increment or decrement a pointer variable.
++, --
What will the following code output? int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl;
2
What will the following code output? int number = 22; int *var = &number; cout << *var << endl;
22
Which of the following statements is NOT valid C++ code? int ptr = int *num1; int ptr = &num1; float num1 = &ptr2; All invalid All valid
ALL INVALID
What does the following statement do?double *num2;
Declares a pointer variable named num2
Which of the following is TRUE about this statement?sum += *array++;
This statement assigns the dereferenced pointer's value, then increments the pointer's address.
A pointer can be used as a function argument, giving the function access to the original argument - T/F
True
An array name is a pointer constant because the address stored in it cannot be changed at runtime - T/F
True
Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing myValues[index] (myValues + index) T/F
True
In C++11 you can use smart pointers to dynamically allocate memory and not worry about deleting the memory when you are finished using it - T/F
True
Select all that apply. Select as many of the following options that make this sentence TRUE: The contents of pointer variables may be changed with mathematical statements that perform
addition and subtraction
Every byte in the computer's memory is assigned a unique
address
Dynamic memory allocation occurs
when a new variable is created at runtime
C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array - T/F
True
In C++11, the nullptr keyword was introduced to represent the address 0 - T/F
True
It is legal to subtract a pointer variable from another pointer variable - T/F
True
The unique_ptr is the sole owner of a piece of dynamically allocated memory - T/F
True
To use any of the smart pointers in C++11 you must use the following directive in the header file: #include <memory> - T/F
True
In C++11, the ________ keyword was introduced to represent address 0
nullptr
The ampersand (&) is used to dereference a pointer variable in C++
False
The weak_ptr can share ownership of a piece of dynamically allocated memory
False
With pointer variables you can access but not modify data in other variables - T/F
False
Which of the following statements displays the address of the variable numb?
cout << &numb
Use the delete operator only on pointers that were
created with the new operator
Not all arithmetic operations can be performed on pointers. For example, you cannot ________ or ________ pointers.
multiply, divide
When you pass a pointer as an argument to a function, you must
none of these
When the less than operator (<) is used between two pointer values, the expression is testing whether
the address of the first variable comes before the address of the second variable in the computer's memory
What will the following code output? int number = 22; int *var = &number; cout << var << endl;
the address of the variable number
If you are using an older computer that does NOT support the C++11 standard, you should initialize pointers with
the integer 0 or the value NULL
What will the following statement output? cout << &num1;
the memory address of the variable named num1
Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int?
unique_ptr uniq( new int ); //should be unique_ptr<int> uniq( new int );