/
Intro, C refresher CSE 333 Autumn 2018 Intro, C refresher CSE 333 Autumn 2018

Intro, C refresher CSE 333 Autumn 2018 - PowerPoint Presentation

discoverfe
discoverfe . @discoverfe
Follow
342 views
Uploaded On 2020-08-26

Intro, C refresher CSE 333 Autumn 2018 - PPT Presentation

Instructor Hal Perkins Teaching Assistants Tarkan Al Kazily Renshu Gu Travis McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu Lecture Outline Course Introduction ID: 802648

sum int sumstore char int sum char sumstore cse argv max sumto file return function main system code java

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Intro, C refresher CSE 333 Autumn 2018" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Intro, C refresherCSE 333 Autumn 2018

Instructor:

Hal Perkins

Teaching Assistants:

Tarkan

Al-

Kazily

Renshu

Gu Travis

McGaha

Harshita

Neti

Thai Pham Forrest

Timour

Soumya

Vasisht

Yifan Xu

Slide2

Lecture OutlineCourse IntroductionCourse Policieshttps://courses.cs.washington.edu/courses/cse333/18au/syllabus/

C Intro

2

Slide3

Introductions: Course StaffHal Perkins (instructor)Long-time CSE faculty member and CSE 333 veteran

TAs:

Tarkan

Al-

Kazily

,

Renshu Gu, Travis McGaha, Harshita Neti, Thai Pham, Forrest Timour, Soumya Vasisht, Yifan XuAvailable in section, office hours, and discussion groupAn invaluable source of information and helpGet to know usWe are here to help you succeed!

3

Slide4

Introductions: Students~130 students this quarterThere are no overload forms or waiting lists for CSE coursesMajors must add using the UW system as space becomes available

Non-majors should work with undergraduate advisors to handle enrollment details

Expected background

Prereq

:

CSE 351 – C, pointers, memory model, linker, system calls

CSE 391 or Linux skills needed for CSE 351 assumed4

Slide5

Course Map: 100,000 foot view5

C application

C standard library (

glibc

)

C++ STL/boost/ standard library

C++ application

Java application

JRE

CPU memory storage

network

GPU

clock audio radio peripherals

HW/SW interface

(x86 + devices)

OS / app interface

(system calls)

operating system

hardware

Slide6

Systems ProgrammingThe 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 topicsConcurrency, OS interfaces and semantics, techniques for consistent data management, distributed systems algorithms, …Most important: a deep(

er

) understanding of the “layer below”

6

Slide7

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 codeAvoid write-once, read-never code

7

Slide8

Lecture OutlineCourse IntroductionCourse Policieshttps://courses.cs.washington.edu/courses/cse333/18sp/syllabus/

Summary here, but you

must

read the full details online

C Intro

8

Slide9

CommunicationWebsite: http://cs.uw.edu/333Schedule, policies, materials, assignments, etc.

Discussion:

Google group linked to course home page

Must log in using your

@

uw.edu

Google identity (not cse)Ask and answer questions – staff will monitor and contributeStaff mailing list: cse333-staff@cs for things not appropriate for discussion group (not email to instructor or individual TAs)

Course mailing list:

for announcements from staff

Registered students automatically subscribed with your @

uw

email

Office Hours:

spread throughout the week

Schedule posted now and will start today (can adjust later if needed)

Can also e-mail to staff list to make individual appointments9

Slide10

Course ComponentsLectures (~29)Introduce the concepts; take notes!!!Sections (10)Applied concepts, important tools and skills for assignments, clarification of lectures, exam review and preparation

Programming Exercises (~20)

Roughly one per lecture, due the morning before the next lecture

Coarse-grained grading (0, 1, 2, or 3)

Programming Projects (0+4)

Warm-up, then 4 “

homeworks” that build on each otherExams (2)Midterm: Friday, November 2, in classFinal: Wednesday, December 12, 2:30-4:20

10

Slide11

GradingExercises: 25% totalSubmitted via

GradeScope

(account info mailed later today)

Graded on correctness and style by TAs

Projects:

40% total

Submitted via GitLab; must tag commit that you want gradedBinaries provided if you didn’t get previous part workingExams: Midterm (15%) and Final (20%)Some old exams on course website

More details on course website

You

must

read the syllabus there – you are responsible for it

11

Slide12

Deadlines and Student ConductLate policiesExercises: no late submissions accepted, due 10 amProjects: 4 late days for entire quarter, max 2 per project

Need to get things done on time – difficult to catch up!

Academic Integrity (

read

the full policy on the web)

I trust you implicitly and will follow up if that trust is violated

In short: don’t attempt to gain credit for something you didn’t do and don’t help others do so eitherThis does not mean suffer in silence – learn from the course staff and peers, talk, share ideas; don’t share or copy work that is supposed to be yours 12

Slide13

GadgetsGadgets reduce focus and learningBursts of info (e.g. emails, IMs, etc.) are

addictive

Heavy multitaskers have more trouble focusing and shutting out irrelevant information

http://www.npr.org/2016/04/17/474525392/attention-students-put-your-laptops-away

Seriously, you will learn more if you use

paper instead!!!So how should we deal with laptops/phones/etc.?Just say no!No open gadgets during class (really!)Urge to search? – ask a question! Everyone benefits!!You may close/turn off your electronic devices nowPull out a piece of paper and pen/pencil instead 

13

Slide14

Lecture OutlineCourse IntroductionCourse Policieshttps://courses.cs.washington.edu/courses/cse333/18sp/syllabus/ C Intro

Workflow, Variables, Functions

14

Slide15

CCreated in 1972 by Dennis RitchieDesigned for creating system softwarePortable across machine architecturesMost recently updated in 1999 (C99) and 2011 (C11)

Characteristics

“Low-level” language that allows us to exploit underlying features of the architecture –

but easy to fail spectacularly (!)

Procedural (not object-oriented)

Typed but unsafe (possible to bypass the type system)

Small, basic library compared to Java, C++, most others….15

Slide16

Generic C Program Layout16

#include

<

system_files

>

#include

"

local_files

"

#define

macro_name

macro_expr

/* declare functions */

/* declare external variables &

structs

*/

int main(int argc, char* argv[]) { /* the innards */}/* define other functions */

Slide17

C Syntax: mainTo get command-line arguments in

main

, use:

int

main

(int

argc

,

char*

argv

[])

What does this mean?

argc

contains the number of strings on the command line (the executable name counts as one, plus one for each argument).

argv is an array containing pointers to the arguments as strings (more on pointers later)Example:

$ foo hello 87argc = 3

argv[0]="foo", argv[1]="hello", argv[2]="87"17int main(int argc, char* argv[])

Slide18

C WorkflowEditor (

emacs

,

vi

) or IDE (

eclipse

)18

Source files

(

.c

,

.h

)

Object files (

.o

)

“COMPILE” (compile + assemble)

LINK

LOAD

EXECUTE, DEBUG, …

EDIT

foo.c

bar.c

foo.h

foo.o

bar.o

libZ.a

bar

Statically-linked

libraries

bar

LINK

libc.so

Shared libraries

LINK

Slide19

C to Machine Code19

C source file

(

sumstore.c

)

Assembly file (

sumstore.s

)

C compiler (

gcc

–S

)

Assembler (

gcc

-c

or

as

)

EDIT

void

sumstore

(

int

x,

int

y,

int

*

dest

) {

*

dest

= x + y;

}

sumstore

:

addl

%

edi

,

%

esi

movl

%

esi

,

(%

rdx

)

ret

Machine code

(

sumstore.o

)

400575:

01

fe

89 32

c3

C compiler

(

gcc

–c

)

Slide20

When Things Go South…Errors and ExceptionsC does not have exception handling (no try

/

catch

)

Errors are returned as integer error codes from functions

Because of this, error handling is ugly and inelegant

CrashesIf you do something bad, you hope to get a “segmentation fault” (believe it or not, this is the “good” option)20

Slide21

Java vs. C (351 refresher)Are Java and C mostly similar (S) or significantly different (D) in the following categories?List any differences you can recall (even if you put ‘S’)

21

Language Feature

S/D

Differences in C

Control structures

Primitive datatypes

Operators

Casting

Arrays

Memory management

Slide22

Java vs. C (351 refresher)Are Java and C mostly similar (S) or significantly different (D) in the following categories?List any differences you can recall (even if you put ‘S’)

22

Language Feature

S/D

Differences in C

Control structures

S

Primitive datatypes

S/D

Similar but sizes can differ (char, esp.), unsigned, no

boolean

, uninitialized data, …

Operators

S

Java has >>>, C has ->

Casting

D

Java enforces type safety, C does not

Arrays

DNot objects, don’t know their own length, no bounds checking

Memory managementDManual (malloc/free), no garbage collection

Slide23

Primitive Types in CInteger types

char

,

int

Floating point

float

, double

Modifiers

short

[

int

]

long

[int, double]

signed [char, int]unsigned

[char, int]23

C Data Type

32-bit64-bitprintf

char

1

1

%c

short

int

2

2

%

hd

unsigned short

int

2

2

%

hu

int

4

4

%d

/

%

i

unsigned

int

4

4

%u

long

int

4

8

%

ld

long

long

int

8

8

%

lld

float

4

4

%f

double

8

8

%lf

long double

12

16

%Lf

pointer

4

8

%p

Typical sizes – see

sizeofs.c

Slide24

C99 Extended Integer TypesSolves the conundrum of “how big is an

long

int

?”

24

void

sumstore

(

int

x,

int

y,

int

*

dest

) {

void

sumstore(int32_t x, int32_t y, int32_t* dest) {

#include

<

stdint.h>void

foo

(

void

) {

int8_t

a;

// exactly 8 bits, signed

int16_t

b;

// exactly 16 bits, signed

int32_t

c;

// exactly 32 bits, signed

int64_t

d;

// exactly 64 bits, signed

uint8_t

w;

// exactly 8 bits, unsigned

...

}

Use extended types in cse333 code

Slide25

Basic Data StructuresC does not support objects!!!Arrays are contiguous chunks of memory

Arrays have no methods and do not know their own length

Can easily run off ends of arrays in C –

security bugs!!!

Strings

are null-terminated char arrays

Strings have no methods, but string.h has helpful utilitiesStructs are the most object-like feature, but are just collections of fields – no “methods” or functions

25

x

h

e

l

l

o

\n

\0

char*

x =

"hello\n"

;

Slide26

Function DefinitionsGeneric format:

26

// sum of integers from 1 to max

int

sumTo

(

int

max) {

int

i

, sum =

0

;

for

(

i = 1; i <= max; i++) { sum += 1; } return sum;}

returnType fname

(type param1, …, type

paramN) { // statements

}

Slide27

Function OrderingYou shouldn’t call a function that hasn’t been declared yet

27

#include

<

stdio.h

>

int

main

(

int

argc

,

char**

argv

) {

printf("sumTo(5) is: %d\n", sumTo(5)); return 0;}

// sum of integers from 1 to maxint sumTo

(int max) {

int i, sum =

0

;

for

(

i

=

1

;

i

<= max;

i

++) {

sum +=

1

;

}

return

sum;

}

sum_badorder.c

Slide28

Solution 1: Reverse OrderingSimple solution; however, imposes ordering restriction on writing functions (who-calls-what?)

28

#include

<

stdio.h

>

// sum of integers from 1 to max

int

sumTo

(

int

max) {

int

i

, sum =

0

; for (i = 1; i <= max; i++) { sum += 1; } return

sum;}int

main(int

argc, char**

argv

) {

printf

(

"

sumTo

(5) is: %d\n"

,

sumTo

(

5

));

return

0

;

}

sum_betterorder.c

Slide29

Solution 2: Function DeclarationTeaches the compiler arguments and return types; function definitions can then be in a logical order

29

sum_declared.c

#include

<

stdio.h

>

int

sumTo

(

int

);

//

func

prototype

int

main

(int argc, char** argv) { printf("

sumTo(5) is: %d\n", sumTo

(5));

return 0;

}

// sum of integers from 1 to max

int

sumTo

(

int

max) {

int

i

, sum =

0

;

for

(

i

=

1

;

i

<= max;

i

++) {

sum +=

1

;

}

return

sum;

}

Hint: code examples from slides are on the course web for you to experiment with

Slide30

Function Declaration vs. DefinitionC/C++ make a careful distinction between these twoDefinition: the thing itself

e.g.

code for function, variable definition that creates storage

Must be

exactly one

definition of each thing (no duplicates)

Declaration: description of a thinge.g. function prototype, external variable declarationOften in header files and incorporated via #includeShould also #include

declaration in the file with the actual definition to check for consistency

Needs to appear in

all files

that use that thing

Should appear before first use

30

Slide31

Multi-file C Programs31

void

sumstore

(

int

x,

int

y,

int

*

dest

) {

*

dest

= x + y;

}

#include

<stdio.h>void sumstore(int x, int

y, int*

dest);

int main

(

int

argc

,

char**

argv

) {

int

z, x =

351

, y =

333

;

sumstore

(

x,y,&z

);

printf

(

"%d + %d = %d\n"

,

x,y,z

);

return

0

;

}

C source file 1

(

sumstore.c

)

C source file 2

(

sumnum.c

)

Compile together:

$

gcc

-o

sumnum

sumnum.c

sumstore.c

Slide32

Compiling Multi-file ProgramsThe linker combines multiple object files plus statically-linked libraries to produce an executable

Includes many standard libraries (

e.g.

libc

,

crt1)A library is just a pre-assembled collection of .o files

32

sumstore.c

sumnum.c

sumstore.o

sumnum.o

libraries

(

e.g.

libc

)

sumnum

gcc

-c

gcc

-c

ld

or

gcc

Slide33

Peer Instruction QuestionWhich of the following statements is FALSE?Vote at http://PollEv.com/justinh

With the standard

main()

syntax, It is always safe to use

argv

[0]

.

We can’t use

uint64_t

on a 32-bit machine because there isn’t a C integer primitive of that length.

Using function declarations is beneficial to both single- and multi-file C programs.

When compiling multi-file programs, not all linking is done by the Linker.

We’re lost…

33

Slide34

To-do List (1)Explore the website thoroughly: http://cs.uw.edu/333

Computer setup: CSE lab,

attu

, or CSE Linux VM

Exercise 0

is

due Friday before class (10 am)Find exercise spec on website, submit via GradescopeSample solution will be posted Friday after classGradescope accounts created just before classUserid is your uw.edu email address

Exercise submission: find CSE 333 18au, click on the exercise, drag-n-drop file(s)! That’s it!! Ignore any messages about

autograding

– we don’t use that.

34

Slide35

To-do List (2)Homework 0 out later today, due Monday nightLogistics and infrastructure for projectsGitlab email sent later today when repos created – no action needed right away

Demos and setup in part of section tomorrow –

bring laptop!

Updated CSE VM for 18au. If you use the VM, get the new version and get it running on your laptop before section

Not your old one from CSE 351;

gcc

/g++/etc. updated for 18auReference system for grading is CSE lab/attu/VMYour job to be sure your solution(s) work there35