1
h14
CS16 F16
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
9am or 10:30am
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h14: Homework 14

ready? assigned due points
true Thu 11/17 02:00PM Tue 11/22 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Reading: Read Chapter 9 and the lecture notes. If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”.

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL! FOR BEST RESULTS, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.

1.(3 pts) What is the freestore (also known as the heap) used for?

2.(2 pts) What operator returns unused memory to the freestore (heap)?

3.(2 pts) What C++ unary operator is the “de-referencing” operator?

4.(2 pts) What C++ unary operator is the “address-of” operator?

5.(9 pts) Consider the following code:

int a=3, b=5, *p=&a, *p2;

We can draw a diagram that shows the effect of this code in memory, like this.

pointerdiagram

Note that we are showing the contents of a and b, and we are showing where p1 and p2 point. Since p2 is uninitialized, we show it pointing to a question mark (?). If a regular int variable were uninitialized, we would show a question mark (?) inside the box for the variable. Your job is to draw similar diagrams for the code below (see next page). Please do not be messy and make your diagram very clear and readable.

int a=6, b=7, *p1=&b, *p2=&a;
p1 = p2;
*p1 = 8;
p2 = &b;


int a=2, b, *p1=&b, *p2=&a, *p3;
p3 = p2;
*p1 = 8;
p2 = p1;
p1 = p3;
*p2 = 4;
int a=2, b=3, *p1, *p2;
p2 = &a;
p1 = &b;
*p1 = *p1 + *p2;

6.(8 pts) What will be the output of this program?

#include <iostream>
using namespace std;
int main() {
    int *pc, c;
    
    c = 5;
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;

    pc = &c;    
    cout << "Address that pointer pc holds (pc): "<< pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
    
    c = 11;    
    cout << "Address pointer pc holds (pc): " << pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;

    *pc = 2; 
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;

    return 0;
}

7.Write a program (8 pts) that introduces int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information:

(a) The address of x and the value of x. (1 pt)

(b) The value of p and the value of *p. (1 pt)

(c) The address of y and the value of y. (1 pt)

(d) The value of q and the value of *q. (1 pt)

(e) The address of p (not its contents!). (1 pt)

(f) The address of q (not its contents!). (1 pt)

Print the program and the outputs of the program on a separate sheet of paper and attach that to this homework. Please do not use more than 1 page (two sided is ok, but one sided is preferred).