CMPSC 461: Programming Language Concepts Assignment 6 Solution
CMPSC 461: Programming Language Concepts Assignment 6 Solution Problem 1 [6pt] Assume that integers are stored using one byte in memory. What are the binaries for 8 and -10 in two’s complement format? What is the result of (8-10) in two’s complement format? Solution: binary for 8: binary for -10: result of (8-10): Problem 2 [10pt] Consider the following C declaration: union U1 {int a; float b;}; union U2 {char a; double b;}; struct S { union U1 u1; union U2 u2; } s; Assume that the machine has 1-byte characters, 4-byte integers, 8-byte floating numbers, and 16-byte double-precision floating numbers. Assume the compiler does not reorder the fields, and it leaves no holes in the memory layout. a) (4pt) How many bytes does s occupy? Solution: s occupies max(4,8)+max(1,16)=24 bytes. b) (6pt) If the memory address of s starts from 1000, what are the start addresses of s.u1.b and s.u2.b? Solution: The start address of s.u1.b is 1000; the start address of s.u2.b is 1008. Problem 3 [8pt] In a programming language, there are multiple times that the size of an array can be determined, such as a) Static/Fixed - the dimensions of the array are known at compile time, and they are fixed b) Dynamic/Fixed - the dimensions are not known until storage is allocated for an array, but then are fixed For each of these, describe an appropriate representation and allocation strategy (stack/heap). Describe how bounds checking would be supported. You should consider both one and multi- dimensional arrays. Solution: (This is an open question. We only show one possible solution) a) Since the array size is available at compile time, the compiler can allocate continuous space on the stack. Multi-dimensional arrays can either follow row-major or column-major layout. Since the size of the array
Written for
Document information
- Uploaded on
- September 25, 2023
- Number of pages
- 3
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
cmpsc 461 programming language concepts assignmen
Also available in package deal