0%

cs61c-disc02

Q1 C Memory Management

In which memory sections (CODE, STATIC, HEAP, STACK) do the following reside?

1
2
3
4
5
6
7
#define C 2
const int val = 16;
char arr[] = "foo";
void foo(int arg){
char *str = (char *) malloc (C*val);
char *ptr = arr;
}
  • arg resides in STACK, str resides in STACK
  • arr resides in STATIC, *str resides in HEAP
  • val resides in CODE, C resides CODE

What is wrong with the C code below?

1
2
3
int* ptr = malloc(4 * sizeof(int));
if(extra_large) ptr = malloc(10 * sizeof(int));
return ptr;

Memory leak if extra_large is TRUE.

Write code to prepend (add to the start) to a linked list, and to free/empty the entire list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct ll_node {
struct ll_node* next;
int value;
}
void free_ll(struct ll_node** list){
if (*list == NULL) {
return;
}
free_ll(&((*list) -> next));
free(*list);
*list = NULL;
}

void prepend(struct ll_node** list, int value){
struct ll_node *ll_p = (struct ll_node *)malloc(sizeof(struct ll_node));
if (ll_p == NULL) {
printf("memory exhausted\n");exit(1);
}
ll_p -> value = value;
ll_p -> next = *list;
*list = ll_p;
}

Note: list points to the first element of the list, or to NULL if the list is empty

2 MIPS Intro

a) make arr[2] <- 4
b) make $t0 <- 0
c) alignment error
d) alignment error
e) out of bounds
f) make arr[3] <- 6

In question 1, what other instructions could be used in place of each load/store without alignment errors?

In a, we can use lb, lh because the number is 4, which in 2’s complement is 00000100 where the rest is all zero. sw can be replaced by sb and sh also.
In b, lh can be replaced by lb and lw, the value to be transfered is zero.
In e, sw can be replaced by sh, sb. The value is 8.
In f, sw can be replaced by sh, sb because the value is 6

3 Translating between C and MIPS

Translate between the C and MIPS code. You may want to use the MIPS Green Sheet as a reference. In all of the C examples, we show you how the different variables map to registers – you don’t have to worry about the stack or any memory-related issues.