Final CSE 131B Spring 2005

Similar documents
Final CSE 131B Spring 2004

Final CSE 131B Winter 2003

Midterm CSE 131B Spring 2005

Final CSE 131 Winter 2010

CSE 30 Spring 2006 Final Exam

Final CSE 131 Winter 2012

CSE 30 Fall 2006 Final Exam

CSE 30 Fall 2013 Final Exam

Midterm CSE 131B Spring 2006

Midterm CSE 131 Winter 2012

CSE 30 Winter 2014 Final Exam

CSE 30 Winter 2009 Final Exam

Midterm CSE 131 Winter 2014

CSE 30 Fall 2012 Final Exam

Final CSE 131 Fall 2014

CSE 30 Spring 2007 Final Exam

Midterm CSE 131 Fall 2014

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CSE 30 Fall 2007 Final Exam

CSE 5A Final Fall 2006

Midterm CSE 131 Winter 2013

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Midterm CSE 131 Winter 2015

CS C Primer. Tyler Szepesi. January 16, 2013

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

PRINCIPLES OF OPERATING SYSTEMS

DECLARAING AND INITIALIZING POINTERS

Final CSE 131 Fall 2015

Kurt Schmidt. October 30, 2018

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

ECE264 Spring 2014 Exam 2, March 11, 2014

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Common Misunderstandings from Exam 1 Material

System Software Assignment 1 Runtime Support for Procedures

Lecture 03 Bits, Bytes and Data Types

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS16 Exam #1 7/17/ Minutes 100 Points total

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

C++ for Java Programmers

CS270 Homework 5 (HW5)

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

Heap Arrays. Steven R. Bagley

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

We would like to find the value of the right-hand side of this equation. We know that

211: Computer Architecture Summer 2016

Dynamic memory allocation

Arrays and Pointers in C. Alan L. Cox

Programming in C - Part 2

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS 61c: Great Ideas in Computer Architecture

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Lectures 13 & 14. memory management

Data Representation and Storage. Some definitions (in C)

ESC101N: Fundamentals of Computing End-sem st semester

High Performance Programming Programming in C part 1

CSE 333 Midterm Exam Sample Solution 5/10/13

Course organization. Course introduction ( Week 1)

CSE 374 Programming Concepts & Tools

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Procedure and Object- Oriented Abstraction

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

ECEN 449 Microprocessor System Design. Review of C Programming

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CSE351 Spring 2018, Final Exam June 6, 2018

CS2141 Software Development using C/C++ C++ Basics


United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

CSE 303 Midterm Exam

CPSC213/2014W1 Midterm EXTRA Practice

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 3: C Programm

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Chapter 1 Getting Started

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

CSE 124 Discussion (10/3) C/C++ Basics

Memory Corruption 101 From Primitives to Exploit

CS240: Programming in C

CSE 333 Midterm Exam 5/10/13

ECE 15B COMPUTER ORGANIZATION

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

Understanding Pointers

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Processes. Johan Montelius KTH

CSE 11 Midterm Fall 2008

The University of Nottingham

POINTER AND ARRAY SUNU WIBIRAMA

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

FUNCTIONS POINTERS. Pointers. Functions

Lecture 2, September 4

Introduction to C: Pointers

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Transcription:

Login name Signature Name Student ID Final CSE 131B Spring 2005 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (27 points) (24 points) (32 points) (24 points) (32 points) (26 points) (31 points) (20 points) Subtotal Page 9 Extra Credit (216 points) (11 points) Total 0

1 Consider the following C++ function: int fubar( int a ) static int y = a; // Other possible code not relevant to this question return y; Generate unoptimized SPARC assembly code for this function taking into consideration the semantics involved with the C++ language allowing static variables to be initialized at run time as discussed in class (24 points) Regarding the C++ function above: How does the compiler differentiate between the static variable y in function fubar() and a global variable named y or another static variable named y defined elsewhere in separate source files that may be part of the overall resulting program and keep their scope/visibility semantically correct? Be specific Be sure to incorporate this into your code generation above (3 points) 1

2 Given the following Oberon program and expected output, determine whether each parameter is pass-byreference or pass-by-value Fill in the blanks with "VAR" if pass-by-reference, leave it blank if pass-by-value VAR x : INTEGER; VAR y : INTEGER; PROCEDURE foo1 ( a : INTEGER); a = y + 30; END foo1; PROCEDURE foo2 ( a : INTEGER, b : INTEGER); a = 2 * y; b = 3 * x; END foo2; PROCEDURE foo3 ( a : INTEGER, b : INTEGER); a = x + y; b = x - y; END foo3; x = 10; y = 10; foo1(y); foo2(x, y); foo3(x, y); OUTPUT x, " ", y; (* should output 80 60 *) END Now in order to get full credit for the above and discourage Jeff Spicoli-like random guesses, what is the output if each VAR parameter was changed to non-var and each non-var parameter was changed to VAR? You may use the space below as a scratch pad area to help figure out the correct parameter passing modes 2

3 In your Project 2, how did you (and your partner if you had a partner) implement global variables? There were several possible implementation options discussed Be specific how your project implemented them! (10 points) Given the following Oberon program, fill in the values of the global and local variables and parameter in the run time environment when the program reaches the label HERE memory locations low memory TYPE t = RECORD a: INTEGER; b: BOOLEAN; END; VAR x : INTEGER; PROCEDURE f(var i: INTEGER); VAR j: INTEGER; VAR r2: POINTER TO INTEGER; VAR a2: ARRAY 3 OF t; NEW(r2); r2^ := 95; j := 17; a2[0]a := 5; a2[1]a := 13; a2[2]b := FALSE; a2[0]b := TRUE; a2[2]a := -28; a2[1]b := TRUE; (* HERE *) END; f(x); END x: 4000 Heap 8000 20000 %fp 20100 high memory 3

4 Identify where each of the following program parts live in the Java runtime environment as discussed in class (12 points) public class Foo private static Foo a; a private int b; b public Foo() Foo() a = this; this ++b; main() public static void main( String[] args ) args Foo c = new Foo(); c int d; d c = new Foo(); where c is pointing cmethod( d ); method() private void method( int e ) e int f; f f = e; Using the Right-Left rule (which follows the operator precedence rules) write the definition of a variable named foo that is a pointer to a pointer to a function that takes a pointer to a struct Fubar as the single parameter and returns a pointer to a 4x7 2-D array where each element is a pointer to an array of 8 doubles (6 points) What is an advantage of storing multi-dimensional arrays as contiguous memory locations as opposed to arrays of arrays in C/C++? (3 points) What is an advantage of storing multi-dimensional arrays as arrays of arrays as opposed to contiguous memory locations in C/C++? (3 points) 4

5 Given the following C program, order the printf() line numbers so that the values that are printed when run on a Sun SPARC Unix system are displayed from smallest value to largest value (note the address of the different program parts are printed, not their value) Also specify the corresponding C Run Time area where each program part will be allocated (22 points) void foo( int *, int ); /* Function Prototype */ int a = 420; Line that prints the int main( int argc, char *argv[] ) smallest value Run Time (low memory) Area int b = 420; a foo( &argc, b ); b /* 1 */ (void) printf( "a --> %p\n", &a ); c /* 2 */ (void) printf( "b --> %p\n", &b ); /* 3 */ (void) printf( "argc --> %p\n", &argc ); d /* 4 */ (void) printf( "argv --> %p\n", &argv ); /* 5 */ (void) printf( "malloc --> %p\n", malloc(75) ); e /* 6 */ (void) printf( "foo --> %p\n", foo ); f void foo( int *c, int d ) g int e = 404; foo static int f; int g; argv /* 7 */ (void) printf( "c --> %p\n", &c ); malloc /* 8 */ (void) printf( "d --> %p\n", &d ); /* 9 */ (void) printf( "e --> %p\n", &e ); argc /* 10 */ (void) printf( "f --> %p\n", &f ); /* 11 */ (void) printf( "g --> %p\n", &g ); Line that prints the largest value (high memory) List the two main tasks/functions of the linkage editor as discussed in class? (6 points) 1) 2) How does the this reference (in Java) or the this pointer (in C++) become available/accessible in a method? (4 points) 5

6 Given the following Oberon program, emit the unoptimized SPARC assembly language code that should be generated for the two procedures foo() and foo1() Assume no optimizations treat each instruction separately without any knowledge of any previously computed/loaded/stored values that may still be in a register from a previous instruction Draw a line between each group of assembly language instructions that represent the emitted code generated for each instruction and label them with the instruction number All local variables must be allocated and accessed on the Stack (do not map them directly into a local register) (26 points) PROCEDURE foo( a : INTEGER; VAR b : INTEGER ) : INTEGER; VAR i : ARRAY 2 OF INTEGER; (**** Local Stack variables Do not initialize to zero ****) a := i[1] * 5; (* 1 *) b := a + 10; (* 2 *) i[0] := b; (* 3 *) RETURN b; (* 4 *) END foo1; PROCEDURE foo1( VAR a : INTEGER; VAR b : INTEGER ); VAR i, j : INTEGER; (**** Local Stack variables Do not initialize to zero ****) j := foo( i, b ); (* 5 *) a := foo( a, j ); (* 6 *) (* Don't worry about any local variables not initialized *) END foo; (* *) END global foo, foo1, main foo: section "text" foo1: 6

7 Show the memory layout of the following C struct/record definition taking into consideration the SPARC data type memory alignment restrictions discussed in class Fill bytes in memory with the appropriate struct/record member/field name For example, if member/field name p takes 4 bytes, you will have 4 p's in the appropriate memory locations If the member/field is an array, use the name followed by the index number For example, some number of p0's, p1's, p2's, etc Place an X in any bytes of padding Structs are padded so the total size is evenly divisible by the most strict alignment requirement of its members (11 points) struct foo short a[3]; int b; double c; char d; short e; struct foo fubar; fubar: low memory Generate SPARC assembly code for the C function below (20 points) C void foo( int i ) while ( i < 20 ) printf( "%d\n", i ); ++i; SPARC 7

8 Consider a typical if-then-else statement that will be executed many times such as being embedded in a loop Is it more efficient to structure the condition so the statements you know will most likely to be executed are in the if/true block or the else/false block? Why? (8 points) if ( condition ) if/true block else else/false block Perform loop improvements on the following C code fragment so the loop should execute more efficiently even without any compiler optimization (10 points) Code improved version here int i; int i; char str[] = "e_superstore = color"; char str[] = "e_superstore = color"; /* other code */ /* Modifications from this point down */ for ( i = 1; (i 1) < strlen( str ); i = i * 2 ) str[i-1] = toupper( str[i-1] ); Tell me something you learned in this class that is extremely valuable and that you think you will be able to use for the rest of your programming/computer science career (2 points if serious; you can add non-serious comments also) 8

9 Extra Credit (11 points) What gets printed by the following C program? #include <stdioh> int main() char a[] = "End this, Please!"; char *ptr = &a[6]; printf( "%c\n", *ptr++ ); printf( "%c\n", *(ptr = ptr + 3) ); printf( "%c\n", ptr[2] ); printf( "%c\n", a[strlen(a)-2] ); printf( "%c\n", (*(a+7))-3 ); ptr = &a[8]; printf( "%c\n", *--ptr ); return 0; Given the following ANSI/ISO C variable definitions, identify which expressions will produce a static semantic compiler error Hint: Think modifiable l-value A) No compiler error B) Compiler error int i = 5; float f = 15; int *iptr = &i; float *fptr = &f; fptr = &(i + f); ++( (float *) iptr ); i = *(int *)fptr; iptr = *(int **)&fptr; Crossword Puzzle (next page) 9

Scratch Paper 10

Scratch Paper 11