CS 449 – Midterm 2 Practice Problems

Answers in red

1.)  For the program below, what will the stack look like if execution is paused at the line marked HERE? (You may ignore padding and alignment.)

 

int g(char *a) {

       //HERE

       return strlen(a);

}

 

 

int main()

{

       int x;

       x = g(“hello”);

       return 0;

}

 

 

Stack grows down

 

Main’s EBP

 

Return address to main

 

Pointer to “hello”

 

x

 

Old EBP

 

 

2.) Answer the following about a memory region of 20 MB that has a chunk size of 1MB that is managed with a linked list using best fit allocation.

a.)  Assume the region is initially empty. Show the list after allocations of size 4, 6, 3, and 2 (in that order).

 

 

 


b.) From the list in part (a), free the 6 and 3 MB chunks. What is the resulting linked list?

 

 

 


1

Free

 

4

Used

 

2

Used

 

9

Free

 

4

Used

 
c.) Finally, a request for a chunk of size 4 occurs. Show the linked list from part (b) with the new allocation.

 

 

 

 

 

3.) Write a macro that finds the minimum of its three arguments.

There are several ways to do this:

#define MIN(a, b, c) ((a) < (b))? (((a) < (c)) ? (a) : (c)) : (((b) < (c)) ? (b) : (c))

 

or

 

#define MIN2(a, b) (((a) < (b)) ? (a) : (b))

#define MIN(a, b, c) MIN2(MIN2(a, b), MIN2(b, c))

 

Etc.