Chapter 10 Computer Programming w/ C++

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

indirection operator

(*) Will dereference a pointer variable. (The pointer variable will work with the value listed in memory address rather than just the memory address itself).

Member functions of class objects can be called through a pointer by either of these expressions.

(*ptr).fun(); ptr->fun();

The ______ operator has precedence over the + operator, so *numbers+1 is not equivalent to *(numbers+1).

*

What are the two ways to avoid memory leaks?

1. Whenever possible, the function that invokes new to allocate storage should also be the function that invokes delete to deallocate the storage. 2. A class that needs to dynamically allocate storage should invoke new in its constructors and invoke the corresponding delete in its destructor.

Legal or Illegal by using the new variable? cout<<*iptr; cin>>*iptr; total += *iptr;

Legal

A pointer is said to be _____________ if it is pointing to a memory location that has been freed by call to delete.

dangling

The increment operator adds the size of one _______ to the pointer, so it points to the next element in the array. The decrement operator subtracts the size of one ________ from the pointer.

data type

When you are working with a ___________, you are actually working with the value the pointer is pointing to.

dereferenced pointer

Its important to remember that the structure point operator is used to _________ a pointer to a structure or class object, not a pointer that is a member of a structure or class.

dereferences

When the indirection operator (*) is placed in front of a pointer variable name, it ____________________________ the pointer, thereby allowing you to actually work with the value to which the pointer variable points.

dereferences

In an array, all the elements are stored in consecutive memory locations, so the address of ________ is greater that the address of ____________.

element 1 , element 0

A program that has finished using a dynamically allocated block of memory should _______ the memory and _________ it to the heap to make it available for future use.

free and return

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

function parameter

Every call to new allocates storage from a special area of the program's memory called the ________.

heap

A constant pointer may not be changed after it has been __________.

initialized

Pointers may be __________ with the address of an existing object.

initialized

int *prt; can also be written as

int* prt;

When stepping through an array with a pointer, it's possible to give the pointer an address __________ of the array because of no bounds checking.

outside

Pointers to structures and class variables can be passed to functions as ____________. The function receiving the pointer can then use it to access or modify members of the structure.

parameters

__________ are critical when adding values to pointers.

parentheses

Array names can be used as ______________.

pointer constants

A _______ points to a constant item. The data that the pointer points to cannot change, but the pointer itself can change.

pointer to const

A variable that stores an address is called a _______________.

pointer variable

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

return

An array name, without brackets and a subscript, actually represents the ________________ of the array. This means that an array name is really a pointer to the first element of the array.

starting address

An element in an array can be retrieved by using its _________ or by adding its __________ to a pointer to the array.

subscript

What does the following statement compare? if (ptr1 < ptr2)

the addresses stored in the pointer variables

*ptr=100 is an example of

the indirection operator

T or F: A local variable that has not been initialized does not hold a valid address, and an attempt to use such a pointer will result in execution-time errors.

true

T or F: A pointer may be subtracted from another pointer.

true

Can mathematical operations be performed on pointers?

yes

The convention in older versions of C++ is to assign the address ______ to a pointer that does not currently point to a valid memory location.

zero

If one address comes before another address in memory, the first address is considered "____" the second.

less than

array[index] is equivalent to

*(array + index)

Legal or Illegal: int *pint = &myValue; int myValue;

Illegal because a pointer can only be initialized with the address of an object that has already been defined.

Legal or Illegal: float myFloat; int *pint= &myFloat;

Illegal because myFloat is not an int

Function prototype examples: void getNumber (int *); void doubleValue (int *);

It isn't necessary to specify the name of the variable for the pointer in the prototype but the * is required.

Legal or Illegal: int ages[20]; int *pint = ages;

Legal because ages is an array of integers

Legal or Illegal: int myValue; int *pint=&myValue;

Legal because myValue is an integer

Legal or Illegal: int myValue, *pint = &myValue;

Legal because the pointer is initialized with the address myValue

Legal or Illegal: int *ptrToint=NULL; double *ptrToDouble=NULL;

Legal because the pointer is not pointing to a valid memory location and you can use NULL in the place of 0.

Pointers to Class Object and Structures Example: class Rectangle { int width, height; }; You can declare a pointer to Rectangle and create a Rectangle object by writing Rectangle *pRect = nullptr; //Pointer to Rectangle Rectangle rect; ///Rectangle object And you can assign the address of rect to pRect as follows: pRect = &rect;

Now suppose that you want to access the members of the Rectangle object through the pointer pRect. You must use parentheses to force the indirection operator * to be applied first, as shown below: (*pRect).width The following statements will set the dimensions of the rectangle to 10 and 20. (*pRect).width = 10; (*pRect).height=20;

The getNumber function asks the user to enter an integer value. The following cin statement stores the value entered by the user in memory. cin>>*input;

The indirection operator causes the value entered by the user to be stored, not in input, but in the variable pointed to by input. Without this format, cin would store the value entered by the user in input, as if the value were an address. If this happens, input will no longer point to the number variable in function main.

Below is a definition of a function that uses a pointer parameter: void doubleValue(int *val) { *val *=2; }

The purpose of this function is to double the variable pointed to by val with the following statement. *val *= 2; This statement multiplies the original variable, whose address is stored in val, by two.

Here is an example of a call to the doubleValue function: doubleValue(&number);

This statement uses the address operator (&) to pass the address of number into the val parameter. After the function executes, the contents of number will have been multiplied by two.

*a.p Dereferencing Pointers to Structures

a is a structure variable or class object and p, a pointer, is a member of a. This expression accesses the value pointed to by a p.

What is this an example of? struct GradeInfo { string name;//student name int *testScores;//dynamically allocated array double average;//test average };

a structure declaration with an int pointer member

The contents of pointer variables may be changed with mathematical statements that perform ________________.

addition or subtraction

You can use the ________ to get the address of an individual element in an array. Example: &numbers[1] gets the address of numbers[1]

address operator

Pointer constants can be used as _____________.

array names

The address operator is not needed when an _______________ is assigned to a pointer.

array's address

The only difference between array names and pointer variables is that you __________ change the address an array name points to.

cannot

Pointers and dynamic memory allocation can be used with _________ and structures.

class objects

C++'s relational operators may be used to __________ pointer values.

compare

What is this an example of? int value = 22; int * const ptr = &value;

const pointer

A pointer to a _________ may not be used to change the value it points to.

constant

With a ___________ is the pointer itself that is constant. Once the pointer is initialized with an address, it cannot point to anything else.

constant pointer

What is this an example of? int value = 22; const int * const ptr = &value;

constant pointer to constant

Variables may be __________ and destroyed while a program is running.

created

Putting an empty pair of braces {} at the end of a variable definition initializes the variable to its ___________ value.

default

To free and return any dynamically allocated memory, one must use the __________ operator.

delete

What is this an example of? delete iptr; iptr=nullptr;

delete operator

What is this an example of? delete [ ] iptr; iptr = nullptr;

delete operator for a dynamically allocated array

Only use pointers with _______ that were previously used with _______.

delete, new

A function can safely return a pointer to dynamically allocated storage that has not yet been ________.

deleted

What is this an example of? iptr= new int [100];

dynamically allocating an array

What is this an example of? iptr= new int; *iptr=25;

dynamically allocating memory

How does C++ print addresses?

hexadecimal

Functions should not return pointers to _____________ because the storage for such variables is automatically delocated upon return.

local variables

Memory leaks are especially serious in ________.

loops

Dynamically allocating memory

means that a program, while running, asks the computer to set aside a chunk of unused memory large enough to hold a variable of a specific data type

A ________ is said to occur in your program if, after you have finished using a block of memory allocated by new, you forget to free it via delete.

memory leak

Every variable is assigned a ____________ whose address can be retrieved using the operator &.

memory location

Dynamically allocated class objects are used in programs that build and manage advanced data structures such as lists and binary trees. The _______ operator is used to allocate such objects in the same way that it is used to allocated variables of other types. Example: pRect = new Rectangle; pRect-> width = 10; pRect-> height =3; pRect = new Rectangle (10,30);

new

int *ptr is the same as

ptr is a pointer to an int data type

(*s).m Dereferencing Pointers to Structures

s is a pointer to a structure variable or a class object, and m is a member. The * operator deferences s, causing the expression to access the m member of the object *s. The expression is the same as s->m.

*(*s).p Dereferencing Pointers to Structures

s is a pointer to a structure variable or class object and p, a pointer, is a member of the object pointed to by s. This expression accesses the value pointed to by (*s).p.This expression is the same as *s->p.

*s->p Dereferencing Pointers to Structures

s is a pointer to a structure variable or class object and p, a pointer, is a member of the object pointed to by s. This expression accesses the value pointed to by s->p.

s->m Dereferencing Pointers to Structures

s is a structure pointer and m is a member. This expression accesses the m member of the structure pointed to by s.

When you add a value to a pointer, you are actually adding that number times the _____________ referenced by the pointer. Example: add one to numbers *(numbers +1) is the value at address numbers + 1 * 2 Times two because of the bytes of memory the data type stores. For this instance a short.

size of the data type

C++ provides the ____________ to use when you want to access a member of a class object through a pointer.

structure pointer operator -> Example: pRect->width=10; prect->height=20;

What does the following statement compare? if (*ptr1 < *ptr2)

the values that ptr1 and ptr2 point to

T or F: An integer may be added to or subtracted from a pointer variable. This may be performed with the + and - operators, or the += and =+ operators.

true

T or F: Many header files, including iostream, fstream, and cstdlib, define a constant named NULL to represent the pointer value 0.

true

T or F: Sometimes structures and classes contain pointers as members?

true

Are the following statements equivalent? int myInt=0; double myDouble = 0.0; int *ptrToInt = nullptr; equivalent to int myInt={ }; double myDouble = { }; int *ptrToInt = { };

yes

Does the heap deplete?

yes

Is this a valid way to determine whether the pointer points to a legitimate address? if(ptrToInt != nullptr) cout<<*ptrToInt; else cout<<"null pointer";

yes

Given double readings [20], totals [20]; double *dptr; Are these statements legal? dptr=readings; dptr=total;

yes because you are making dptr point to readings and totals

How do you avoid dangling pointers?

1. By setting pointers to null as soon as their memory is freed 2. Verifying that a pointer is not null before you attempt to access its memory

A pointer whose value is 0 is called a _________.

null pointer

The keyword _________ is used to indicate an invalid memory address. Example: int *ptrToint=nullptr; double *ptrToDouble=nullptr;

nullptr

How do you use the address operator? Example: &amount cout<<long(&amount) //displays the variable's address on the screen

place it before the variable whose address you want

A value that represents the address of memory location, or holds the address of some variable, is called a __________.

pointer

The address of a memory location is called a ___________.

pointer

A ______________ is a variable that holds address of memory locations.

pointer variable

An alternative to passing an argument by reference is to use a ____________ as the parameter.

pointer variable

________________can be used to accept array addresses as arguments.

pointer variables

Although a constant's address can be passed only to a pointer to const, a pointer to const can also ___________ the address of a non-constant item.

receive

When a variable is passed into a reference parameter, the argument is said to be passed by ________.

reference

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. You cannot leave the word const out of the definition of the rates parameter.

Example: void displayPayRates (const double *rates, int size)

Legal or Illegal: int *ptrToint=0; double *ptrToDouble=0;

Legal because the pointer is not pointing to a valid memory location.

Legal or Illegal: if (p != nullptr) if (p != NULL) if (p != 0)

Legal because the pointer will only be used if it does not evaluate to 0.

When the heap becomes depleted it will throw a ___________ exception to notify the calling program that the requested memory cannot be allocated.

bad_alloc

An ___________ us a mechanism for notifying a program that something has gone drastically wrong with the operation that was being executed and that the results of the operation cannot be trusted. The default of this is for the program to terminate.

exception

T or F: Comparing two pointers is the same as comparing the values the two pointers point to.

false

A variables address is the address of the _______ byte allocated to that variable.

first

The following definition defines an array, readings, and a pointer , marker, which is initialized with the address of the _______ first element in the array. double readings[50], *marker = readings;

first

You can use a pointer to indirectly access and _________ the variable being pointed to.

modify

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

new operator

Can all arithmetic operators be performed on pointers?

no

Can you use multiplication or division with pointers?

no

Given double readings [20], totals [20]; double *dptr; Are these statements legal? readings=totals; totals=dptr;

no because you cannot change readings or totals

T or F: Most comparisons involving pointers compare a pointer to 0, NULL, or nullptr to determine whether the pointer points to a legitimate address.

true

T or F: Pointers may be compared using any of the relational operators > < == != >= <=

true

T or F: Pointers may be defined in the same statement as other variables of the same type.

true

T or F: The ++ and -- operators may be used to increment or decrement a pointer variable.

true

T or F: The default value for numeric types such as int, long, and double is zero, while the default value for pointer types is nullptr.

true

T or F: You can test a pointer p against 0, NULL, or nullptr to determine if it points to a valid address using equality-testing operators != and ==.

true


Set pelajaran terkait

chapter 17 technical rescue support and vehicle extrication

View Set

ap world test #2 multiple choice questions

View Set

California Real Estate Practice Chapter 2 Rockwell Slides

View Set

ATI fundamentals practice test B

View Set

Chapter 1 Mini Sim on Strategic Marketing

View Set