CSE 333 Autumn 2013 Midterm

Similar documents
CSE 333 Autumn 2014 Midterm Key

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

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Midterm Exam 7/27/15 Sample Solution

Memory Corruption 101 From Primitives to Exploit

EL6483: Brief Overview of C Programming Language

CSE 303, Autumn 2006, Final Examination 12 December 2006

Final CSE 131B Spring 2004

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

CSE 333 Midterm Exam July 24, Name UW ID#

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Bruce Merry. IOI Training Dec 2013

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

CSE 333 Midterm Exam 7/22/12

Midterm #2 With Solutions solutions in blue marking notes in red

The University of Nottingham

Lecture 07 Debugging Programs with GDB

CSE 333 Midterm Exam 5/10/13

Question 1. [15 marks]

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Arrays and Pointers in C. Alan L. Cox

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

EL2310 Scientific Programming

CSE 333 Lecture 2 Memory

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

In Java we have the keyword null, which is the value of an uninitialized reference type

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS 0449 Sample Midterm

CSE 303 Final Exam. March 16, 2009

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam 2/14/14

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS 11 C track: lecture 5

Supporting Class / C++ Lecture Notes

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSE 333 Midterm Exam 7/29/13

High-performance computing and programming I. Introduction to C programming in *NIX

Topic 6: A Quick Intro To C

CSE 303: Concepts and Tools for Software Development

QUIZ. What are 3 differences between C and C++ const variables?

Pointers, Dynamic Data, and Reference Types

Lectures 5-6: Introduction to C

CS24 Week 2 Lecture 1

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

High Performance Programming Programming in C part 1

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Chapter IV Introduction to C for Java programmers

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Dynamic memory. EECS 211 Winter 2019

CSE 333 Midterm Exam 7/25/16. Name UW ID#

Chapter 2. Procedural Programming

CSE 333 Lecture 7 - final C details

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

C++ Undefined Behavior What is it, and why should I care?

C for C++ Programmers

211: Computer Architecture Summer 2016

Exercise 1.1 Hello world

A brief introduction to C programming for Java programmers

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

EE 355 Lab 4 - Party Like A Char Star

ATOMS. 3.1 Interface. The Atom interface is simple: atom.h #ifndef ATOM_INCLUDED #define ATOM_INCLUDED

Arrays and Memory Management

Operator overloading

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Creating a String Data Type in C

Signature: ECE 551 Midterm Exam

Have examined process Creating program Have developed program Written in C Source code

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 103 Lab - Party Like A Char Star

CE221 Programming in C++ Part 1 Introduction

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Programs in memory. The layout of memory is roughly:

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Short Notes of CS201

CSE 374 Final Exam Sample Solution 3/17/11

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

EL2310 Scientific Programming

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Transcription:

CSE 333 Autumn 2013 Midterm Please do not read beyond this cover page until told to start. A question involving what could be either C or C++ is about C, unless it explicitly states that it is about C++. Code is considered to have compiled if no errors are raised; the code may generate warnings, but still compile. We don't think the answer to any individual question needs to be very long. Make sure you have at least read all the questions before spending too much time on any one. Name:

1. [3 points] This code sometimes runs and produces a correct result and sometimes experiences a fatal runtime error. #include <stdlib.h> int sum(int n) { if ( n <= 1 ) return 1; return n + sum(n-1); if ( argc!= 2 ) exit(1); int n = atoi(argv[1]); printf( Sum(0..n) = %d\n, sum(n)); return EXIT_SUCCESS; Briefly explain exactly why some executions experience fatal errors. If the command line argument is sufficiently large the recursion will be deep enough that it overflows the available stack space. Note: Many people answered that atoi will crash if its argument isn't a string representation of an integer. It doesn't (and I don't think you should expect it would crashing is never a good option). Note: Many people answered that the result is wrong if the argument is zero or less. That may be, but it's not a runtime error, it's just a bug (if that). 2. [3 points] Suppose you run the following code through the preprocessor when symbol _MY_H_ is defined: #ifndef _MY_H_ int gint = -2; #include B.h #endif // _MY_H_ We know that the preprocessor will drop all source lines between the #ifndef and its #endif, and so will not emit the line defining gint. Although we haven't talked about it directly, we can also be certain that the preprocessor will not execute the #include directive, that is, will ignore preprocessor directives as well. Briefly explain how you can deduce, with certainty, that the preprocessor will not include B.h in the code above based on things you're already sure of. I think a total of two people understood this question the way I intended it, so it wasn't graded as part of the midterm. It was possible to get some bonus points on it, but the entire exam is graded as though this question weren't here. What I intended was that this be a question about ifdef guards. We know they work. We know they prevent double delcarations, but that's because they omit source lines. How do we know the

#include is not executed by the preprocessor? Because if it were some ifdef guarded.h files would put the preprocessor into a loop, and they don't that's another of the purposes of the ifdef guards. If they don't, the preprocessor must be ignoring commands to itself whenever its also eliding source file lines. 3. [8 points] Given the following informal description of a function, design and then give C code for an implementation. Note that our description does not intend to specify the C interface you should decide on the actual types used in implementing the function. The function takes two strings as inputs, and returns two values: a string that is the longest prefix of both input strings, and the length of that prefix. For example, if the two input strings are lark and large, the function returns lar and 3. If the two strings are abc and def it returns and 0. The function has some way of indicating that an error has occurred, and so that it is unable to provide a result. Give the function's implementation here. char * lcp(const char* s1, const char* s2, int *length) { char *result = (char*)malloc(strlen(s1) + 1); if ( result == NULL ) return NULL; for ( length=0; s1[length] && s1[*length] == s2[*length]; (*length)++ ) { p[*length] = s1[*length]; result[*length] = '\0'; return result; Many people typedef'ed a struct containing a char* and an int, and returned one. That's a reasonable solution as well,. 4. [3 points] Below is the complete contents of C source file q4.c. Circle all (and only) the lines of code that cause compile time errors when the file is compiled. char a[] = initial string ; char *b; a = "test"; b = "test"; *a = 'a'; *b = 'b'; a[4] = 'a'; b[4] = 'b'; An array name cannot be assigned to. All the other statements compile without warning, although many of them are runtime errors.

A string literal is a char *. A character array can be initialized from a string literal. String literals follow C string conventions, so are null terminated. Array notation is shorthand for pointer arithmetic and dereferencing (e.g., b[4] is the same as *(b+4)), so can be used even on char* variables. 5. [3 points] This code builds without warning but experiences a run time error. #include <stdlib.h> #include <string.h> void delete(void **p) { free(*p); *p = NULL; void *p = malloc(1024); strcpy(p, "literal string"); printf("memory contents = '%s'\n", (const char*)p); delete(p); delete(&p); return EXIT_SUCCESS; Fix the bug, either by describing the fix here or annotating the code above. DO NOT change the obvious intent of the code, just make what it is trying to do work. The delete() function intends to both free the memory pointed at by some pointer and set the pointer to NULL. That requires that the argument be a pointer to the pointer. The call incorrectly passes the pointer to the memory to be freed.

6. [3 points] This code seg faults. Briefly explain why. #include <string.h> #define LEN 4 char *truncate(char *str, unsigned int len) { if ( strlen(str) <= len ) return str; str[len] = '\0'; return str; char *originalstr = "long string"; printf("%s truncated to length %d is ", originalstr, LEN); printf("%s\n", truncate(originalstr,len)); The truncate() method tries to modify the memory pointed at by its str parameter. But in this code that parameter is the address of a string literal. String literals are stored in read-only memory, so the attempt to modify it (in the line str[len] = '\0';) results in a seg fault. 7. [5 points] I'm considering building an application in C. As part of my design I'd like to implement a fixed capacity stack to hold elements of different types. I'd like my stack implementation to be generic it can hold data of many different types, including some I haven't thought of yet. Finally, for debugging, I'd like to be able to print the contents of the stack, as part of the stack's functionality. Briefly describe how I can achieve this. Be convincing. What information must be supplied by the client code? On what call(s) is that information supplied? How does my stack implementation keep track of the information it needs? The original intention of this question was that you explain how a single stack could hold elements of many distinct types, all at the same time, and still print itself. It was graded without requiring the simultaneous part of different types, though. Fully correct answers need to mention two specific things: void* as the mechanism that allows the client to provide data of arbitrary type to the stack implementation, and the use of a callback, supplied by the client code, to print the data.

8. [4+2 = 6 points] Here is some C++ code. (It compiles and runs without error.) #include <iostream> using namespace std; class Foo { public: Foo(int x) { data = x; Foo(const Foo &other) { data = other.data; int value() const { return data; Foo merge(const Foo second) const; Foo &reset(); private: int data; ; Foo Foo::merge(const Foo second) const { Foo result(*this); result.data += second.data; return result; Foo &Foo::reset() { data = 0; return *this; Foo first(2); Foo second(first); Foo third = first.merge(second); Foo fourth = third.reset(); cout << fourth.value() << endl; (A) Circle all the bits of code where it's possible a copy constructor might be invoked. (Be specific. For example, don't just circle all of main!) There are six instances, highlighted above. (Note that, by default, the compiler will elide two of them).. (B) Explain why the following line of code, if inserted into main(), will not compile: Foo fooarray[10]; For that declaration to work, there must be a default (no argument) constructor available for Foo. But, there is no explicitly provided default constructor and C++ won't synthesize one because there are explicitly provided constructors.

9. [3 points] $ cat q9.c #include <stdlib.h> #include <math.h> double radians = atof(argv[1]); printf("cosine(%lf) = %lf\n", radians, cos(radians)); $ gcc -Wall -std=gnu99 q9.c /tmp/cckwxxhb.o: In function `main': q9.c:(.text+0x39): undefined reference to `cos' collect2: error: ld returned 1 exit status What's wrong, and what do I need to do to fix it? This is a linker error, meaning that the linker isn't finding an object file that defines the symbol cos. The solution is to provide such an object file. In this particular case, we need to tell the compiler to use the math library: gcc -Wall -std=gnu99 q9.c -lm Note that this has nothing to do with.h files, in the sense that no.h file can fix the problem..h files serve mainly to declare types, so that the compiler can type-check the code. This isn't a type error; there's a piece of code missing. 10.[4 points] This code intends to print 0, 1,, 4: #include <stdlib.h> int *createsequence(unsigned int N) { int result[n]; int *result = (int*)malloc(n * sizeof(int)); if ( result == NULL ) return NULL; for (int i=0; i<n; i++) { result[i] = i; return result; void printintarray(int array[], int N) { for (int i=0; i<n; i++) { printf("%d ", array[i]); int *seq = createsequence(5); if ( seq == NULL ) return 1; printintarray(seq, 5); free(seq);

It builds, but when I run it it instead prints 0 0 0 0 4196100. Fix the code (by writing on the source shown above). (You must solve all problems with it, so that it meets the correctness criteria we've required of exercise solutions.) The problem is that createsequence is returning a pointer to a stack allocated variable. The memory it occupied is overwritten when printintarray is called. The solution is inserted into the code above.