Exam2-spring02

# Exam2-spring02 - EE 312 Sp'02 Exam 2 NAME Section 1 Write...

This preview shows pages 1–3. Sign up to view the full content.

EE 312 Sp ’02 Exam 2 NAME: Section 1: Write the following programs. Exactly one of the programs is impossible (cannot be written in C). For the impossible program, you must write “ this is impossible ”. If you attempt the impossible program, you will receive zero points for that problem. If you say that a possible program is impossible, you will receive zero points for that problem. Note that the “impossible” problem is worth only 5 pts (no partial credit), even though it is labeled as a 10 point problem. If you would like to ignore my suggested steps for any problem and write your own solution from scratch, you may do so, but you will not receive partial credit if you do (all or nothing). 1. (10 pts) Write a function isInt that behaves as follows. The function has one parameter. The type of the parameter is void * If the variable pointed to by the parameter is an int variable, then return true . If the variable pointed to by the parameter is anything other than an int variable, then return false . So, for example, if the function were called from the following main routine, the first invocation of the function would return true, and the second would return false: int main(void) { int n; double x; if (isInt(&n)) { printf(“hello world\n”); } if (isInt(&x)) { printf(“never printed\n”); } } You do not need to worry about “type cast” expressions for this problem. You will not lose points for an incorrect type cast, or for leaving a type cast out when it is required (so, I suggest that you just leave them all out and don’t worry about them). int isInt(void* p) { // return true if p points to an int /* Step 1: determine the size of the variable pointed to ( int s are 4 bytes) */ int size; // set this to the size of the variable pointed to by p /* Step 2: determine if *p is 2’s complement ( int s are 2’s complement) and return true or false as appropriate */ }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. (10 pts) Write the function realloc . The function has two parameters as follows. The first parameter, p , is an address previously returned by malloc . The chunk that p points into is current “in use” (has not yet been free’d). The second parameter, new_size , is the number of bytes desired for the chunk. When realloc is called, the function must determine the actual size of the chunk. If the actual size is the same or larger than new_size then realloc returns without doing anything. If the actual size is smaller than new_size then realloc allocates a new chunk that has at least new_size bytes available. All data from the original chunk is copied into the new chunk and then the original chunk is deallocated. The function realloc always returns a pointer to the first byte of the final chunk. If a new chunk was allocated, realloc returns the address of the first byte in this new chunk. If a new chunk was not allocated, then realloc returns p . You should assume that a Knuth style heap is being used (just as described in class). You can call malloc and/or free in your solution (please do not write malloc and free ). Since realloc is a bit complicated, we’ll ask you to write the function in stages.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern