C++ Chapter 9 Review Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What does the indirection operator do?

The * (indirection) operator dereferences the value referred to by the pointer type operand, and it is used to access the value stored in an address.

When a program is finished with a chunk of dynamically allocated memory, it should free it with the _______ operator.

delete

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

delete []tempNumbers;

T/F: The * operator is used to get the address of a variable.

false, * is used to get the value at that address if specified to a pointer variable.

What is the difference between a pointer to a constant and a constant pointer?

A pointer to constant may not be used to change the value it points to. A constant pointer may not be changed after it has been initialized.

Each byte in memory is assigned a unique ____

Address

T/F: The address operator is not needed to assign an array's address to a pointer.

True

True or false: Assuming numbers[], *(numbers+3) is the same as numbers[3]

True

T/F: The new operator dynamically allocates memory.

True by definition of new operator.

Write the definition of ptr, constant pointer to an int.

int value; int *const ptr = &value;

T/F: Each byte of memory is assigned a unique address

true

What are the three different uses for the * operator?

Multiplication operator, pointer definition, indirection operator

What math operations are allowed on pointers?

addition and subtraction

T/F: The & symbol is called the indirection operator.

False, the * is an indirection operator.

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

False, as delete operator is used to free memory that was allocated with new.

T/F: The address 0 is generally considered unusable.

False, by definition of null pointers.

T/F: Any mathematical operation, including multiplication and division, may be performed on a pointer.

False, pointer doesn't support all arithmetic operations.

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?

If new fails, it throws bad_alloc exception. In older computers, new returns value 0

What is the purpose of the new operator?

allocate memory dynamically at runtime

Write the definition of ptr, a pointer to a constant int.

const int value; int *ptr = &value;

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 << *ptr;

What is the purpose of the delete operator?

deallocate the memory allocated by the new operator

Array names can be used as ____, and vice versa.

pointers

The ____ operator can be used to determine a variable's address.

&

The ________ operator can be used to work with the variable the a pointer points to

*

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

*(set + 7) = 99;

Under older compilers, if the new operator cannot allocated the amount of memory requested, it returns _______.

0 or NULL

What circumstances can you successfully return a pointer from a function?

1. When a pointer variable is passed into a function as a parameter 2. A pointer is indicated to dynamically allocated memory

T/F: Array names cannot be dereferenced with the indirection operator.

False

T/F: You can change the address that an array name points to.

False, an array points to a memory location but these are constant pointers.

T/F: A pointer variable that has not been initialized is called a null pointer.

False, as a pointer that contains the address 0 is called a null pointer.

T/F: The & operator dereferences a pointer.

False, as by definition of the address operator.

________ variables are designed to hold addresses

Pointer

Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?

The value 16 is added to the memory address of ptr.

T/F: 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.

True, as pointers do not work like regular variables when used in mathematical statements.

T/F: When used as function parameters, reference variables are much easier to work with than pointers.

True, as reference variable acts as an alias to original variable used as an argument.

T/F: When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to.

True, by definition of the indirection operator.

T/F: Pointer variables are designed to hold addresses.

True, by definition they are designed to hold addresses.

T/F: Pointers may be compared using relational operators.

True, relational operators can be used in comparing two pointers.

With the following code, what would be displayed if you sent *iptr to cout? int x = 7; int *iptr = &x;

address of x

Creating variables while a program is running is called ____

memory allocation

The ______ operator is used to dynamically allocate memory

new

You should only use pointers with delete that were previously used with _______.

new

A pointer that contains the address 0 is called a(n) _____ pointer.

null


Set pelajaran terkait

Criminal Justice 100 - Exam one book questions

View Set

Discuss the four key functions of management.

View Set

ASTRONOMY 1010 Questions from Quizzes

View Set

Chapter 5: The Integumentary System

View Set

Suspicious Activity and Criminal Violations

View Set

Understanding Nutrition Midterm, Chapters 1 -7

View Set