CSE 303: Concepts and Tools for Software Development

Similar documents
CSE 374 Programming Concepts & Tools

CSE 303 Lecture 8. Intro to C programming

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

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CSE 303: Concepts and Tools for Software Development

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS 392/681 Lab 6 Experiencing Buffer Overflows and Format String Vulnerabilities

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

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

C for C++ Programmers

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSE 12 Spring 2018 Week One, Lecture Two

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

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

CSE 374 Midterm Exam Sample Solution 2/6/12

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

Programming in C UVic SEng 265

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

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

High Performance Computing MPI and C-Language Seminars 2009

Algorithms, Data Structures, and Problem Solving

CSE 12 Spring 2016 Week One, Lecture Two

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

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

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Lecture 07 Debugging Programs with GDB

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Systems Programming and Computer Architecture ( )

Array Initialization

CSE 333 Lecture 2 - arrays, memory, pointers

Dynamic memory allocation (malloc)

CSE 303: Concepts and Tools for Software Development

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

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Today s class. Review of more C Operating system overview. Informationsteknologi

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

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Memory Corruption 101 From Primitives to Exploit

Introduction to Computer Systems

ch = argv[i][++j]; /* why does ++j but j++ does not? */


Lecture 03 Bits, Bytes and Data Types

Introduction to Computer Systems

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Kernels & Processes The Structure of the Operating System

ENEE 457: Computer Systems Security. Lecture 16 Buffer Overflow Attacks

HW 8 CS681 & CS392 Computer Security Understanding and Experimenting with Memory Corruption Vulnerabilities DUE 12/18/2005

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.

CS61C : Machine Structures

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Lecture 9 Assertions and Error Handling CS240

6.S096 Lecture 1 Introduction to C

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

CAAM 420 Notes Chapter 2: The C Programming Language

Programming refresher and intro to C programming

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

CS 11 Ocaml track: lecture 3

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

Problem Set 1: Unix Commands 1

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

COMP s1 Lecture 1

18-600: Recitation #3

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Section 2: Processes

Welcome to... CS113: Introduction to C

Lecture Notes for 04/04/06: UNTRUSTED CODE Fatima Zarinni.

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Lecture 7: file I/O, more Unix

Inter-Process Communication

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

A brief introduction to C programming for Java programmers

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

CSE 303 Midterm Exam

Important From Last Time

Chris Riesbeck, Fall Introduction to Computer Systems

CS240: Programming in C

COSC Software Engineering. Lecture 16: Managing Memory Managers

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

Topics Introduction to Microprocessors

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

Week 3. This is CS50. Harvard University. Fall Cheng Gong

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

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

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Transcription:

CSE 303: Concepts and Tools for Software Development Hal Perkins Winter 2009 Lecture 7 Introduction to C: The C-Level of Abstraction CSE 303 Winter 2009, Lecture 7 1

Welcome to C Compared to Java, in rough order of importance Lower level (less for compiler to do) Unsafe (wrong programs might do anything) Procedural programming not object-oriented Standard library is much smaller. Many similar control constructs (loops, ifs,...) Many syntactic similarities (operators, types,...) A different world-view and much more to keep track of; Java-like thinking can get you in trouble. CSE 303 Winter 2009, Lecture 7 2

Our plan A semi-nontraditional way to learn C: Learn how C programs actually run on machines like attu Not promised by C s definition You do not need to reason in terms of the implementation when you follow the rules. But it does help to know this model To remember why C has the rules it does To debug incorrect programs Learn some C basics (including Hello World! ) Learn what C is (still) used for Learn more about the language and good idioms Later: A little C++ CSE 303 Winter 2009, Lecture 7 3

Some References There s a lot on the web, but here are some primary sources. C: A Reference Manual, Harbison & Steele (currently 5th ed.). Probably the best current reference on C and its libraries, updated with information about recent versions of the C standard. The C Programming Language, Kernighan & Ritchie. K&R is a classic, one that every programmer must read. A bit dated now (doesn t include C99 extensions), but the primary source. Essential C, Stanford CS lib, http://cslibrary.stanford.edu/101/essentialc.pdf. Good short introduction to the language by some friends at Stanford. CSE 303 Winter 2009, Lecture 7 4

Address space Simple model of a running process (provided by the O/S): There is one address space (an array of bytes) Most common size today for a machine like attu is 2 32 We will assume 32 for now, though you often shouldn t That is more RAM than you (probably) have (O/S maintains illusion; may lead to slowness) Subscripting this array takes 32 bits Something s address is its position in this array. Trying to read a not-used part of the array may cause a segmentation fault (immediate crash). All data and code for the process are in this address space. Code and data are bits; program remembers what is where. O/S also lets you read/write files, stdin, stdout, stderr. CSE 303 Winter 2009, Lecture 7 5

Address-space layout The following is definitely different on different systems, but it s one way to understand how C is implemented: code globals heap... stack So in one array of 8-bit bytes we have: Code instructions (typically immutable) Space for global variables (mutable and immutable) (like Java s static fields) A heap for other data (like objects returned by Java s new) Unused portions; access causes seg-fault A call-stack holding local variables and code addresses Note: Assuming an int occupies 4 bytes CSE 303 Winter 2009, Lecture 7 6

The stack The call-stack (or just stack) has one part or frame (compiler folks call it an activation record) for each function (cf. Java method) call that has not yet returned. It holds: Room for local variables The return address (index into code for what to execute after the function is done) CSE 303 Winter 2009, Lecture 7 7

What could go wrong? Remember, the programmer has to keep the bits straight even though C deals in terms of variables, functions, data structures, etc. (not bits). If arr is an array of 10 elements, arr[30] accesses some other thing. Writing 8675309 where a return address should be makes a function start executing stuff that may not be code.... Correct C programs can t do these things, but nobody is perfect. On the plus side, there is no unnecessary overhead like keeping array lengths around and checking them! Okay, time to see C... CSE 303 Winter 2009, Lecture 7 8

Hello, World! #include<stdio.h> int main(int argc, char**argv) { fputs("hello, World!\n",stdout); return 0; } Compiling: gcc -o hi hello.c (usually add -Wall -g) Running:./hi Intuitively: main gets called with the command-line args and the program exits when it returns. But there is a lot going on in terms of what the language constructs mean, what the compiler does, and what happens when the program runs. We will focus mostly on the language. CSE 303 Winter 2009, Lecture 7 9

Quick Hello Explanation #include<stdio.h> int main(int argc, char**argv) { fputs("hello, World!\n",stdout); return 0; } #include finds the file stdio.h (from where?) and includes its entire contents. (stdio.h describes fputs and stdout.) A function definition is much like a Java method (return type, name, arguments with types, braces, body); it is not part of a class and there are no built-in objects or this. An int is like in Java, though its size depends on the compiler (it is 32 bits on attu). main is a special function name; every full program has one. char** is a long story... CSE 303 Winter 2009, Lecture 7 10

Pointers Think address, i.e., an index into the address-space array. If argv is a pointer, then *argv returns the pointed-to value. So does argv[0]. And if argv points to an array of 2 values, then argv[1] returns the second one (and so does *(argv+1) but the + here is funny). People like to say arrays and pointers are the same thing in C. this is sloppy talking, but people say it anyway. Type syntax: t* describes either NULL (seg-fault if you dereference it) A pointer holding the address of some number of values of type t. How many? You have to know somehow; no length primitive. CSE 303 Winter 2009, Lecture 7 11

Pointers, continued So reading right to left: argv (of type char**) holds a pointer to (one or more) pointer(s) to (one or more) char(s). Fact #1 about main: argv holds a pointer to j pointers to (one or more) char(s) where argc holds j. Common idiom: array lengths as other arguments. Fact #2 about main: For 0 i j where argc holds j, argv[i] is an array of char(s) with last element equal to the character \0 (which is not 0 ). Very common idiom: pointers to char arrays ending with \0 are called strings. The standard library and language often use this idiom. [Let s draw a picture of memory when hi runs.] CSE 303 Winter 2009, Lecture 7 12

Rest of the story #include<stdio.h> int main(int argc, char**argv) { fputs("hello, World!\n",stdout); return 0; } fputs is a function taking a string (a char*) and a FILE* (where FILE is a type defined in stdio.h). "Hello, World!\n" evaluates to a pointer to a global, immutable array of 15 characters (including a trailing \0, \n is one character). stdout is a global variables of type FILE* defined in stdio.h. How this gets hooked up to the screen (or somewhere else) is the library s (nontrivial) problem. CSE 303 Winter 2009, Lecture 7 13

Variations of Hello World Things we can try to understand, time permitting: print command-line arguments use printf change exit values use local variables to hold strings update string elements (won t work for constant strings) update string elements (including buffer overflow and overwriting the \0 and putting a \0 early. CSE 303 Winter 2009, Lecture 7 14