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
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 06/04/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

### Page1 / 10

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online