Ch.9 Flashcards
If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using
p1 = new int*[numRows];
for(int i = 0; i < numRows; i ++)
p1[i] = new int[numColumns];
Which of the following is not true?
A) If a pointer points to an array, it can be used in place of the array name.
B) An array can be assigned the value in a pointer variable.
C) A pointer can be assigned the address of an array.
D) If a pointer points to an array, the pointer does not know how many elements are in the array.
An array can be assigned the value in a pointer variable.
Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?
delete [] p1;
Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to?
cout << *p1;
If two pointer variables point to the same memory location, what happens when one of the pointers is freed?
If you attempt to free the other pointer a run-time error will occur.
&
The other pointer should be considered to be un-initialized and If you attempt to free the other pointer a run-time error will occur
Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal?
A) p1 = new char[size*size];
B) p1 = new string[size];
C) p1 = new ifstream[size];
D) A and B
E) A, B and C
p1 = new string[size];, p1 = new ifstream[size];, and p1 = new char[size\*size];
Which of the following correctly declares a user-defined data type that defines a pointer to a float?
A) typedef floatPtr* float
B) float* floatPtr ;
C) typedef float* floatPtr;
D) typedef floatPtr *float;
typedef float* floatPtr;
What is the output of the following code fragment?
int v1 = 2, v2 = -1, *p1, *p2;
p1 = & v1;
p2 = & v2;
p2 = p1;
cout << *p2 << endl;
2
What is wrong with the following code fragment?
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1 = 11;
*p2 = 0;
p2 = p1;
cout << *p1 < delete p1;
delete p2;
p1 and p2 both have the same value, so the delete p2 will cause an error and You have a memory leak.
Given that p1 is a pointer variable of the string class, which of the following are legal statements?
cout << *p1;
If p1 is an integer pointer that is pointing to memory location 1001, and an integer takes 4 bytes, then (p1 + 1) evaluates to
1005
Given that p1 is a pointer, p1 ++
advances p1 by one unit of the type of variable to which p1 points.
Which of the following assigns to p1 the pointer to the address of value?
p1=&value;
Which of the following correctly declare 3 integer pointers?
int *p1, *p2, *p3;
In the statement cout << *p1;, the * is called the
dereferencing operator
Write the code to declare a dynamic array of strings (use the string pointer variable p1) that has as many elements as the variable arraySize
p1 = new string[arraySize];
Declare a pointer variable named ptr to an integer
int *ptr;
The & operator is called the
address of operator
Write the code that assigns to p1 (an integer pointer variable) the pointer to a dynamically created integer.
p1 = new int;
Write the code to return the dynamic memory pointed to by p1 to the freestore.
delete p1;
Dynamic variables are created from the
freestore or heap.
A pointer can be stored in an integer variable
FALSE
When you return a dynamic array to the freestore, you must include the number of elements in the array.
False
Even though pointers point to addresses which are integers, you cannot assign an integer to a pointer variable.
True
You can assign an array to a pointer variable.
True
Dynamically created variables have no name.
True
If p1 and p2 are both pointers that point to integers in memory, the condition p1 == p2 will be true if the values that are in those memory locations are the same.
False