# IC221: Systems Programming (SP17)

Home Policy Calendar Units Assignments Resources

# HW 5: Dynamic Memory

## Instructions

• You must turn in a sheet of paper that is neatly typed or written answering the questions below. (You are strongly encouraged to type your homework.)

You can use the following tex file to get started with preparing your homework.

• This homework is graded out of 100 points. Point values are associated to each question.

## Questions

1. (20 points) Consider the program below (and, yes!, you should run this program to understand it further):

```#include <stdio.h>
#include <stdlib.h>

int * makearray(int size,int base){

int array[size];
int j;

for(j=0;j<size;j++)
array[j] = base*=2; //doubling base

return array;
}

int main(){
int * a1 = makearray(5,2);
int * a2 = makearray(10,3);
int j, sum=0;

for(j=0;j<5;j++){
printf("%d ",a1[j]);
sum+=a1[j];
}
printf("\n");

for(j=0;j<10;j++){
printf("%d ",a2[j]);
sum+=a2[j];
}
printf("\n");

printf("SUM: %d\n", sum);
}
```
1. This program has a memory violation. Identify the memory violation and explain it.
2. Using a dynamic memory allocation (e.g., with `calloc()` or `malloc()`), rewrite the `makearray()` function to remove the memory violation.
3. Explain how your correction to `makearray()` removes the memory violation.
2. (15 points) For the code below, draw the function stack diagram at each push (function call) and pop (function return)

```int times(int a, int b){
return a*b;
}

return a+b;
}

int sub(int a, int b){
}

int main(){
sub(i,6);
}
```

Diagram start:

```push main      |____main____|

|____main____|

...
```
3. (10 points) Consider allocating an array of 16 long's: write two C expression using sing `malloc()` and one using `calloc()`.

```long * larray = /*allocate with calloc and mallc*/
```
4. (10 points) What is one advantage of using `malloc()` over `calloc()`? What is one advantage of using `calloc()` over `malloc()`?
5. (20 points) Consider the following program, complete the deallocation routine such that there are no memory violations/leaked. (Yes! You should try programming it.)

```#include <stdio.h>
#include <stdlib.h>

typedef struct{
int * a; //array of ints
int size; //of this size
} mytype_t;

mytype_t ** allocate(int n){
mytype_t ** mytypes;
int i,j;

mytypes = calloc(n,sizeof(mytype_t*));
for(i=0;i<n;i++){
mytypes[i] = malloc(sizeof(mytype_t));

mytypes[i]->a = calloc(i+1,sizeof(int));

for(j=0;j<i+1;j++){
mytypes[i]->a[j] = j*10;
}

mytypes[i]->size = i;
}

return mytypes;
}

void deallocate(mytype_t ** mytypes){

/*Complete me*/

}

int main(){
int i,j;
mytype_t ** mytypes;

mytypes = allocate(10);

for(i=0;i<10;i++){
printf("mytpyes[%d] = [",i);
for(j=0;j<mytypes[i]->size;j++){
printf(" %d", mytypes[i]->a[j]);
}
printf(" ]\n");
}

deallocate(mytypes);
}
```
6. (15 points) Consider the code below that prints the bytes of the integer `a` in hexadecimal.

```#include <stdio.h>
#include <stdlib.h>

int main(){
unsigned int a = 0xcafebabe;
unsigned char *p = (unsigned char *) &a;
int i;

for(i=0;i<4;i++){
printf("%d: 0x%02x\n", i, p[i]);
}

}
```
1. What is the output?
2. Explain the output using the terms "Big Endian" and "Little Endian".