CSE 333. Lecture 1 - Intro, C refresher. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Similar documents
CSE 333 Lecture 2 - gentle re-introduction to C

CMPSC 311- Introduction to Systems Programming Module: Introduction to C

CSE 333 Lecture 1 - Systems programming

Intro, C refresher. CSE 333 Summer Instructor: Hal Perkins. Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

CSE 333 Lecture 1 - Systems programming

CSE 333 Lecture 2 - arrays, memory, pointers

CSE 333 Lecture 2 Memory

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CS61, Fall 2012 Section 2 Notes

CMPSC 311- Introduction to Systems Programming Module: Systems Programming

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

CSE 303 Lecture 8. Intro to C programming

CSE 374 Programming Concepts & Tools

Announcements. 1. Forms to return today after class:

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

CMPSC 311- Introduction to Systems Programming Module: Systems Programming

CSE 374 Programming Concepts & Tools

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

Welcome to... CS113: Introduction to C

HW1 due Monday by 9:30am Assignment online, submission details to come

Motivation was to facilitate development of systems software, especially OS development.

PRINCIPLES OF OPERATING SYSTEMS

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CMPSC 311- Introduction to Systems Programming Module: Systems Programming

Motivation was to facilitate development of systems software, especially OS development.

CSE 333 Lecture 6 - data structures

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

CSE 303: Concepts and Tools for Software Development

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

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

Exercise Session 2 Systems Programming and Computer Architecture

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

Arrays and Memory Management

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

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

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Kurt Schmidt. October 30, 2018

Final CSE 131B Spring 2004

CS61C : Machine Structures

EL2310 Scientific Programming

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Reviewing gcc, make, gdb, and Linux Editors 1

Deep C (and C++) by Olve Maudal

EL2310 Scientific Programming

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

CS 3813/718 Fall Python Programming. Professor Liang Huang.

just a ((somewhat) safer) dialect.

Class Information ANNOUCEMENTS

Common Misunderstandings from Exam 1 Material

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

CSE 333 Lecture 5 - data structures & modules

Pointers, Pointers, Pointers

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CS 6371: Advanced Programming Languages

Lecture 03 Bits, Bytes and Data Types

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

Deep C by Olve Maudal

CSE 333 Lecture 4 - malloc, free, struct, typedef

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS61C : Machine Structures

CS61C : Machine Structures

Topics Introduction to Microprocessors

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CSE 333 Midterm Exam 5/10/13

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

High Performance Computing MPI and C-Language Seminars 2009

Processes. Johan Montelius KTH

BLM2031 Structured Programming. Zeyneb KURT

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CSE 333 Midterm Exam 7/29/13

Lecture 2, September 4

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

A process. the stack

CSE 333 Midterm Exam July 24, Name UW ID#

CS 113: Introduction to

Fall, $ cat welcome.c #include <stdio.h>

Important From Last Time

CSE 333 Lecture 9 - intro to C++

CS 241 Data Organization using C

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

CS61C : Machine Structures

377 Student Guide to C++

Pointers, Pointers, Pointers

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

CS 241 Data Organization. August 21, 2018

C++ Tutorial AM 225. Dan Fortunato

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Important From Last Time

Transcription:

CSE 333 Lecture 1 - Intro, C refresher Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Welcome! Today s goals: - introductions - course syllabus - quick C refresher

Introductions Us (cse333-staff@cs - please use this address, not individual email) - Hal Perkins (Instructor) - Meghan Cowan, Renshu Gu, Steven Lyubomirsky, Josh Rios, Nathan Wong, and Jack Xu (TAs) Most important: You!! - Anyone still trying to register or add the class? Watch for openings; we ll collect names of anyone still trying to register on Friday

Welcome! Today s goals: - introductions - course syllabus (highlights only - read the whole thing on the web [yes, really do read it]) - quick C refresher

Course map: 100,000 foot view C application C++ application Java application OS / app interface (system calls) HW/SW interface (x86 + devices) C standard library (glibc) C++ STL / boost / standard library operating system hardware JRE CPU memory storage network GPU clock audio radio peripherals

Systems programming The programming skills, engineering discipline, and knowledge you need to build a system - programming: C / C++ - discipline: testing, debugging, performance analysis - knowledge: long list of interesting topics concurrency, OS interfaces and semantics, techniques for consistent data management, distributed systems algorithms,... most important: a deep understanding of the layer below quiz: is data safely on disk after a write( ) system call returns?

Discipline?!? Cultivate good habits, encourage clean code - coding style conventions - unit testing, code coverage testing, regression testing - documentation (code comments, design docs) - code reviews Will take you a lifetime to learn - but oh-so-important, especially for systems code avoid write-once, read-never code

What you will be doing Attending lectures and sections - lecture: ~28 of them, MWF here - sections: ~10 of them, Thur. 8:30 (sorry), 9:30, 10:30 - go to your actual sec. - Take notes!!!! Don t expect everything to be on the web Doing programming projects - 4 of them, successively building on each other, plus a warmup - includes C, C++, file system, network Doing programming exercises - one per lecture, due in the morning before the next lecture - coarse-grained grading (0,1,2,3) Midterm and a final exam - (Who is also taking 311 or 312? Trying to minimize scheduling conflicts)

Lecture laptop policy Just say no!! No open laptops during class (no kidding!) - (unless we re doing something where everyone should participate) Why? You will learn better if you are mentally present during the class (not just physically) Got the urge to search? Ask a question! Exception: if you actually, really, really, use a laptop or tablet to take notes, ok, but sit in the back row so you don t distract others - But you ll learn more if you use paper instead! (really!!) And no phone texting, web surfing, etc., either You may close your electronic devices now.

Deadlines & Conduct Need to get things done on time (very hard to catch up) - Programming assignments: 4 late days total, 2 max per project Intended for unusual circumstances, not routine procrastination - Exercises: no late days (max benefit that way) Academic Integrity (details on the web; read them) - I trust you implicitly; I will follow up if that trust is violated - The rules boil down to: don t attempt to gain credit for something you didn t do; don t help others to do so - That does not mean suffer in silence - you have colleagues, instructor, TAs - work with them; learn from each other!

Course web/calendar Linked off of the course web page - master schedule for the class (might change slightly) - links to: lecture slides (posted night before class - preview & use for notes) code discussed in lectures assignments, exercises (including due dates) optional self-exercise solutions various C/C++/Linux/git/CSE resources Explore!!!

Labs, office hours, &c Office hours: plan is to have something Mon.-Fri. - Initial schedule through next Mon. posted. Preferences? Discussion board: stay in touch outside of class - See main web page for link, post followup to welcome msg Mailing list for announcements - You are automatically subscribed when you are registered

Welcome! Today s goals: - introductions - course syllabus - quick C refresher

C Created in 1972 by Dennis Ritchie - designed for creating system software - portable across machine architectures - most recently updated in 1999 (C99) and 2011 (C11) Characteristics - low-level, smaller standard library than Java - procedural (not object-oriented) - typed but unsafe; incorrect programs can fail spectacularly

C workflow Editor (emacs, vi) or IDE (eclipse) edit source files (.c,.h) foo.h foo.c bar.c compile object files (.o) foo.o bar.o executable link load link bar execute, debug, profile,... process link bar libz.a libc.s statically linked libraries shared libraries

From C to machine code C source file (dosum.c) int dosum(int i, int j) { return i+j; } C compiler (gcc -S) assembly source file (dosum.s) dosum: pushl movl movl addl popl ret %ebp %esp, %ebp 12(%ebp), %eax 8(%ebp), %eax %ebp machine code (dosum.o) 80483b0: 55 89 e5 8b 45 0c 03 45 08 5d c3 assembler (as)

Skipping assembly language Most C compilers generate.o files (machine code) directly - i.e., without actually saving the readable.s assembly file dosum.c gcc -S dosum.s as dosum.o gcc -c

Multi-file C programs C source file (dosum.c) C source file (sumnum.c) int dosum(int i, int j) { return i+j; } #include <stdio.h> int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); return 0; } this prototype of dosum( ) tells gcc about the types of dosum s arguments and its return value dosum( ) is implemented in dosum.c

Multi-file C programs C source file (dosum.c) C source file (sumnum.c) int dosum(int i, int j) { return i+j; } #include <stdio.h> int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); return 0; } why do we need this #include? where is the implementation of printf?

Compiling multi-file programs Multiple object files are linked to produce an executable - standard libraries (libc, crt1,...) are usually also linked in - a library is just a pre-assembled collection of.o files dosum.c gcc -c dosum.o ld (or gcc) sumnum sumnum.c gcc -c sumnum.o libraries (e.g., libc)

Object files sumnum.o, dosum.o are object files - each contains machine code produced by the compiler - each might contain references to external symbols variables and functions not defined in the associated.c file e.g., sumnum.o contains code that relies on printf( ) and dosum( ), but these are defined in libc.a and dosum.o, respectively - linking resolves these external symbols while smooshing together object files and libraries

Let s dive into C itself Things that are the same as Java - syntax for statements, control structures, function calls - types: int, double, char, long, float - type-casting syntax: float x = (float) 5 / 3; - expressions, operators, precedence + - * / % ++ -- = += -= *= /= %= < <= ==!= > >= &&! - scope (local scope is within a set of { } braces) - comments: /* comment */ // comment

Primitive types in C integer types type typical sizes see sizeofs.c bytes (32 bit) bytes (64 bit) 32 bit range printf - char, int floating point - float, double modifiers - short [int] - long [int, double] - signed [char, int] char 1 1 [0, 255] %c short int 2 2 [-32768,32767] %hd unsigned short int 2 2 [0, 65535] %hu int 4 4 [-214748648, 2147483647] unsigned int 4 4 [0, 4294967295] %u long int 4 8 long long int 8 8 [-2147483648, 2147483647] [-9223372036854775808, 9223372036854775807] %d %ld %lld float 4 4 approx [10-38, 10 38 ] %f double 8 8 approx [10-308, 10 308 ] %lf long double 12 16 approx [10-4932, 10 4932 ] %Lf pointer 4 8 [0, 4294967295] %p - unsigned [char, int]

C99 extended integer types Solves the conundrum of how big is a long int? #include <stdint.h> void foo(void) { int8_t w; // exactly 8 bits, signed int16_t x; // exactly 16 bits, signed int32_t y; // exactly 32 bits, signed int64_t z; // exactly 64 bits, signed } uint8_t a;...etc. // exactly 8 bits, unsigned

Similar to Java... - variables C99/C11: don t have to declare at start of a function or block need not be initialized before use (gcc -Wall will warn) #include <stdio.h> int main(int argc, char **argv) { int x, y = 5; // note x is uninitialized! long z = x+y; varscope.c } printf("z is '%ld'\n", z); // what s printed? { int y = 10; printf("y is '%d'\n", y); } int w = 20; // ok in c99 printf("y is '%d', w is '%d'\n", y, w); return 0;

Similar to Java... const - a qualifier that indicates the variable s value cannot change - compiler will issue an error if you try to violate this - why is this qualifier useful? #include <stdio.h> consty.c int main(int argc, char **argv) { const double MAX_GPA = 4.0; } printf("max_gpa: %g\n", MAX_GPA); MAX_GPA = 5.0; // illegal! return 0;

Similar to Java... for loops - C99/C11: can declare variables in the loop header if/else, while, and do/while loops - C99/C11: bool type supported, with #include <stdbool.h> - any type can be used; 0 means false, everything else true int i; loopy.c for (i = 0; i < 100; i++) { if (i % 10 == 0) { printf("i: %d\n", i); } }

Similar to Java... pointy.c parameters / return value - C always passes arguments by value - pointers lets you pass by reference more on these soon least intuitive part of C very dangerous part of C void add_pbv(int c) { c += 10; printf("pbv c: %d\n", c); } void add_pbr(int *c) { *c += 10; printf("pbr *c: %d\n", *c); } int main(int argc, char **argv) { int x = 1; printf("x: %d\n", x); add_pbv(x); printf("x: %d\n", x); add_pbr(&x); printf("x: %d\n", x); } return 0;

Very different than Java arrays - just a bare, contiguous block of memory of the correct size - an array of 10 ints requires 10 x 4 bytes = 40 bytes of memory arrays have no methods, do not know their own length - C doesn t stop you from overstepping the end of an array!! - many, many security bugs come from this

Very different than Java strings - array of char - terminated by the NULL character \0 - are not objects, have no methods; string.h has helpful utilities x h e l l o \n \0 char *x = hello\n ;

Very different than Java errors and exceptions - C has no exceptions (no try / catch) - errors are returned as integer error codes from functions - makes error handling ugly and inelegant crashes - if you do something bad, you ll end up spraying bytes around memory, hopefully causing a segmentation fault and crash objects - there aren t any; struct is closest feature (set of fields)

Very different than Java memory management - you must worry about this; there is no garbage collector - local variables are allocated off of the stack freed when you return from the function - global and static variables are allocated in a data segment are freed when your program exits - you can allocate memory in the heap segment using malloc( ) you must free malloc ed memory with free( ) failing to free is a leak, double-freeing is an error (hopefully crash)

Very different than Java Libraries you can count on - C has very few compared to most other languages - no built-in trees, hash tables, linked lists, sort, etc. - you have to write many things on your own particularly data structures error prone, tedious, hard to build efficiently and portably - this is one of the main reasons C is a much less productive language than Java, C++, python, or others

For Friday Exercise 0 is due in the morning, 10am, before class: - http://courses.cs.washington.edu/courses/cse333/17au/exercises/ex00.html - (Easier: look on the calendar or homework page for the link) - Not registered yet? Do the exercise and submit it anyway! Post a message on the discussion board - Get it to keep track of new messages for you! But before then.

HW0 and tomorrow s sections hw0 out later today - Logistics and infrastructure - should be quick - Demos & setup in sections tomorrow Bring a laptop to sections if you can w/cse VM installed if you use it - Do not be alarmed by email from GitLab when repos are created - Be sure to try to clone the repo tomorrow so we can fix glitches Friday Reference system (grading, etc.) is CSE lab/attu/vm Linux - If you use the CSE Linux VM, be sure it s the latest one and run sudo yum update hw1 (first big project) out by this weekend

See you on Friday!