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
(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); }
- This program has a memory violation. Identify the memory violation and explain it.
- Using a dynamic memory allocation (e.g., with
calloc()
ormalloc()
), rewrite themakearray()
function to remove the memory violation. - Explain how your correction to
makearray()
removes the memory violation.
(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; } int add(int a, int b){ return a+b; } int sub(int a, int b){ return add(a,-b); } int main(){ int i = times(add(1,2),5); sub(i,6); }
Diagram start:
push main |____main____| push add | add | |____main____| ...
(10 points) Consider allocating an array of 16 long's: write two C expression using
malloc()
and one usingcalloc()
.long * larray = /*allocate with calloc and mallc*/
- (10 points) What is one advantage of using
malloc()
overcalloc()
? What is one advantage of usingcalloc()
overmalloc()
? (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); }
(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]); } }
- What is the output?
- Explain the output using the terms "Big Endian" and "Little Endian".