Chapter 9 Pointers

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

Notice the ___ ____ is not needed when an array's address is assigned to a pointer. Because the name of an array is already an address, use of the & operator would be incorrect.

address operator

When a program is finished using a dynamically allocated chunk of memory, it should release it for future use. The ______ operator is used to free memory that was allocated with new.

delete

It's important to know, however, that pointers ___ __ __ like regular variables when used in mathematical statements.

do not work

The capability of comparing addresses gives you another way to be sure a pointer ___________________

does not go beyond the boundaries of an array

"int *ptr;" In this definition, the word int ___ ___ ___ ptr is an integer variable. It means ptr can hold the address of an integer variable. Remember, pointers only hold one kind of value: an address.

does not mean

In an array, all the elements are stored in consecutive memory locations, so the address of element 1 is ______ than the address of element 0.

greater

An ____ may be added to or subtracted from a pointer variable. This may be performed with the + and − operators, or the += and −= operators.

integer

The address operator (&) returns the memory address of a variable.

operator (&)

Constant pointers must be initialized with a __________

starting value

The ___ operators may be used to increment or decrement a pointer variable.

++ and −−

______________________. The data that the pointer points to cannot change, but the pointer itself can change.

A pointer to const points to a constant item

If you are using an older compiler that does not support the C++ 11 standard, you should initialize pointers with the integer 0, or the value ____. The value NULL is defined in the <iostream> header file (as well as other header files) to represent the value 0.

NULL

To dynamically allocate memory means that a program, while running, asks the computer to set aside a __________ large enough to hold a variable of a specific data type.

chunk of unused memory

In passing the address of a constant into a pointer variable, the variable must be defined as a pointer to a _______.

constant

array name, without brackets and a subscript, actually represents the starting address of the array. This means that an array name is really a ______.

pointer.

A reference variable acts as an alias for another variable. It is called a reference variable because it ______ another variable in the program. Anything you do to the reference variable is actually done to the variable it references.

references

Functions can return pointers, but you must be sure the item the pointer references _________.

still exists

The definition of a pointer variable looks pretty much like any other definition. The ____ in front of the variable name indicates that is a pointer variable.

asterisk (int *ptr;)

A smart pointer _________ a chunk of dynamically allocated memory when the memory is no longer being used.

automatically deletes

pointer can only be initialized with the address of an object that has already been ______.

defined

The address of the variable x is displayed in _______. This is the way addresses are normally shown in C++.

hexadecimal

Memory addresses ________ specific locations in the computer's memory.

identify

Pointer variables also allow you to work with the data that they ___ ___.

point to

In C++, when you add a value to a pointer, you are actually adding that ___________________ being referenced by the pointer.

value times the size of the data type

In C++ 11, you can use smart pointers to dynamically allocate memory and _______________________ when you are finished using it.

not worry about deleting the memory

When a pointer is set to the address 0, it is referred to as a null pointer because it points to "______."

nothing

In C++ 11, the ____ key word was introduced to represent the address 0.

nullptr

it is a good idea to initialize pointer variables with the special value ____.

nullptr

Remember that C++ performs no bounds checking with arrays. When stepping through an array with a pointer, it's possible to give the pointer an address ___ ____ ___ ___.

outside of the array

When the address operator (&) is ___ ____ of a variable name, it returns the address of that variable. Here is an expression that returns the address of the variable amount: &amount

placed in front

With pointer variables, you can ____ manipulate data stored in other variables.

indirectly

The real benefit of pointers is that they allow you to _________.

indirectly access and modify the variable being pointed to

You can, however, use the address operator to get the address of an _______________

individual element in an array.

if a constant pointer is used as a function parameter, the parameter will be__________________, and cannot be changed to point to anything else while the function is executing.

initialized with the address that is passed as an argument into it

A pointer may be _______ from another pointer.

subtracted

when you use a pointer variable to store a value in the memory location that the pointer references, your code has to specify that ___ ___ ___ ___ ___in the location referenced by the pointer variable, and not in the pointer variable itself.

the value should be stored

Pointer variables can also be used to accept __________ as arguments. Either subscript or pointer notation may then be used to work with the contents of the array.

array addresses

when we pass an array as an argument to a function, we are actually passing the array's ___ ___.

beginning address

When you are writing a function that uses a pointer parameter, and the function is not intended to change the data the parameter points to, it is always a good idea to make the parameter a pointer to ______ .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 nonconstant arguments.

const

reference variables are much ______ to work with than pointers. Reference variables hide all the "mechanics" of dereferencing and indirection.

easier

When writing functions that return pointers, you should take care not to create _________.

elusive bugs

The following statement, however, compares the values that ptr1 and ptr2 point to:

if (*ptr1 < *ptr2)

For example, the following if statement compares the addresses stored in the pointer variables ptr1 and ptr2:

if (ptr1 < ptr2)

When working with arrays, remember the following rule: array[index] ___ ___ ___ *(array + index)

is equivalent to

The _______ are critical when adding values to pointers. The * operator has precedence over the + operator, so the expression *number + 1 is not equivalent to *(number + 1).

parentheses

Another way to pass an argument by reference is to use a _____ as the parameter.

pointer variable

When memory cannot be dynamically allocated, C++ throws an exception and terminates the program. Throwing an exception means the______________________.

program signals that an error has occurred

Once you have defined a unique_ptr, you can use it in a similar way as a ________.

regular pointer

Pointers may be defined in the ______ as other variables of the same type.

same statement (int myValue, *pint = &myValue;)

A __________ can share ownership of a piece of dynamically allocated memory. Multiple pointers of the shared_ptr type can point to the same piece of memory. The memory is deallocated when the last shared_ptr that is pointing to it is destroyed.

shared_ptr

You should return a pointer from a function only if it is:

-A pointer to an item that was passed into the function as an argument. - A pointer to a dynamically allocated chunk of memory.

To use any of the smart pointers, you must #include the __________

<memory> header file

___ ___ ___ the address operator with the & symbol used when defining a reference variable.

Do not confuse

_____________________________________. Once the pointer is initialized with an address, it cannot point to anything else.

With a const pointer, it is the pointer itself that is constant

A pointer can be used as a function parameter. It gives the function ______ to the original argument, much like a reference parameter does.

access

The contents of pointer variables may be changed with mathematical statements that perform ____ ___ __.

addition or subtraction

If one address comes before another address in memory, the first address is considered "_______" the second. C++'s relational operators may be used to compare pointer values.

less than

Pointer variables are similar to reference variables, but pointer variables operate at a _____ level.

lower

In C++, pointer variables are yet another ____ for using memory addresses to work with pieces of data.

mechanism

This statement defines a unique_ptr named ptr that points to a dynamically allocated int. Here are some details about the statement: The notation <int> that appears immediately after unique_ptr indicates that the pointer can point to an int. The name of the pointer is ptr. The expression new int that appears inside the parentheses allocates a chunk of memory to hold an int. The address of the chunk of memory will be assigned to the ptr pointer.

unique_ptr<int> ptr( new int );

A ___________ does not own the memory it points to, and cannot be used to access the memory's contents. It is used in special situations where the memory pointed to by a shared_ptr must be referenced without increasing the number of shared_ptrs that own it.

weak_ptr

When you apply the indirection operator (*) to a pointer variable, you are working not with the pointer variable itself, but _____________.

with the item it points to

The only difference between array names and pointer variables is that _______________________________.

you cannot change the address an array name points to

_______ _____ is allocated a section of memory large enough to hold a value of the variable's data type. On a PC, for instance, it's common for 1 byte to be allocated for chars, 2 bytes for shorts, 4 bytes for ints, longs, and floats, and 8 bytes for doubles.

Every variable

It is a good practice to set a pointer variable to nullptr ___________. First, it prevents code from inadvertently using the pointer to access the area of memory that was freed. Second, it prevents errors from occurring if delete is accidentally called on the pointer again. The delete operator is designed to have no effect when used on a null pointer.

after using delete on it

Array names ___ ___ ___. You can't make them point to anything but the array they represent.

are pointer constants

Smart pointers support the same * and −> dereferencing operators as regular pointers, but smart pointers do not support pointer _______.

arithmetic

Sometimes it is necessary to pass the address of a const item into a pointer. When this is the case, the pointer must be defined as a pointer to a ________.

const item

Array names can be used as ___ _____, and pointers can be used as array names.

constant pointers

*number + 1 adds one to the ________ of the array, while *(number + 1) adds one to the address in number, then dereferences it.

contents of the first element

When you are writing a program that will need to work with an unknown amount of data, ___ ____ allocation allows you to create variables, arrays, and more complex data structures in memory while the program is running.

dynamic memory

the program to create its own variables "on the fly." This is called ______________, and is only possible through the use of pointers.

dynamic memory allocation

In order to make a pointer variable reference another item in memory, you have to write code that fetches the ____ ____ of that item and assigns the address to the pointer variable.

memory address

Pointer variables, which are often just called pointers, are designed to hold ___ _____.

memory addresses

Failure to release dynamically allocated memory can cause a program to have a __________.

memory leak

The way a C++ program requests dynamically allocated memory is through the _____________.

new operator

Although a constant's address can be passed only to a pointer to const, a pointer to const can also receive the address of a ________.

nonconstant item

Comparing two pointers is ______as comparing the values the two pointers point to.

not the same

Remember a pointer is designed to point to an object of a specific data type. When a pointer is initialized with an address, it must be_______________________________________.

the address of an object the pointer can point to

When you are working with a dereferenced pointer, you are actually working with __________.

the value the pointer is pointing to

In C, the only way to pass a variable by reference is_____.

to use a pointer

Only use pointers with delete that were previously used with new. If you use a pointer with delete that does not reference dynamically allocated memory, __________________________!

unexpected problems could result

You can define an ____________, then assign it a value in a later statement.

uninitialized unique_ptr

Each byte of memory has a __ ___. A variable's address is the address of the first byte allocated to that variable.

unique address

A __________ is the sole owner of a piece of dynamically allocated memory. No two unique_ptrs can point to the same piece of memory. When a unique_ptr goes out of scope, it automatically deallocates the piece of memory that it points to.

unique_ptr

C++ 11 provides three types of smart pointer:

unique_ptr, shared_ptr, and weak_ptr.


Set pelajaran terkait

BNAD 276 Connect Answers Chp 6-8

View Set

Chapter 11: Differential Analysis

View Set

NUR 417 Genomics in Nursing Quiz #1

View Set

MGMT 329 CH3 EQUAL EMPLOYMENT OPPORTUNITY 3.1-3.7

View Set

Chapter 13: The Immune Response and Lymphatic System

View Set