CS计算机代考程序代写 CSC209H Worksheet: malloc Basics

CSC209H Worksheet: malloc Basics
1. Each time a variable is declared or memory is otherwise allocated, it is important to understand how much memory is allocated, where it will be allocated and when it will be de-allocated. Complete the table below. (Note: some of the programs allocate more than one block of memory.)
Code Fragment
Space?
Where?
De-allocated when?
int main() {
int i;
}
sizeof(int)
stack frame for main
when program ends
int fun() {
float i;
}
int main() {
fun(); }
int fun(char i) {

}
int main() {
fun(¡¯a¡¯); }
int main() {
char i[10] = {¡¯h¡¯,¡¯i¡¯};
}
int main() {
char *i;
}
int main() {
int *i;
}
int fun(int *i) {

}
int main() {
int i[5] = {4,5,2,5,1};
fun(i); }
int main() {
int *i;
i = malloc(sizeof(int));
}
void fun(int **i) {
*i = malloc(sizeof(int)*7);
}
int main() {
int *i;
fun(&i);
free(i);
}

CSC209H Worksheet: malloc Basics
2. Trace the memory usage for the program below up to the point when initialize is about to return. We have set up both stack frames for you, and the location of the heap.
Section
Heap
Address
0x23c 0x240
0x244 0x248
Value
Label
#include
#include
// Initialize two parallel lists.
void initialize(int *a1, int *a2, int n) {
for (int i = 0; i < n; i++) { a1[i] = i; a2[i] = i; stack frame for initialize . . 0x454 0x458 0x45c 0x460 0x464 0x46c 0x470 0x474 0x478 0x47c 0x480 0x484 0x488 0x48c } } int main() { int numbers1[3]; int *numbers2 = malloc(sizeof(int) * 3); initialize(numbers1, numbers2, 3); for (int i = 0; i < 3; i++) { printf("%d %d\n", numbers1[i], numbers2[i]); } free(numbers2); return 0; } stack frame for main