CSE 333 Lecture 2 - gentle re-introduction to C

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

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

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

CSE 303 Lecture 8. Intro to C programming

CSE 333 Lecture 2 Memory

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

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

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

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

CSE 374 Programming Concepts & Tools

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

CSE 303: Concepts and Tools for Software Development

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.

CSE 333 Lecture 6 - data structures

CS61, Fall 2012 Section 2 Notes

Arrays and Memory Management

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

Kurt Schmidt. October 30, 2018

PRINCIPLES OF OPERATING SYSTEMS

CSCI 171 Chapter Outlines

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

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

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

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

CSE 333 Lecture 2 - arrays, memory, pointers

Final CSE 131B Spring 2004

Processes. Johan Montelius KTH

CS Prof J.P.Morrison

A process. the stack

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

SWEN-250 Personal SE. Introduction to C

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

Important From Last Time

CS201 - Lecture 1 The C Programming Language

Lecture 03 Bits, Bytes and Data Types

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

Important From Last Time

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

Important From Last Time

Lecture 7: file I/O, more Unix

CSE 333 Midterm Exam July 24, Name UW ID#

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

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

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

POINTER AND ARRAY SUNU WIBIRAMA

EL2310 Scientific Programming

COSC Software Engineering. Lecture 16: Managing Memory Managers

Reviewing gcc, make, gdb, and Linux Editors 1

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

High Performance Programming Programming in C part 1

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

18-642: Code Style for Compilers

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

CSE 333 Lecture 7 - final C details

CSE 333 Lecture 5 - data structures & modules

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Topics Introduction to Microprocessors

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.


EL2310 Scientific Programming

CSE 333 Lecture 3 - arrays, memory, pointers

Contents. A Review of C language. Visual C Visual C++ 6.0

just a ((somewhat) safer) dialect.

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

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

Programming in C. What is C?... What is C?

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

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

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

edunepal_info

EL2310 Scientific Programming

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

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

ELEC 377 C Programming Tutorial. ELEC Operating Systems

A brief introduction to C programming for Java programmers

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

High Performance Computing MPI and C-Language Seminars 2009

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

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

High Performance Computing

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Kernels & Processes The Structure of the Operating System

QUIZ. What is wrong with this code that uses default arguments?

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 333 Midterm Exam 7/27/15 Sample Solution

Preview from Notesale.co.uk Page 6 of 52

Lecture 3: C Programm

CSE 333 Midterm Exam 5/10/13

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

CSE 12 Spring 2016 Week One, Lecture Two

Data Structure Series

Exercise Session 2 Systems Programming and Computer Architecture

Transcription:

CSE 333 Lecture 2 - gentle re-introduction to C Steve Gribble Department of Computer Science & Engineering University of Washington

HW0 results question T F I have programmed in C before 89% 11% I have programmed in C++ before 26% 74% languages: Java (100%), Python (9%), x86 (4%), C# (5%), Ruby (0%), JavaScript (10%), PHP (15%), Pascal (0%), Haskell (0%), visual basic (2%) I am taking 332 right now 95% 5% I know what a hash table is 100% 0%

HW0 results question T F I know what a hash table is 100% 0% I have implemented a hash table 78% 22% I know what a C pointer is 91% 9% I have debugged pointer bugs 65% 35% I know what (*(x+5))[5] = &y; means 54% 46%

HW0 results unsigned char *mystery_function(unsigned short bufsize) { unsigned char *tmp_buf; if (bufsize == 0) return NULL; tmp_buf = malloc(bufsize); if (tmp_buf == NULL) return NULL; if (verify_something() == 0) // something bad happened return NULL; return tmp_buf;

HW0 results question T F spot the bug: I don t know 50%, 25% memory leak, 10% type error, 5% multiple of 4 issue, 10% other Linux: 0% never, 85% < 1 year, 13% years, 2% expert I know what an inode is 7% 93% I know what a socket is 17% 83% I ve written multithreaded code 89% 11%

HW0 results what is the air-speed velocity of an unladen swallow? African or European? 48% 24 miles an hour 16% 8-11 m/s 30% it depends 8% 500 1% Red. No, blue. AHHHH... 1%

Today s goals: - overview of the C material you learned from cse351 Next two weeks goals: - dive in deep into more advanced C topics - start writing some C code - introduce you to interacting with the OS

Attribution The slides I ll be using are a mixture of: - my own material - slides from other UW CSE courses (CSE303, CSE351; thanks Magda Balazinska, Marty Stepp, John Zahorjan, Hal Perkins, and others!!) - material from other universities courses (particularly CMU s 15-213 and some Harvard courses; thanks Randy Bryant, Dave O Hallaron, Matt Welsh, and others!!) All mistakes are mine. (No, really.)

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

C Created in 1972 by Dennis Ritchie - designed for creating system software - portable across machine architectures This book was typeset (pic tbl - most recently updated in 1999 (C99) eqn troff -ms) using an Autologic APS-5 phototypesetter and a DEC Characteristics VAX 8550 running the 9th Edition of the smaller UNIX operating - low-level, standardsystem. library than Java - procedural (not object-oriented) - typed but unsafe; incorrect programs can fail spectacularly

Mindset of C The PDP-11/45 on which our UNIX installation is implemented is a: - 16-bit word (8-bit byte) computer with 144K bytes of core memory; UNIX occupies 42K bytes a 1M byte fixed-head disk a moving-head disk with 40M byte disk packs - The greater part of UNIX software is written in C. Dennis M. Ritchie and Ken Thompson Bell Laboratories 1974

C workflow Editor (emacs, vi) or IDE (eclipse)

C workflow source files (.c,.h) Editor (emacs, vi) or IDE (eclipse) edit foo.h foo.c bar.c

C workflow source files (.c,.h) Editor (emacs, vi) or IDE (eclipse) edit foo.h foo.c bar.c libz.a statically linked libraries libc.so shared libraries

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 libz.a statically linked libraries libc.so shared libraries

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 link bar libz.a statically linked libraries libc.so shared libraries

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 process link bar libz.a libc.so statically linked libraries shared libraries

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.so statically linked libraries shared libraries

From C to machine code C source file (dosum.c) int dosum(int i, int j) { return i+j;

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

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) int dosum(int i, int j) { return i+j; #include <stdio.h> C source file (sumnum.c) int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); return 0;

Multi-file C programs C source file (dosum.c) int dosum(int i, int j) { return i+j; C source file (sumnum.c) #include <stdio.h> int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); return 0; dosum( ) is implemented in sumnum.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 sumnum.c

Multi-file C programs C source file (dosum.c) int dosum(int i, int j) { return i+j; #include <stdio.h> C source file (sumnum.c) int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); return 0;

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

Multi-file C programs C source file (dosum.c) int dosum(int i, int j) { return i+j; C source file (sumnum.c) #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 sumnums, 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 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 [-2147483648, 2147483647] long long int 8 8 [-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 Solve 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 w;...etc. // exactly 8 bits, unsigned

Similar to Java... - variables must declare at the start of a function or block (changed in C99) 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 - can t declare variables in the loop header (changed in c99) if/else, while, and do/while loops - no boolean type (changed in c99) - 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 to 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 console I/O - C standard library has portable routines for reading/writing scanf, printf file I/O - C standard library has portable routines for reading/writing fopen, fread, fwrite, fclose, etc. does buffering by default, is blocking by default - OS provides (less portable) routines we ll be using these: more control over buffering, blocking

Very different than Java network I/O - C standard library has no notion of network I/O - OS provides (somewhat portable) routines - lots of complexity lies here errors: network can fail performance: network can be slow concurrency: servers speak to thousands of clients simultaneously

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

See you on Friday!