Chapter 9 Flashcards

1
Q
  1. What does the indirection operator do?
A

It dereferences a pointer, allowing code to work with the value that the pointer
points to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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 ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. So far you have learned three different uses for the * operator. What are they?
A

Multiplication operator, definition of a pointer variable, and the indirection
operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What math operations are allowed on pointers?
A

Addition and subtraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Assuming that ptr is a pointer to an int , what happens when you add 4 to ptr ?
A

It adds 4 times the size of an int to the address stored in ptr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Look at the following array definition.
int numbers[] = { 2, 4, 6, 8, 10 };

What will the following statement display?
cout &laquo_space;*(numbers + 3) &laquo_space;endl;

A

8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What is the purpose of the new operator?
A

To dynamically allocate memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the purpose of the delete operator?
A

To free memory that has been dynamically allocated with the

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Under what circumstances can you successfully return a pointer from a function?
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What is the difference between a pointer to a constant and a constant pointer?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What are two advantages of declaring a pointer parameter as a constant pointer?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Each byte in memory is assigned a unique __________.
A

address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. The __________ operator can be used to determine a variable’s address.
A

address (&)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. _________ variables are designed to hold addresses.
A

pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. The __________ operator can be used to work with the variable a pointer points to.
A

indirection (*)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. Array names can be used as __________, and vice versa.
A

pointers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. Creating variables while a program is running is called __________.
A

dynamic memory allocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. The __________ operator is used to dynamically allocate memory.
A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. Under older compilers, if the new operator cannot allocate the amount of memory requested,
    it returns ________
A

0 or null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. A pointer that contains the address 0 is called a(n) __________ pointer.
A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. When a program is finished with a chunk of dynamically allocated memory, it should free it
    with the __________ operator
A

delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. You should only use pointers with delete that were previously used with __________.
A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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.

A

cout &laquo_space;*ptr &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Look at the following array definition.
int set[10];
Write a statement using pointer notation that stores the value 99 in set[7];

A

*(set + 7) = 99;

26
Q

Write code that dynamically allocates an array of 20 integers, then uses a loop to allow
the user to enter values for each element of the array

A

const int SIZE = 20;
int *ptr;
ptr = new int[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout &laquo_space;“Enter a value: “;
cin&raquo_space; ptr[i];
}

27
Q

Assume that tempNumbers is a pointer that points to a dynamically allocated array.
Write code that releases the memory used by the array.

A

delete [] tempNumbers;

28
Q

Look at the following function definition.
void getNumber(int &n)
{
cout &laquo_space;“Enter a number: “;
cin&raquo_space; n;
}
3
In this function, the parameter n is a reference variable. Rewrite the function so that n is a
pointer.

A

void getNumber(int *n)
{
cout &laquo_space;“Enter a number: “;
cin&raquo_space; *n;
}

29
Q
  1. Write the definition of ptr , a pointer to a constant int.
A

const int *ptr;

30
Q
  1. Write the definition of ptr , a constant pointer to an int.
A

int * const ptr;

31
Q

Each byte of memory is assigned a unique address.

A

true

32
Q

The * operator is used to get the address of a variable.

A

false

33
Q

Pointer variables are designed to hold addresses.

A

true

34
Q

The & symbol is called the indirection operator.

A

false

35
Q

The & operator dereferences a pointer.

A

false

36
Q

When the indirection operator is used with a pointer variable, you are actually
working with the value the pointer is pointing to

A

true

37
Q

Array names cannot be dereferenced with the indirection operator.

A

false

38
Q

when you add a value to a pointer, you are actually adding that number times the
size of the data type referenced by the pointer

A

true

39
Q

The address operator is not needed to assign an array’s address to a pointer.

A

true

40
Q

You can change the address that an array name points to.

A

false

41
Q

Any mathematical operation, including multiplication and division, may be
performed on a pointer

A

false

42
Q

Pointers may be compared using the relational operators.

A

true

43
Q

When used as function parameters, reference variables are much easier to work
with than pointers

A

true

44
Q

The new operator dynamically allocates memory.

A

true

45
Q

A pointer variable that has not been initialized is called a null pointer.

A

false

46
Q

The address 0 is generally considered unusable.

A

true

47
Q

In using a pointer with the delete operator, it is not necessary for the pointer to
have been previously used with the new operator.

A

false

48
Q
  1. int ptr* = nullptr;
A

The variable should be declared as int *ptr;

49
Q
  1. int x, *ptr = nullptr;
    &x = ptr;
A

The assignment statement should read ptr = &x;

50
Q
  1. int x, *ptr = nullptr;
    *ptr = &x;
A

The assignment statement should read ptr = &x;

51
Q

int x, *ptr = nullptr;
ptr = &x;
ptr = 100; // Store 100 in x
cout &laquo_space;x &laquo_space;endl;

A

The assignment statement should read *ptr = 100;

52
Q

Int numbers[] = {10, 20, 30, 40, 50};
cout &laquo_space;“The third element in the array is “;
cout &laquo_space;*numbers + 3 &laquo_space;endl;

A

The last line should read
cout &laquo_space;*(numbers + 3) &laquo_space;endl;

53
Q

int values[20], *iptr = nullptr;
iptr = values;
iptr *= 2;

A

Multiplication cannot be performed on pointers.

54
Q

float level;
int fptr = &level;

A

An int pointer is being used to reference a float value

55
Q

int *iptr = &ivalue;
int ivalue;

A

iptr cannot be initialized with the address of ivalue. ivalue is defined after
iptr

56
Q
  1. void doubleVal(int val)
    {
    *val *= 2:
    }
A

The * operator is used on the parameter, but it is not a pointer.

57
Q
  1. int *pint = nullptr;
    new pint;
A

The second statement should read pint = new int;

58
Q

int *pint = nullptr;
pint = new int;
if (pint == nullptr)
*pint = 100;
else
cout &laquo_space;“Memory allocation error\n”;

A

The program segment is storing a value at the address 0.

59
Q

int *pint = nullptr;
pint = new int[100]; // Allocate memory
.
.
Code that processes the array.
.
.
delete pint; // Free memorY

A

The last line should read delete [] pint;

60
Q

Int *getNum()
{
int wholeNum;
cout &laquo_space;“Enter a number: “;
cin&raquo_space; wholeNum;
return &wholeNum;
}

A

The function returns the address of a variable that no longer exists.

61
Q

const int arr[] = { 1, 2, 3 };
int *ptr = arr;
const int arr[] = { 1, 2, 3 };
int *ptr = arr;

A

The pointer definition should read:
const int *ptr = array;

62
Q

void doSomething(int * const ptr)
{
int localArray[] = { 1, 2, 3 };
ptr = localArray;
}

A

The function tries to change the contents of ptr, which is a constant pointer.
During the function’s execution, the value in ptr cannot be changed.