Chapter2 - McMaster CAS Dept.

Chapter 2:
Review questions
1. Define each of the following terms: pointer, array, record.
A pointer is simply the internal machine address of a value inside the computer’s memory.
Array is an order collection of data values, each of which has the same type
Records: a record is a collection of data values that represents some logically coherent whole.
2. What type definition would you use to define a new enumeration type polygonT consisting of the
following elements: Triangle, Square, Pentagon, Hexagon, Octagon? How would you change the
definition so that internal representation for each constant name corresponded to the number of sides
for that polygon?
Enum polygonT{ Triangle, Square, Pentagon, Hexagon, Octagon};
Enum polygonT{ Triangle=3, Square=4, Pentagon=5, Hexagon=6, Octagon=8};
3. What three advantages are cited in the text for defining a new enumeration type as opposed to
defining named constants to achieve an equivalent effect?
a) the programmer does not need to specify the internal code explicitly; b) there is a separate type name
often makes the program easier to read. C) it is easier to debug
4. True or false: In C++, you may apply any operation defined on integers to values of any scalar type.
False:, example 2.5%3
5. At first glance, the following function looks very much like RightFrom, which is
defined in the section on “Scalar types”:
directionT LeftFrom(directionT dir) {
return directionT((dir - 1) % 4);
}
The RightFrom implementation works fine, but this one has a small bug. Identify the problem, and then
rewrite the function so that it correctly calculates the compass direction that is 90 degrees to the left of
dir.
When the input dir =0; the return value will have mistake,since -1%4
6. In your own words, explain what the keyword struct does.
Struct is used to define a structure type or we could say this type definition specifies what fields make up
the record.
7. Define the following terms: bit, byte, word, address.
Bit is a unit of memory that has only two possible states. byte is 8 bits. On most machines, byte are
assembled into larger structures called words. Some computer use two-tyte words, some computer used
four-byte words. Within the memory system, every bytes is identified by a numeric address.
8. True or false: In C++, a value of type int always requires four bytes of memory.
False, different machine used different usages
9. True or false: In C++, a value of type char always requires one byte of memory.
True
http://jk-technology.com/c/inttypes.html#char
10. What is the purpose of the sizeof operator? How do you use it?
Return the number of bytes required to store a value of type sizeof x; sizeof(int);
11. What reasons for using pointers are cited in this chapter?
Pointer allow you to refer to a larger data structure in a computer way. Pointers make it possible to
reserve new memory during program execution. Pointers can be used to record relationships among data
items.
12. What is an lvalue?
Any expression that refers to an internal memory location capable of storing data is called an lvalue
(pronounced “ell-value”). E.g x=1.0; x is lvalue.
13. What are the types of the variables introduced by the following declaration: int * p1, p2;
P1 is pointer to a integer; p2 is a integer.
14. What declaration would you use to declare a variable named pFlag as a pointer to a Boolean value?
bool *pFlag;
15. What are the two fundamental pointer operations? Which one corresponds to the term
dereferencing?
 A pointer type usually includes two fundamental pointer operations, assignment and dereferencing.
Two operator is
&(Address of): & takes an lvalues as its operand and returns the memory address in which that lvalue is
stored
* (Value-pointed-to): * operator takes a value of any pointer and returns the lvalue to which it points. This
operation is called dereferencing the pointer
16. Explain the difference between pointer assignment and value assignment.
Inital int x=42 ,y=163 ; int *p1,*p2;
P1=&x; p2 = &y;
Pointer assignment: p1=p2; change the arrow leading from p1 so that it point to the same memory
address as the p2 pointer to.
Value assignment: *p1=*p2; copy the value from the memory location by p2 into the location addressed
by p1. Then x = 163.
17. Draw diagrams showing the contents of memory after each line of the following code: (refer to the
code)
v1 = 10; v2 = 25; p1 = &v1; p2 = &v2;*p1 += *p2;p2 = p1;*p2 = *p1 + *p2;
18. What is the internal representation of the constant NULL? Zero
19. What does the phrase call by reference mean?
The use of reference parameters makes it possible for functions to change values in the frame of their
caller. This mechanism is referred to as call by reference. Page35
20. Write array declarations for the following array variables:
a. An array realArray consisting of 100 floating-point values: float realArray[100];
b. An array inUse consisting of 16 Boolean values: bool inUse[16];
c. An array lines that can hold up to 1000 strings: string lines[1000];
Remember that the upper bounds for these arrays should be defined as constants to make them easier to
change.
21. Write the variable declaration and for loop necessary to create and initialize the following integer
array:
int square[10],i;
for(i=0;i<=10;i++){
square[i]=i*i;
}
22. What is the difference between allocated size and effective size?
In C++, index numbers in an array always begin with 0. Arrays are declared by specifying a constant size in
square brackets after the name of the array. The size specified in the declaration is called the allocated
size of the array and is typically larger than the effective size, which is the number of elements actually in
use.
23. Assuming that the base address for rectangular is 1000 and that values of type int require four bytes
of memory, draw a diagram that shows the address of each element in the array declared as follows:
int rectangular[2][3];
1000
1004
1008
1012
1016
1020
24. Write a variable declaration that you could use to record the state of a chessboard, which consists
of an 8 x 8 array of squares, each of which may contain any one of the following symbols:
Char chessboard[8][8];
Int i,j;
For(i=0;i<8;i++){
Chessboard[1][i]=’p’;
Chessboard[6][i]=’P’;
}
... ...
25. Assuming that intArray is declared as int intArray[10]; and that j is an integer variable, describe the
steps the computer would take to determine the value of the following expression:
&intArray[j + 3];
Check j+3 , then &intArray[j+3] is the address for store intArray[j+3]
26. True or false: If arr is declared to be an array, then the expressions arr and &arr[0] are equivalent.
True: both arr and &arr[0] will return the address of arr[0].
Try:
Int square[5];
Square[0]=1; Square[0]=2;
cout<<"arr is "<<square<<",&arr[0] is "<<&square[0]<<endl;
output: arr is 0x28ff00 ,& arr[0]= 0x28ff00;
27. Assume that variables of type double take up eight bytes on the computer system you are using. If
the base address of the array doubleArray is 1000, what is the address value of doubleArray + 5?
1000+8*4=1032
28. Describe the distinction between the declarations int array[5]; and int *p;
Int array[5] define a array with length 5, and int *p define a pointer which point to a type integer.
29. What steps are necessary to declare a record variable? (page68)
1. Define a new structure type; 2 declare variables of the new type
30. If the variable p is declared as a pointer to a record that contains a field called cost, what is wrong
with the expression *p.cost as a means of following the pointer from p to its value and then selecting
the cost field? What expression would you write in C++ to accomplish this dereference and select
operation?
Since *p.cost will means *(p.cost), not (*p).cost. So we need to use p->cost.
31. What is the heap?
Refer to page 71. The pool of unallocated memory available to a program is called the heap.
32. Describe the effect of the following operators: new and delete.
New is used to allocate memory from the heap. And delete takes a pointer previously allocated by new
and returns the memory associated with that pointer to the heap.
33. What is the purpose of the delete[] operator?
Delete[] operator is used to free storage that was allocated using the new[] operator;
For example: int *arr = new int[45];
Delete[] arr;
34. What is meant by the term garbage collection?
Garbage collection: a system for dynamic allocation that actively goes through memory to see what parts
of it are use, freeing any storage that is no longer needed.