### Comp111: Operating Systems Classroom Exercise 5 Pointers Fall 2017

#### group member 1: ____________________________ login: ______________ group member 2: ____________________________ login: ______________ group member 3: ____________________________ login: ______________ group member 4: ____________________________ login: ______________ group member 5: ____________________________ login: ______________ group member 6: ____________________________ login: ______________ group member 7: ____________________________ login: ______________ group member 8: ____________________________ login: ______________

In class we have briefly discussed concepts and dangers inherent in pointers. Let's review some important concepts

1. Consider the following code.
```
int a[5]; int *p = a + 3; p[-2] = 20;
```
Draw a picture of memory after this code fragment, indicating contents of memory (and marking unknowns with ?'s).

2. Consider the following code:
```
const char *c = "hello"; char d[20]; int i;
for (i=4; i>=0; i--) d[4-i]=c[i];
d[5]=0; /*here*/
printf("%s\n",d);
```
What does this print? What happens if we leave out the line marked /*here*/?
3. Consider the following code:
```
struct bar { int none; };
void foo(void *v) {
struct bar *b = (struct bar *)v;
b[2].none = 1; b[4].none = 50;
}
main() {
void *d = (void *) malloc (5*sizeof(struct bar));  // the original had a '*'; Sorry!
foo(d);
}
```
1. Draw a picture of memory before the end of foo, identifying the values of d, b, and v and the memory to which they point. Indicate memory whose contents you don't know with '?'.

2. Do "d+1" or "*d" mean anything? Why or why not?

4. Considering the declarations:
```
struct foo {
int i;
struct foo *g;
const char *which;
} f, *h;
```
Which of the following statements will compile?
1. `f.g=&f;`
2. `f.which="ho there"; `
3. `h=&f;`
4. `h=f.g;`
5. `h=g.which;`
6. `h=(struct foo *)(g.which);`
5. (Advanced) In an operating system, it is often the case that the operating system stores something as type `void` and then a subsystem remembers its type when manipulating it. Why would the operating system wish to avoid remembering the appropriate type?