Starting out with C++ Chapter 10 Pointers
True/False: A pointer can be passed as an argument to a function.
True
True/False: A pointer with the value 0 (zero) is called the NULL pointer.
True
True/False: An array name is a pointer constant because the address it represents cannot be changed during run-time.
True
True/False: C++ does not perform array bounds checking.
True
A pointer variable is designed to store
a memory address.
Memory leaks occur in a program when
a programmer fails to use delete on a pointer whose memory was allocated by the new operator.
The statement int *ptr = new int; acquires memory to hold an integer and then
sets ptr to point to the allocated memory.
A ________ keeps track of dynamically-allocated memory and automatically deletes when it is no longer in use
smart pointer
The statement cin >> *p;
stores the keyboard input into the variable pointed to by p.
Any time you use the new operator, it is good practice to
use delete afterwards to free the memory allocated by new
Dynamic memory allocation occurs
when a variable is created at run-time.
Programs with memory leaks
will eventually crash if allowed to execute for long periods of time.
If arr is an array identifier and k is an integer, the expression arr[k] is equivalent to
*(arr + k)
The ________ and ________ operators can respectively be used to increment and decrement a pointer variable.
++, --
You may use the type pointer to a structure as the type of a function parameter. structure member. function return type. all of these none of these
All of these
True/False: Memory cannot be allocated after a program is already running.
False
True/False: You can use pass by value when passing a C++ unique pointer to a function.
False
True/False: If a unique pointer ( unique_ptr) p points to a block of memory, a second unique pointer can be made to point to the same block of memory by using the library function move(p).
True
True/False: It is legal to subtract a pointer variable from another pointer variable.
True
True/False: The expression *s->p; is only meaningful if s is a pointer to a structure and p is a pointer that is a member of that structure.
True
True/False: The expression s->m has the same meaning as (*s).m.
True
True/False: The expression s->m is meaningful only when s is a pointer to a structure and m is a member of the structure.
True
True/False: The statement Rectangle * boxPtr; defines a variable boxPtr to be a pointer pointing to a type Rectangle.
True
True/False: You can use pass by reference when passing a C++ unique pointer to a function
True
Every byte in the computer's memory is assigned a unique
address.
The term pointer can be used interchangeably with
address.
Which of the following statements is not valid C++ code? int ptr = &num1; int ptr = int *num1; float num1 = &ptr2; all of these are valid. all of these are invalid.
all of these are invalid.
When the ________ is placed in front of a variable name, it returns the address of that variable.
ampersand ( & )
A statement that displays the address of the variable num1 is
cout << &num1;.
The statement double *num;
defines a pointer variable called num.
If Circle is a structure type, the statement Circle *pcirc;
defines a structure pointer called pcirc.
Which of the following statements correctly deletes a dynamically-allocated array pointed to by p? delete p; p delete[ ]; delete [ ] p; delete array p; none of these
delete [ ] p;
The code segment int *ptr; has the same meaning as
int* ptr;.
A function may return a pointer, but the programmer must ensure that the pointer
is pointing to an object that is still valid after the return of the function
A function may return a pointer, but the programmer must ensure that the pointer
is pointing to an object that is still valid after the return of the function.
A C++ smart pointer
is used to ensure that memory that is no longer in use is automatically deleted.
The delete operator should only be used on pointers that
point to storage allocated by the new operator.
The statement int *ptr; means
ptr is a pointer variable that will store the address of an integer variable.
A unique pointer is a pointer
that points to a block of memory, and no other pointer is allowed to point to the same block.
A pointer may be initialized with
the address of an existing object of the appropriate type.
If a variable occupies more than one byte of memory, its address is
the address of the first byte of storage allocated to it.
When the less than ( < ) operator is used between two pointer variables, the expression is testing whether
the address of the first variable comes before the address of the second variable in the computer's memory
The set of operations supported by the unique_ptr class include
the dereferencing operators * and ->.
The statement cout << &num1; will output
the memory address of the variable called num1.
Suppose that a function dynamically allocates a block of memory with a local pointer variable p pointing to the allocated block. Suppose further that there are no other pointers referencing that block of memory, and the function returns without doing a delete on p. Then
the program will suffer from memory leaks.
To dereference a structure pointer and simultaneously access a member of the structure, the appropriate operator to use is
the structure pointer operator, ->.
The statement cout << *ptr; will output
the value stored in the variable whose address is contained in ptr.
A dangling pointer is a pointer
to a block of memory that has been deleted.
A reason for passing a pointer to a function is
to avoid the overhead of copying large data structures and to allow the called function to modify a variable accessible to the calling function are both true.
The statement shared_ptr<int> p(new int); involves
two allocations of two different dynamic blocks of memory.