1
h14
CS16 S17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
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: Pointers

ready? assigned due points
true Tue 05/30 12:30PM Thu 06/01 12:30PM

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

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

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

2.(3 pts) What operator returns unused memory to the 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.(6 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). Draw the diagrams as many times as needed for each question (that is, show all the intermediate steps). Please do not be messy and make your diagram very clear and readable.

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 (10 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).