ENCM 339 Fall 2017 Tutorial for Week 8

Similar documents
Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

ENCM 335 Fall 2018 Tutorial for Week 13

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

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

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

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

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

ENCM 501 Winter 2015 Tutorial for Week 5

Memory Organization. The machine code and data associated with it are in the code segment

CS61, Fall 2012 Section 2 Notes

SOFTWARE Ph.D. Qualifying Exam Fall 2017

T02 Tutorial Slides for Week 2

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

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

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

Slide Set 15 (Complete)

DAY 3. CS3600, Northeastern University. Alan Mislove

CSE 333 Midterm Exam 2/14/14

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

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

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

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

CS32 Discussion Week 3

University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Kurt Schmidt. October 30, 2018

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

ECE264 Fall 2013 Exam 3, November 20, 2013

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Dynamic memory. EECS 211 Winter 2019

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

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

ENCM 369 Winter 2017 Lab 3 for the Week of January 30

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

#ifndef DOUBLE_LIST /* this string SHOULD NOT previously exist */ #define DOUBLE_LIST

CS 237 Meeting 19 10/24/12

CS 222: Pointers and Manual Memory Management

CS24 Week 2 Lecture 1

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

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

Dynamic Memory Management

ECE264 Fall 2013 Exam 2, October 24, 2013

Signature: ECE 551 Midterm Exam

Dynamic Memory Management

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

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

Dynamic Data Structures. CSCI 112: Programming in C

PRINCIPLES OF OPERATING SYSTEMS

Advanced Pointer Topics

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C Programming, Autumn 2013, Exercises for the Second Week

Slide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Programs in memory. The layout of memory is roughly:

Programming in C++: Assignment Week 1

CSE 333 Midterm Exam 5/9/14 Sample Solution

ESC101N: Fundamentals of Computing End-sem st semester

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

NYU SCPS X Section 1 Unix System Calls. Fall 2004 Handout 8. Source code on the Web at mm64/x /src/doubly2.

ECE264 Spring 2013 Final Exam, April 30, 2013

Question 1. [15 marks]

ENCM 501 Winter 2019 Assignment 9

CSE 303 Midterm Exam

Winter 2017 MIDTERM TEST #1 Wednesday, February 8 7:00pm to 8:30pm. Please do not write your U of C ID number on this cover page.

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Recitation 2/18/2012

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

return return else return

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam Sample Solution 7/29/13

Computer Architecture I Mid-Term I

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Lecture 14. Dynamic Memory Allocation

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

Common Misunderstandings from Exam 1 Material

CS 1713 Introduction to Programming II

Slide Set 1 (corrected)

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Transcription:

ENCM 339 Fall 2017 Tutorial for Week 8 for section T01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 2 November, 2017

ENCM 339 T01 Tutorial 2 Nov 2017 slide 2/12 Today s tutorial malloc and free. A C struct type for managing dynamically allocated strings. Note: Online copies of ENCM 339 Fall 2017 section T01 tutorials can be found by following links starting at people.ucalgary.ca/~norman

ENCM 339 T01 Tutorial 2 Nov 2017 slide 3/12 Exercise 1 For the program on the next slide... Draw a diagram for point (1). (To save time, let s leave out details of static storage.) Draw a diagram for point (2). (Again, let s leave out details of static storage.) Identify two unsafe and defective uses of the variable p. Describe the output of the program as precisely as possible. (Assume that the calls to malloc succeed.)

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *p, *q; p = malloc(4); // (1) strcpy(p, "foo"); q = malloc(8); strcpy(q, p); free(p); q[3] = t ; q[4] = \0 ; // (2) printf("<%s><%s>\n", q, p); p[0] = x ; free(q); return 0;

ENCM 339 T01 Tutorial 2 Nov 2017 slide 5/12 A C type for dynamically allocated strings Using statically or automatically allocated arrays of chars to hold strings has many disadvantages. One disadvantage is that it s difficult to build up strings by concatenating strings together or by appending characters one at a time eventually, the array may be completely full, with no more room for more characters. The next slide shows a header file called dastring.h, with a type that could be used for dynamically allocated string objects.

ENCM 339 T01 Tutorial 2 Nov 2017 slide 6/12 struct d_a_s { int len; // length of C string int cap; // size of array char *arr; // pointer to element 0 ; typedef struct d_a_s dastring; We ll use these rules for dastring objects... Empty strings will have len == 0 and cap == 0, and arr will point to a statically allocated string of length 0. Non-empty strings will have arr point to element 0 of a dynamically-allocated array of size cap. The array will contain a null-terminated string of length len. (So, clearly, len < cap must be true.)

ENCM 339 T01 Tutorial 2 Nov 2017 slide 7/12 Exercise 2: Bad initialization Which of the initializations below comply with the rules for dastring objects and which ones don t? #include "dastring.h" int main(void) { dastring x = {0, 0, ""; dastring y = {4, 5, "quux"; dastring z; //... more code... return 0;

ENCM 339 T01 Tutorial 2 Nov 2017 slide 8/12 Exercise 3: Better initialization Function interface: void das_init(dastring *das, const char *s); // REQUIRES: das points to an unitialized object. // s points to the start of a C string. // PROMISES: *das has been initialized by copying // the C string. Example use: int main(void) { dastring x; das_init(&x, ""); dastring y; das_init(&y, "quux"); //... code that uses x and y... return 0; Let s write a definition for das_init.

ENCM 339 T01 Tutorial 2 Nov 2017 slide 9/12 Exercise 4: Appending characters one at a time Let s consider a function called push_back, which appends a single character to the string managed by a dastring object... int main(void) { dastring x; das_init(&x, ""); push_back(&x, a ); push_back(&x, b ); push_back(&x, c ); push_back(&x, d ); printf("%s\n", x.arr); return 0; The output should be... abcd

ENCM 339 T01 Tutorial 2 Nov 2017 slide 10/12 It s not efficient to copy all of a string from one array to another every time a character is appended. When a bigger array is needed, it makes sense to allocate more than enough space, so that some future push_back operations can succeed without having to allocate new memory and copy a lot of characters. This approach is taken in the code on the next slide. For the main function shown on the previous slide, let s make diagrams for each time point one in push_back is reached.

ENCM 339 T01 Tutorial 2 Nov 2017 slide 11/12 void push_back(dastring *das, int c) { if (das->len == 0) { das->len = 0; das->cap = 4; das->arr = malloc(4); else if (das->len + 1 == das->cap) { das->cap = 2 * das->cap; char *old = das->arr; das->arr = malloc(das->cap); strcpy(das->arr, old); free(old); das->arr[das->len] = c; das->len += 1; // point one das->arr[das->len] = \0 ;

ENCM 339 T01 Tutorial 2 Nov 2017 slide 12/12 Remark C lacks features needed to make a type like dastring convenient for programmers. Using dastring correctly is awkward, and using it incorrrectly in all sorts of ways is unfortunately quite easy. Class types in C++ have the kinds of features needed for programmers to create new types that are as convenient to use as built-in types. (These exercises were orginally designed mostly to give students practice with malloc and free, but also partly to provide an introduction to C++ class types. Section 01 of ENCM 339 in Fall 2017 won t cover C++ at all.)