lecture 1: introduction to c - mit opencourseware · pdf file6.s096 lecture 1 –...

31
6.S096 Lecture 1 – Introduction to C Welcome to the Memory Jungle Andre Kessler Andre Kessler 6.S096 Lecture 1 – Introduction to C 1/ 30

Upload: hoangbao

Post on 05-Mar-2018

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

6.S096 Lecture 1 – Introduction to CWelcome to the Memory Jungle

Andre Kessler

Andre Kessler 6.S096 Lecture 1 – Introduction to C 1 / 30

Page 2: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

1 Motivation

2 Class Logistics

3 Memory Model

4 Compiling

5 Wrap-up

Outline

Andre Kessler 6.S096 Lecture 1 – Introduction to C 2 /

1

2

3

4

5

30

Page 3: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

First Example (Python)

def binary_search( data, N, value ): lo, hi = 0, N - 1

while lo < hi: mid = ( lo + hi ) / 2

if data[mid] < value: lo = mid + 1

else: hi = mid

if hi == lo and data[lo] == value: return lo

else: return N

Andre Kessler 6.S096 Lecture 1 – Introduction to C 3 / 30

Page 4: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

First Example (C)

size_t binary_search( int *data, size_t N, int value ) { size_t lo = 0, hi = N - 1;

while( lo < hi ) { size_t mid = lo + ( hi - lo ) / 2;

if( data[mid] < value ) { lo = mid + 1;

} else { hi = mid;

} }

return ( hi == lo && data[lo] == value ) ? lo : N; }

Andre Kessler 6.S096 Lecture 1 – Introduction to C 4 / 30

Page 5: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Motivation

Why C or C++?

Speed

Andre Kessler 6.S096 Lecture 1 – Introduction to C

Graph of prog ram s peed acro ss language im plementations remove d due to copy right r estrictions.Source: http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.php.

5 / 30

Page 6: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Motivation

Why C or C++?

PowerC: direct access to memory and memory management, expressive butterse

C++: all the power of C, plus stronger typing, object-oriented andgeneric programming, and more

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

6

Page 7: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Motivation

Why C or C++?

UbiquityC: operating systems, drivers, embedded, high-performance computing

C++: large software projects everywhere

Examples: Linux kernel, Python, PHP, Perl, C#, Google searchengine/Chrome/MapReduce/etc, Firefox, MySQL, MicrosoftWindows/Office, Adobe Photoshop/Acrobat/InDesign/etc, lots offinancial/trading software, Starcraft, WoW, EA games, Doom engine,and much, much more

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

7

Page 8: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Motivation

Effective Programming

Writing good, standards-compliant code is not hard.

Doing so will make your life much easier.

There is a lot of bad code out there.

You are better than that!

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

8

Page 9: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Motivation

Effective Programming

Anyone can write good, readable, standards-compliant code.

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

9

Page 10: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Course Syllabus

Day Topic 1 Introduction to C: memory and the compiler 2 Subtleties of C: memory, floating point 3 Guest lectures: Assembly and Secure C 4 Transition from C to C++ 5 Object-oriented programming in C++ 6 Design patterns and anti-patterns 7 Generic programming: templates and more 8 Projects: putting it all together 9 Projects: continued 10 Grab-bag: coding interviews, large projects

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

10

Page 11: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Grading

6 units U credit, graded Pass/Fail Coding assignments

Three assignments worth 20%, final worth 40%. Automatic instantaneous feedback

Code reviews Two reviews of code by your peers More details later

To

Pass

at least 50% of available coding assignment points must submit both code reviews

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

11

Page 12: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Textbooks

None required. However, the following books are on reserve at the library and may be useful as references. Highly recommended if you end up doing more C/C++ coding after this course.

Recommended The C Programming Language by B. Kernighan and D. Ritchie (“K&R”) The C++ Programming Language, 4th ed. by Bjarne Stroustrop Effective C++, More Effective C++, and Effective STL by Scott Meyers

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

12

Page 13: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

1

2

3

4

Class Logistics

The Minimal C Program

nothing.c: takes no arguments, does nothing, returns 0 (“exit success”) int main(void) {

return 0; }

To compile: make nothing Previous step produced an executable named nothing To run: ./nothing Surprise! Does nothing.

But you probably have higher aspirations for your programs...

Andre Kessler 6.S096 Lecture 1 – Introduction to C /

1

2

3

4

30

13

Page 14: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Hello, world!

hello.c: takes no arguments, prints “Hello, world!”, returns 0 int main(void) {

return 0; }

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

30

14

14

Page 15: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Hello, world!

hello.c: takes no arguments, prints “Hello, world!”, returns 0 #include <stdio.h>

int main(void) { return 0;

}

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

15

Page 16: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Class Logistics

Hello, world!

hello.c: takes no arguments, prints “Hello, world!”, returns 0 #include <stdio.h>

int main(void) { printf( "Hello, world!\n" ); return 0;

}

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

16

Page 17: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

2

3

4

1

Class Logistics

Hello, world!

hello.c: takes no arguments, prints “Hello, world!”, returns 0 #include <stdio.h>

int main(void) { printf( "Hello, world!\n" ); return 0;

}

To compile: make hello Previous step produced an executable named hello To run: ./hello Hello, world!

Andre Kessler 6.S096 Lecture 1 – Introduction to C /

1

2

3

4

30

17

Page 18: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

Pointers

How do you get at this information about memory?

Through pointers; that is, the & and * operators

int a = 5; The address of a is &a. int *a ptr = &a; Read declarations from right to left. See it this way: “*a ptr is declared to be of type int.”

You can apply & to any addressable value (“lvalue”)

return &5; // error: lvalue required as unary ‘&’ operand

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

18

Page 19: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

It’s all about the memory

int a = 5; int *a ptr = &a;

Memory Address Value Identifier &a 0x7fff6f641914 0x???????????? a &a ptr 0x7fff6f641918 0x???????????? a ptr

Note: definitely a 64-bit machine, since the addresses are larger than 232 .

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

19

Page 20: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

It’s all about the memory

int a = 5; int *a ptr = &a;

Memory Address Value Identifier &a 0x7fff6f641914 0x000000000005 a &a ptr 0x7fff6f641918 0x???????????? a ptr

Note: definitely a 64-bit machine, since the addresses are larger than 232 .

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

20

Page 21: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

It’s all about the memory

int a = 5; int *a ptr = &a;

Memory Address Value Identifier &a 0x7fff6f641914 0x000000000005 a &a ptr 0x7fff6f641918 0x7fff6f641914 a ptr

Note: definitely a 64-bit machine, since the addresses are larger than 232 .

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 3021

Page 22: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

C Data Types

For the bit counts, we’re assuming a 64-bit system.

char (8) short (16), int (32),

long (64), long long (64+) float (32), double (64), long double ( 80)

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

22

Page 23: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Memory Model

C Data Types

Table of C data types removed due to copyright restrictions.

Courtesy of http://en.cppreference.com/w/cpp/language/types

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

23

Page 24: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

Development Environment

Andre Kessler 6.S096 Lecture 1 – Introduction to C /

We officially support development with gcc on Linux.

If you don’t have a computer running Linux, then that’s what today’slab time is devoted to.Some options: SSH with PuTTY, Cygwin, Xcode on Mac

Create a directory dev/

Copy the file Makefile to this directory.

To compile a file filename.c, just run “make filename”.

30

24

Page 25: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

What happens when we compile?

#include <stdio.h>

int do_thing( float a, float b ) { /* do things */

}

void call(void) { /* do stuff */ do_thing( a, b ); /* do more */

}

int main(void) { call(); return 0;

} Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

25

Page 26: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

What happens when we compile?

Three functions main, call, and do thing. Object code is produced for each When we run: the object code is loaded into memory Each function that is called is in memory, somewhere.

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 30

26

Page 27: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

Examples

Time for some examples!

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 3027

Page 28: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

With great power comes great responsibility

C is focused on speed; always checkiwould slow you down. simple typo for( int i = 0; i <corruption Memory corruption can cause totally behavior at worst At best: Segmentation fault (core du(at least it’s more obvious!)

ng array bounds/memory access

= N; ++i ) can cause

unexpected, hard-to-debug

mped)

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 3028

Page 29: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Compiling

“C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you

do, it blows your whole leg off.” — Bjarne Stroustrop, creator of the C++ programming language

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 3029

Page 30: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

Wrap-up

Wrap-up & Friday

Open lab Bring your laptops, get a C programming environment working Test out the automatic grader

Cl

u

ass on Friday Will cover floating point arithmetic, memory management, and headers in more depth.

Q estions?

Andre Kessler 6.S096 Lecture 1 – Introduction to C / 3030

Page 31: Lecture 1: Introduction to C - MIT OpenCourseWare · PDF file6.S096 Lecture 1 – Introduction to C. 28/ Compiling “C makes it easy to shoot yourself in the ... Bring your laptops,

MIT OpenCourseWarehttp://ocw.mit.edu

6.S096 Effective Programming in C and C++IAP 2014

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.