6.087 Lecture 5 January 15, 2010

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

Analysis of Binary Search algorithm and Selection Sort algorithm

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

6. Standard Algorithms

The C Programming Language course syllabus associate level

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

The programming language C. sws1 1

Module 816. File Management in C. M. Campbell 1993 Deakin University

5 Arrays and Pointers

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

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

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

Informatica e Sistemi in Tempo Reale

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

C++ Programming Language

Lecture 22: C Programming 4 Embedded Systems

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

File Handling. What is a file?

Object Oriented Software Design

Tutorial on C Language Programming

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Object Oriented Software Design

System Calls and Standard I/O

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

6.S096 Lecture 1 Introduction to C

Pseudo code Tutorial and Exercises Teacher s Version

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Algorithms. Margaret M. Fleck. 18 October 2010

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Faculty of Engineering Student Number:

Symbol Tables. Introduction

Basic Programming and PC Skills: Basic Programming and PC Skills:

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Lecture 1: Data Storage & Index

Linux Driver Devices. Why, When, Which, How?

Embedded Systems Design Course Applying the mbed microcontroller

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

How To Write Portable Programs In C

So far we have considered only numeric processing, i.e. processing of numeric data represented

Unit Storage Structures 1. Storage Structures. Unit 4.3

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LESSON PLAN

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Binary search algorithm

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

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Record Storage and Primary File Organization

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Zabin Visram Room CS115 CS126 Searching. Binary Search

Illustration 1: Diagram of program function and data flow

DATA STRUCTURES USING C

Binary Heap Algorithms

Data Structures and Data Manipulation

Introduction to Java

El Dorado Union High School District Educational Services

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

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

Analysis of a Search Algorithm

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

C Strings and Pointers

CSC 2405: Computer Systems II

arrays C Programming Language - Arrays

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

AP Computer Science AB Syllabus 1

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Converting a Number from Decimal to Binary

Introduction to Data Structures

Introduction to Information Security

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Leak Check Version 2.1 for Linux TM

Chapter 2: Elements of Java

DNA Data and Program Representation. Alexandre David

Physical Data Organization

Semantic Analysis: Types and Type Checking

Moving from CS 61A Scheme to CS 61B Java

MISRA-C:2012 Standards Model Summary for C / C++

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

X86-64 Architecture Guide

Computer Science Questions and Answers UNIT-A Chapter - 1 Configuring a Computer

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

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

7.1 Our Current Model

Object Oriented Software Design II

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Stacks. Linear data structures

4.2 Sorting and Searching

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

10CS35: Data Structures Using C

Why you shouldn't use set (and what you should use instead) Matt Austern

Java Interview Questions and Answers

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

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

Chapter 13: Query Processing. Basic Steps in Query Processing

Transcription:

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 1

Review: Unconditional jumps goto keyword: jump somewhere else in the same function Position identified using labels Example (for loop) using goto: { i n t i = 0, n = 20; / i n i t i a l i z a t i o n / goto loop_cond ; loop_body : / body of loop here / i ++; loop_cond : i f ( i < n ) / loop c o n d i t i o n / goto loop_body ; } Excessive use of goto results in spaghetti code 1

Review: I/O Functions I/O provided by stdio.h, not language itself Character I/O: putchar(), getchar(), getc(), putc(), etc. String I/O: puts(), gets(), fgets(), fputs(), etc. Formatted I/O: fprintf(), fscanf(), etc. Open and close files: fopen(), fclose() File read/write position: feof(), fseek(), ftell(), etc.... 2

Review: printf() and scanf() Formatted output: int printf (char format [], arg1, arg2,...) Takes variable number of arguments Format specification: %[flags][width][.precision][length]<type> types: d, i (int), u, o, x, X (unsigned int), e, E, f, F, g, G (double), c (char), s (string) flags, width, precision, length - modify meaning and number of characters printed Formatted input: scanf() - similar form, takes pointers to arguments (except strings), ignores whitespace in input 3

Review: Strings and character arrays Strings represented in C as an array of characters (char []) String must be null-terminated ( \0 at end) Declaration: char str [] = "I am a string."; or char str[20] = "I am a string."; strcpy() - function for copying one string to another More about strings and string functions today... 4

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 5

Pointers and addresses Pointer: memory address of a variable Address can be used to access/modify a variable from anywhere Extremely useful, especially for data structures Well known for obfuscating code 5

Physical and virtual memory Physical memory: physical resources where data can be stored and accessed by your computer cache RAM hard disk removable storage Virtual memory: abstraction by OS, addressable space accessible by your code 6

Physical memory considerations Different sizes and access speeds Memory management major function of OS Optimization to ensure your code makes the best use of physical memory available OS moves around data in physical memory during execution Embedded processors may be very limited 7

Virtual memory How much physical memory do I have? Answer: 2 MB (cache) + 2 GB (RAM) + 100 GB (hard drive) +... How much virtual memory do I have? Answer: <4 GB (32-bit OS), typically 2 GB for Windows, 3-4 GB for linux Virtual memory maps to different parts of physical memory Usable parts of virtual memory: stack and heap stack: where declared variables go heap: where dynamic memory goes 8

Addressing variables Every variable residing in memory has an address! What doesn t have an address? register variables constants/literals/preprocessor defines expressions (unless result is a variable) How to find an address of a variable? The & operator i n t n = 4; double p i = 3. 14159; i n t pn = &n ; / address of i n t e g e r n / double ppi = &p i ; / address of double p i / Address of a variable of type t has type t * 9

Dereferencing pointers I have a pointer now what? Accessing/modifying addressed variable: dereferencing/indirection operator * / p r i n t s " p i = 3.14159\n " / p r i n t f ( "pi = %g\n", ppi ) ; / p i now equals 7.14159 / ppi = ppi + pn ; Dereferenced pointer like any other variable null pointer, i.e. 0 (NULL): pointer that does not reference anything 10

Casting pointers Can explicitly cast any pointer type to any other pointer type ppi = (double )pn; / pn originally of type ( int ) / Implicit cast to/from void * also possible (more next week... ) Dereferenced pointer has new type, regardless of real type of data Possible to cause segmentation faults, other difficult-to-identify errors What happens if we dereference ppi now? 11

Functions with multiple outputs Consider the Extended Euclidean algorithm ext_euclid(a,b) function from Wednesday s lecture Returns gcd(a, b), x and y s.t. ax + by = gcd(a, b) Used global variables for x and y Can use pointers to pass back multiple outputs: int ext_euclid(int a, int b, int x, int y); Calling ext_euclid(), pass pointers to variables to receive x and y: i n t x, y, g ; / assume a, b declared p r e v i o u s l y / g = e x t _ e u c l i d ( a, b,&x,& y ) ; Warning about x and y being used before initialized 12

Accessing caller s variables Want to write function to swap two integers Need to modify variables in caller to swap them Pointers to variables as arguments void swap ( i n t x, i n t y ) { i n t temp = x ; x = y ; y = temp ; } Calling swap() function: i n t a = 5, b = 7; swap(&a, &b ) ; / now, a = 7, b = 5 / 13

Variables passing out of scope What is wrong with this code? #include < s t d i o. h> char get_message ( ) { char msg [ ] = "Aren t pointers fun?" ; return msg ; } i n t main ( void ) { char s t r i n g = get_message ( ) ; puts ( s t r i n g ) ; return 0; } 14

Variables passing out of scope What is wrong with this code? #include < s t d i o. h> char get_message ( ) { char msg [ ] = "Aren t pointers fun?" ; return msg ; } i n t main ( void ) { char s t r i n g = get_message ( ) ; puts ( s t r i n g ) ; return 0; } Pointer invalid after variable passes out of scope 14

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 15

Arrays and pointers Primitive arrays implemented in C using pointer to block of contiguous memory Consider array of 8 ints: int arr [8]; Accessing arr using array entry operator: int a = arr [0]; arr is like a pointer to element 0 of the array: int pa = arr; int pa = &arr [0]; Not modifiable/reassignable like a pointer 15

The sizeof() operator For primitive types/variables, size of type in bytes: int s = sizeof(char); / == 1 / double f; / sizeof( f ) == 8 / (64-bit OS) For primitive arrays, size of array in bytes: int arr [8]; / sizeof(arr) == 32 / (64-bit OS) long arr [5]; / sizeof(arr) == 40 / (64-bit OS) Array length: / needs to be on one l i n e when implemented / #define a r r a y _ l e n g t h ( a r r ) ( sizeof ( a r r ) == 0? 0 : sizeof ( a r r ) / sizeof ( ( a r r ) [ 0 ] ) ) More about sizeof() next week... 16

Pointer arithmetic Suppose int pa = arr; Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. Suppose char pc = (char )pa; What value of i satisfies (int )(pc+i) == pa + 3? 17

Pointer arithmetic Suppose int pa = arr; Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. Suppose char pc = (char )pa; What value of i satisfies (int )(pc+i) == pa + 3? i = 12 17

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 18

Strings as arrays Strings stored as null-terminated character arrays (last character == \0 ) Suppose char str [] = "This is a string."; and char pc = str ; Manipulate string as you would an array (pc+10) = S ; puts( str ); / prints "This is a String." / 18

String utility functions String functions in standard header string.h Copy functions: strcpy(), strncpy() char strcpy( strto,strfrom ); copy strfrom to strto char strncpy( strto,strfrom,n); copy n chars from strfrom to strto Comparison functions: strcmp(), strncmp() int strcmp(str1, str2 ); compare str1, str2; return 0 if equal, positive if str1>str2, negative if str1<str2 int strncmp(str1,str2,n); compare first n chars of str1 and str2 String length: strlen() int strlen ( str ); get length of str 19

More string utility functions Concatenation functions: strcat(), strncat() char strcat ( strto,strfrom ); add strfrom to end of strto char strncat( strto,strfrom,n); add n chars from strfrom to end of strto Search functions: strchr(), strrchr() char strchr ( str,c); find char c in str, return pointer to first occurrence, or NULL if not found char strrchr ( str,c); find char c in str, return pointer to last occurrence, or NULL if not found Many other utility functions exist... 20

6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 21

Searching and sorting Basic algorithms Can make good use of pointers Just a few examples; not a course in algorithms Big-O notation 21

Searching an array Suppose we have an array of int s int arr [100]; / array to search / Let s write a simple search function: i n t l i n e a r _ s e a r c h ( i n t v a l ) { i n t parr, parrend = a r r + a r r a y _ l e n g t h ( a r r ) ; for ( parr = a r r ; parr < parrend ; parr ++) { i f ( parr == v a l ) return parr ; } return NULL ; } 22

A simple sort A simple insertion sort: O(n 2 ) iterate through array until an out-of-order element found insert out-of-order element into correct location repeat until end of array reached Split into two functions for ease-of-use i n t a r r [ 1 0 0 ] ; / array to s o r t / void s h i f t _ e l e m e n t ( unsigned i n t i ) { / do i n s e r t i o n of out of order element / } void i n s e r t i o n _ s o r t ( ) { / main i n s e r t i o n s o r t loop / / c a l l s h i f t _ e l e m e n t ( ) f o r each out of order element / } 23

Shifting out-of-order elements Code for shifting the element / move previous elements down u n t i l i n s e r t i o n p o i n t reached / void s h i f t _ e l e m e n t ( unsigned i n t i ) { i n t i v a l u e ; / guard against going outside array / for ( i v a l u e = a r r [ i ] ; i && a r r [ i 1] > i v a l u e ; i ) a r r [ i ] = a r r [ i 1]; / move element down / a r r [ i ] = i v a l u e ; / i n s e r t element / } 24

Insertion sort Main insertion sort loop / i t e r a t e u n t i l out of order element found ; s h i f t the element, and continue i t e r a t i n g / void i n s e r t i o n _ s o r t ( void ) { unsigned i n t i, len = a r r a y _ l e n g t h ( a r r ) ; for ( i = 1; i < len ; i ++) i f ( a r r [ i ] < a r r [ i 1]) s h i f t _ e l e m e n t ( i ) ; } Can you rewrite using pointer arithmetic instead of indexing? 25

Quicksort Many faster sorts available (shellsort, mergesort, quicksort,... ) Quicksort: O(n log n) average; O(n 2 ) worst case choose a pivot element move all elements less than pivot to one side, all elements greater than pivot to other sort sides individually (recursive algorithm) Implemented in C standard library as qsort() in stdlib.h 26

Quicksort implementation Select the pivot; separate the sides: void q u i c k _ s o r t ( unsigned i n t l e f t, unsigned i n t r i g h t ) { unsigned i n t i, mid ; i n t p i v o t ; i f ( l e f t >= r i g h t ) return ; / nothing to s o r t / / p i v o t i s midpoint ; move to l e f t side / swap ( a r r + l e f t, a r r + ( l e f t + r i g h t ) / 2 ) ; p i v o t = a r r [ mid = l e f t ] ; / separate i n t o side < p i v o t ( l e f t +1 to mid ) and side >= p i v o t ( mid+1 to r i g h t ) / for ( i = l e f t +1; i <= r i g h t ; i ++) i f ( a r r [ i ] < p i v o t ) swap ( a r r + ++mid, a r r + i ) ; [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 27

Quicksort implementation Restore the pivot; sort the sides separately: } / r e s t o r e p i v o t p o s i t i o n / swap ( a r r + l e f t, a r r +mid ) ; / s o r t two sides / i f ( mid > l e f t ) q u i c k _ s o r t ( l e f t, mid 1); i f ( mid < r i g h t ) q u i c k _ s o r t ( mid+1, r i g h t ) ; Starting the recursion: quick_sort(0, array_length(arr) 1); [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 28

Discussion of quicksort Not stable (equal-valued elements can get switched) in present form Can sort in-place especially desirable for low-memory environments Choice of pivot influences performance; can use random pivot Divide and conquer algorithm; easily parallelizeable Recursive; in worst case, can cause stack overflow on large array 29

Searching a sorted array Searching an arbitrary list requires visiting half the elements on average Suppose list is sorted; can make use of sorting information: if desired value greater than value and current index, only need to search after index each comparison can split list into two pieces solution: compare against middle of current piece; then new piece guaranteed to be half the size divide and conquer! More searching next week... 30

Binary search Binary search: O(log n) average, worst case: i n t binary_search ( i n t v a l ) { unsigned i n t L = 0, R = a r r a y _ l e n g t h ( a r r ), M; while ( L < R) { M = ( L+R 1)/2; i f ( v a l == a r r [M] ) return a r r +M; / found / else i f ( v a l < a r r [M] ) R = M; / i n f i r s t h a l f / else L = M+1; / i n second h a l f / } return NULL ; / not found / } 31

Binary search Worst case: logarithmic time Requires random access to array memory on sequential data, like hard drive, can be slow seeking back and forth in sequential memory is wasteful better off doing linear search in some cases Implemented in C standard library as bsearch() in stdlib.h 32

Summary Topics covered: Pointers: addresses to memory physical and virtual memory arrays and strings pointer arithmetic Algorithms searching: linear, binary sorting: insertion, quick 33

MIT OpenCourseWare http://ocw.mit.edu 6.087 Practical Programming in C January (IAP) 2010 For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.