Q1 C Memory Management
In which memory sections (CODE, STATIC, HEAP, STACK) do the following reside?
1 |
|
- 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 | int* ptr = malloc(4 * sizeof(int)); |
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 | struct ll_node { |
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.