Exam in EDAA25 C Programming

Similar documents
Stacks. Linear data structures

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

1 Abstract Data Types Information Hiding

Illustration 1: Diagram of program function and data flow

Sources: On the Web: Slides will be available on:

Chapter 13 Storage classes

The programming language C. sws1 1

C++ INTERVIEW QUESTIONS

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Object Oriented Software Design II

Programming languages C

The C Programming Language course syllabus associate level

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Object Oriented Software Design II

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

C++ Programming Language

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Stack Allocation. Run-Time Data Structures. Static Structures

MISRA-C:2012 Standards Model Summary for C / C++

C Interview Questions

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

JavaScript: Control Statements I

7.1 Our Current Model

Common Data Structures

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

DATA STRUCTURE - STACK

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Lecture 12 Doubly Linked Lists (with Recursion)

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Variable Base Interface

X86-64 Architecture Guide

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Tutorial on C Language Programming

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Data Structure with C

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Introduction to Data Structures

Semantic Analysis: Types and Type Checking

Chapter 7D The Java Virtual Machine

Chapter 5 Names, Bindings, Type Checking, and Scopes

arrays C Programming Language - Arrays

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CS 241 Data Organization Coding Standards

5 Arrays and Pointers

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Tail call elimination. Michel Schinz

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Lecture Data Types and Types of a Language

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Data Structures Using C++ 2E. Chapter 5 Linked Lists

DATA STRUCTURES USING C

Static Code Analysis Procedures in the Development Cycle

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

/* File: blkcopy.c. size_t n

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

CS 2412 Data Structures. Chapter 2 Stacks and recursion

10CS35: Data Structures Using C

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Data Structures. Level 6 C Module Descriptor

CmpSci 187: Programming with Data Structures Spring 2015

Using Power to Improve C Programming Education

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

2) Write in detail the issues in the design of code generator.

Example 1/24. Example

Lecture Notes on Binary Search Trees

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Keil C51 Cross Compiler

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Computer Programming I

C++ Overloading, Constructors, Assignment operator

Software Engineering Techniques

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

Securing software by enforcing data-flow integrity

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Java Program Coding Standards Programming for Information Technology

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Java Basics: Data Types, Variables, and Loops

Binary storage of graphs and related data

Module 816. File Management in C. M. Campbell 1993 Deakin University

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Object Oriented Software Design II

Lecture 22: C Programming 4 Embedded Systems

Security types to the rescue

Computer Programming Tutorial

CS106A, Stanford Handout #38. Strings and Chars

Chapter 2 Introduction to Java programming

recursion, O(n), linked lists 6/14

Module 2 Stacks and Queues: Abstract Data Types

Transcription:

Exam in EDAA25 C Programming October 30, 2014, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt Grading instructions In general: assess if a function or program works as intended while ignoring syntax errors. That gives full score. Then subtract each syntax error from that score and give at least zero points. If the code doesn t work, judge youself on how serious error it contains, keeping in mind the obvious that the intent is to give the proper grade. If a student makes the same mistake n times, the student s score will be reduced n times. In general 1 per trivial syntax error which can be regarded as a typo. In general 2 per serious syntax error which indicates the student needs more practising. 30 out of 60p are needed to pass the exam. You are not required to follow any particular coding style but your program should of course be readable. If you call a function which may fail, such as malloc, you must check whether the call succeeded or not. 1. (10p) Implement the function #include <string.h> ; appends the string pointed to by src to the end of the string pointed to by dest, and the first character appended overwrites the null character of dest. The strings must not overlap, but if they do the behavior is undefined. The return value is dest. Answer: The simplest solution is: strcpy(dest + strlen(dest), src); 1

An alternative is: size_t n; size_t i; n = 0; while (dest[n]!= 0) n += 1; i = 0; while ((dest[n+i] = src[i])!= 0) i += 1; And another alternative: char* p = dest; while (*p!= 0) p += 1; while (*p++ = *src++) ; With a good compiler, the first solution is likely to be the most efficient if the strings are long, since both strlen and strcpy often are implemented in assembler that uses special tricks and instructions. With a good compiler, the two other alternative solutions should be equivalent after compiler optimization. 2. (3p) In which sense is the function in the previous question insecure (not counting the fact that the strings are not allowed to overlap as a security issue)? Answer: it is insecure in the sense that it just assumes dest points to a sufficiently large memory area, and this is sometimes exploited in hacker attacks. 3. (2p) What does the following function print, and why? char b[] = "Arrays and pointers in C are not the same thing."; char* c = b; if (sizeof b == sizeof c) printf("then\n"); else printf("else\n"); return 0; 2

Answer: It will print ELSE. See book. 4. (5p) Consider the previous question again but now with the following variant. void f(const char b[100], const char* c) if (sizeof b == sizeof c) printf("then\n"); else printf("else\n"); f("arrays", " and pointers."); return 0; What is the output this time and why? Answer: It will print THEN. See book an array parameter is converted to a pointer. 5. (10p) Implement a stack that can be used in an RPN-calculator such as the one in Assignment 1 from September. Your stack should have int as the type of the stored data, and it must be implemented with a heap allocation for each time you push, and deallocation each time something is popped. Your implementation should check that it was possible to allocate memory and that no attempt is made to pop from an empty stack. At any error, call exit(1). In addition to push and pop, you should implement a function new_stack to create an empty stack (which, of course, may simply return a null pointer if you wish that), and free_stack to deallocate any memory used by the stack. These functions should be used as in the code below, and the return value from main should be zero (which it will be if free_stack sets what the parameter points at to NULL). stack_t* stack; int a; stack = new_stack(); push(stack, 2014); a = pop(stack); push(stack, 10); push(stack, 30); free_stack(&stack); return stack == NULL? 0 : 1; Answer: See page 95 in the book. 6. (20p) Give brief descriptions of each of the following. ->?: :0 3

... volatile Function scope Internal linkage ANSI C aliasing rules Flexible array member Variable length array Answer: See book. 7. (5p) Use the declaration of a single linked list below, and implement the function reverse, which should reverse the list and set what the parameter points at to the new first list element. Your implementation is, however, not allowed to use neither goto or any loop syntax (i.e. for, while, and do-while are forbidden). Instead, obviously, use recursion. Furthermore you are allowed to visit each list node only once. You are allowed to use additional functions (which may have any number of parameters). Except for a constant number of local variables (such as a few pointers), you are not allowed to allocate memory either from the heap nor from the stack (which you might consider for a variable length array but that is forbidden). typedef struct list_t list_t; struct list_t list_t* next; void* data; ; void reverse(list_t** list); Answer: Only one store is needed in the following solution in the recursive function since a good compiler optimizes the recursive call into a goto and therefore no stack frame is needed, but this is not part of the course, and any working solution is OK! There is another store in the function reverse. static list_t* rec_reverse(list_t* new_next, list_t* current) list_t* old_next; if (current == NULL) return new_next; else old_next = current->next; current->next = new_next; return rec_reverse(current, old_next); void reverse(list_t** list) *list = rec_reverse(null, *list); 4

8. (5p) Implement the macro isnan(a) which returns a non-zero value if the floating point type parameter is a so called not-a-number, NaN. Hint: you do not need to know how a NaN value is represented it s possible (and easier in some sense) to solve the problem without using that information, but if you do know it, you are allowed to use that knowledge and check the representation, but do not break any rule of the C standard! Answer: #define isnan(a) (!((a)==(a))) Comparisons with a NaN are always false in ISO C. 5