6.S096 Lecture 1 Introduction to C



Similar documents
CSC230 Getting Starting in C. Tyler Bletsch

COS 217: Introduction to Programming Systems

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

6.s096. Introduction to C and C++

Sources: On the Web: Slides will be available on:

arrays C Programming Language - Arrays

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

CS 253: Intro to Systems Programming

Lecture 22: C Programming 4 Embedded Systems

The C Programming Language course syllabus associate level

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

An Incomplete C++ Primer. University of Wyoming MA 5310

C++ INTERVIEW QUESTIONS

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology

Lecture 27 C and Assembly

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

5 Arrays and Pointers

CPSC 226 Lab Nine Fall 2015

C# and Other Languages

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

Object Oriented Software Design II

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Informatica e Sistemi in Tempo Reale

Getting Started with 1. Borland C++Builder Compiler

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

The programming language C. sws1 1

Chapter 3.2 C++, Java, and Scripting Languages. The major programming languages used in game development.

CP Lab 2: Writing programs for simple arithmetic problems

Semantic Analysis: Types and Type Checking

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Applying Clang Static Analyzer to Linux Kernel

Lecture 2 Notes: Flow of Control

Fundamentals of Programming

C Programming Tutorial

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

CMSC Fundamentals of Computer Programming II (C++)

CSCE 665: Lab Basics. Virtual Machine. Guofei Gu

Lab 4: Socket Programming: netcat part

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

1 Description of The Simpletron

Selection Statements

C Programming Review & Productivity Tools

Chapter 13 Storage classes

1/20/2016 INTRODUCTION

A deeper look at Inline functions

Interface and Simulation of a LCD Text Display

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

1001ICT Introduction To Programming Lecture Notes

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 5 Names, Bindings, Type Checking, and Scopes

ERIKA Enterprise pre-built Virtual Machine

Syllabus Introduction to C++ Programming and Numerical Analysis Spring 2016

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Comp151. Definitions & Declarations

1 Abstract Data Types Information Hiding

C++ Programming Language

Introduction to Programming II Winter, 2014 Assignment 2

Parallelization: Binary Tree Traversal

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Testing for Security

Introduction to Python

COMP[29]041 - Software Construction 15s2. Course Goals. Course Goals. 41 Introduction 15s2]COMP[29]041 Introduction 15s2

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

General Software Development Standards and Guidelines Version 3.5

CS 103 Lab Linux and Virtual Machines

Introduction to Java

CISC 181 Project 3 Designing Classes for Bank Accounts

Keil C51 Cross Compiler

Parallel and Distributed Computing Programming Assignment 1

CS 535 Course Syllabus. Basics of Applications PNA Chap 4. pp

C++FA 5.1 PRACTICE MID-TERM EXAM

1. Boolean Data Type & Expressions Outline. Basic Data Types. Numeric int float Non-numeric char

C++ Overloading, Constructors, Assignment operator

13 Classes & Objects with Constructors/Destructors

Introduction to GPU hardware and to CUDA

CMPT 183 Foundations of Computer Science I

How To Write Portable Programs In C

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Operating Systems. Privileged Instructions

How to Write a Simple Makefile

Chapter 7D The Java Virtual Machine

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Moving from CS 61A Scheme to CS 61B Java

C Programming Language

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

School of Computing and Information Sciences. Course Title: Computer Programming III Date: April 9, 2014

Transcription:

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

Outline 1 Motivation 2 Class Logistics 3 Memory Model 4 Compiling 5 Wrap-up Andre Kessler 6.S096 Lecture 1 Introduction to C 2 / 30

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

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

Motivation Why C or C++? Speed Graph of program s peed acro ss language implementations remove d due to copyright restrictions. Source: http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.php. Andre Kessler 6.S096 Lecture 1 Introduction to C 5 / 30

Motivation Why C or C++? Power C: direct access to memory and memory management, expressive but terse C++: all the power of C, plus stronger typing, object-oriented and generic programming, and more Andre Kessler 6.S096 Lecture 1 Introduction to C 6/ 30

Motivation Why C or C++? Ubiquity C: operating systems, drivers, embedded, high-performance computing C++: large software projects everywhere Examples: Linux kernel, Python, PHP, Perl, C#, Google search engine/chrome/mapreduce/etc, Firefox, MySQL, Microsoft Windows/Office, Adobe Photoshop/Acrobat/InDesign/etc, lots of financial/trading software, Starcraft, WoW, EA games, Doom engine, and much, much more Andre Kessler 6.S096 Lecture 1 Introduction to C / 30 7

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 8/ 30

Motivation Effective Programming Anyone can write good, readable, standards-compliant code. Andre Kessler 6.S096 Lecture 1 Introduction to C 9/ 30

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 10/ 30

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

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

Class Logistics The Minimal C Program nothing.c: takes no arguments, does nothing, returns 0 ( exit success ) int main(void) { return 0; } 1 2 3 4 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 13/ 30

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 14/ 30

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 15/ 30

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 16/ 30

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; } 1 2 3 4 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 17 / 30

Memory Model Andre Kessler 6.S096 Lecture 1 Introduction to C / 30 18 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

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 2 32. Andre Kessler 6.S096 Lecture 1 Introduction to C / 30 19

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 2 32. Andre Kessler 6.S096 Lecture 1 Introduction to C 20 / 30

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 2 32. Andre Kessler 6.S096 Lecture 1 Introduction to C 21 / 30

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 22/ 30

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

Compiling Development Environment We officially support development with gcc on Linux. If you don t have a computer running Linux, then that s what today s lab 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. Andre Kessler 6.S096 Lecture 1 Introduction to C 24 / 30

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

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 26/ 30

Compiling Examples Time for some examples! Andre Kessler 6.S096 Lecture 1 Introduction to C 27 / 30

Compiling With great power comes great responsibility C is focused on speed; always checking array bounds/memory access would slow you down. simple typo for( int i = 0; i < = N; ++i ) can cause corruption Memory corruption can cause totally behavior at worst At best: Segmentation fault (core dumped) (at least it s more obvious!) unexpected, hard-to-debug Andre Kessler 6.S096 Lecture 1 Introduction to C 28 / 30

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 29 / 30

Wrap-up Wrap-up & Friday Open lab Bring your laptops, get a C programming environment working Test out the automatic grader Class on Friday Will cover floating point arithmetic, memory management, and headers in more depth. Questions? Andre Kessler 6.S096 Lecture 1 Introduction to C 30 / 30

MIT OpenCourseWare http://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.