NOTES: * Some questions in this test are intentionally phrased ambiguous. Where you sense such ambiguities, you are encouraged to make reasonable assumptions and state those assumptions clearly. * You are encouraged to document your observations on issues such as performance, maintainability and architecture dependencies of the code segments you encounter in this test. Such comments may be about the questions themselves, about the answers you provide or both. 1. Consider the following declarations: typedef struct { int a; int b; int c; } ABC; typedef struct { int d; int e; int f; ABC *abc; } DEF; Given the above declarations, fill in the body of the following two functions given below: // The create function should use a single call to malloc to allocate memory // for structure DEF and its constituting structure ABC. In other words, after // a call to the CreateDEF() function, the following statements should be valid // and should work without segmentation violations: // DEF* pDEF = CreateDEF(); // pDEF-->abc-->a = 100 ; DEF *CreateDEF ( void ) { } // Note that this function takes a pointer to structure ABC. void FreeDEF (ABC *pABC) { } 2. Comment on the following code segment, in light of the previous definitions: Will it compile on an ANSI compliant compiler? Will it execute without faults and, if so, what does it do? DEF *pdef = CreateDEF(); int value; value = (int) pdef; value += 8; *(int *) value = 10; 3. Consider four components of a color where: unsigned char ucRed = 0x10; unsigned char ucGreen = 0xFF; unsigned char ucBlue = 0x0F; unsigned char ucAlpha = 0x44; We would like this laid out in memory in the ARGB format. In the figures below show how the color components would be laid out in memory (print letters R, G, B, and A in the appropriate boxes below) on architectures with (a) Little Endian Layout: |---------------|---------- --|-----------|------- ------| | | | | | | | | | | |---------------|---------- --|-----------|------- ------| (b) Big Endian Layout: |---------------|---------- --|-----------|------- ------| | | | | | | | | | | |---------------|---------- --|-----------|------- ------| Write a function to generate a packed color ARGB which is a 32 bit integer on a little endian machine: unsigned int PackBytes ( unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue, unsigned char ucAlpha ) { } 4. Consider the following incorrect code segment. It has two pieces to it: PRINT_THREE_TIMES macro and the code that uses the macro: #define PRINT_THREE_TIMES \ printf ("one: %d\n", 1); \ printf ("two: %d\n", 2); \ printf ("three: %d\n", 3); \ if (i == 1) printf( “Only once: %d\n”, 1 ); else if (i == 2) printf( “i == 2, we don’t need to print anything here!\n”) ; else if (i == 3) PRINT_THREE_TIMES else (i == 4) printf( “i == 4, we don’t need to print anything here!\n”) ; The code does not quite do what is intended. There are two ways to fix it: either by changing the macro PRINT_THREE_TIMES or by changing the rest of the code segment. a) First, show how to do this by changing the macro alone. (You are not allowed to consolidate the three ‘printf’ statements to a single one.) #define PRINT_THREE_TIMES \ b) Second, show how it can be fixed without changing the macro. You may change the rest of the code. #define PRINT_THREE_TIMES \ printf ("one: %d\n", 1); \ printf ("two: %d\n", 2); \ printf ("three: %d\n", 3); \ if (i == 1) prinft( "Only once: %d\n", 1 ); 5. Consider an ANSI compliant compiler on a machine using 2’s complement arithmetic for integers. The task is to figure out whether a variable used for integers is signed or not. One suggested solution is to write a macro like the following: #define IS_UNSIGNED( var ) \ ( (var) >= 0 && ~(var) >= 0 ) (a) Will this macro work for all possible integer values or are there any exceptions? (If you find any exceptions, you are encouraged to find a solution that will get rid of those exceptions.) (b) If the variable is a type instead of a variable, how would the macro above be rewritten? #define IS_UNSIGNED( var ) \ (c) Is it possible to write a function to do this job instead of a macro? If yes, show how the the macro shown above can be rewritten as a function. If it cannot be cast as a function, explain why not. [Either write below, the IsVarUnsigned( ... ) function or give your reasoning.] 6. Consider the following function: int SpecialFunction (int a) { if ( a <= 1 ) { return ( 1 ); } else { return ( a * SpecialFunction (a - 2) ); } } What is the value of x where "x = SpecialFunction (11)" 7. Write a function to check if a number is a power of 2 bool IsPowerOfTwo (unsigned int a) { } 8. Given structure A defined as shown below, what does the following statement return (x declared as an unsigned)? struct A { char a; char b; int c; short d; short *p; }; x = sizeof (struct A); Assume an ANSI compliant compiler with CHAR_BIT == 8 and sizeof int == 4. Explain how you arrived at your answer. 9. The operating system typically allocates memory in pages such that the base address of the pages is 0, 4K, 8K, .... Given two (virtual) addresses, write a program to find if they refer to locations on the same page. bool AreOnSamePage (int *a, int *b) { } 10. Write a function to return the nearest integer value of a given floating point number, with an ANSI compliant compiler. int RoundToNearestInteger (float f) { } rev 03142005 sns-oh-jb-ko