Chapter 9 Flashcards
- What does the indirection operator do?
It dereferences a pointer, allowing code to work with the value that the pointer
points to.
Look at the following code.
int x = 7;
int *iptr = &x;
What will be displayed if you send the expression *iptr to cout ? What happens if you send the
expression ptr to cout ?
The value 7 will be displayed if the expression *iptr is sent to cout. If the
expression iptr is sent to cout, the address of the variable x will be displayed.
- So far you have learned three different uses for the * operator. What are they?
Multiplication operator, definition of a pointer variable, and the indirection
operator
- What math operations are allowed on pointers?
Addition and subtraction.
- Assuming that ptr is a pointer to an int , what happens when you add 4 to ptr ?
It adds 4 times the size of an int to the address stored in ptr
Look at the following array definition.
int numbers[] = { 2, 4, 6, 8, 10 };
What will the following statement display?
cout «_space;*(numbers + 3) «_space;endl;
8
- What is the purpose of the new operator?
To dynamically allocate memory.
What happens when a program uses the new operator to allocate a block of memory, but the
amount of requested memory isn’t available? How do programs written with older compilers
handle this?
An exception is thrown, which causes the program to terminate. Under older
compilers, the new operator returns the null address (address 0) when it cannot
allocate the requested amount of memory.
- What is the purpose of the delete operator?
To free memory that has been dynamically allocated with the
- Under what circumstances can you successfully return a pointer from a function?
You should only return a pointer from a function if it is
* A pointer to an object that was passed into the function as an argument
* A pointer to a dynamically allocated object
- What is the difference between a pointer to a constant and a constant pointer?
A pointer to a constant points to a constant item. The data that the pointer points
to cannot change, but the pointer itself can change. With a constant pointer, it is
the pointer itself that is constant. Once the pointer is initialized with an address, it
cannot point to anything else.
- What are two advantages of declaring a pointer parameter as a constant pointer?
Not only will this protect you from writing code in the function that accidentally
changes the argument, but the function will be able to accept the addresses of both
constant and non-constant arguments.
- Each byte in memory is assigned a unique __________.
address
- The __________ operator can be used to determine a variable’s address.
address (&)
- _________ variables are designed to hold addresses.
pointer
- The __________ operator can be used to work with the variable a pointer points to.
indirection (*)
- Array names can be used as __________, and vice versa.
pointers
- Creating variables while a program is running is called __________.
dynamic memory allocation
- The __________ operator is used to dynamically allocate memory.
new
- Under older compilers, if the new operator cannot allocate the amount of memory requested,
it returns ________
0 or null
- A pointer that contains the address 0 is called a(n) __________ pointer.
null
- When a program is finished with a chunk of dynamically allocated memory, it should free it
with the __________ operator
delete
- You should only use pointers with delete that were previously used with __________.
new
Look at the following code.
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
cout «_space;*ptr «_space;endl;