A Short Course for REU Students Summer Instructor: Ben Ransford

Similar documents
A Short Course for REU Students Summer Instructor: Ben Ransford

Dynamic memory allocation

Dynamic memory allocation (malloc)

Final CSE 131B Spring 2004

PRINCIPLES OF OPERATING SYSTEMS

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Common Misunderstandings from Exam 1 Material

C programming basics T3-1 -

C LANGUAGE A Short Course

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Introduction to C. CS2023 Winter 2004

Understanding Pointers

Language comparison. C has pointers. Java has references. C++ has pointers and references

Lecture 2, September 4

ECE264 Fall 2013 Exam 1, September 24, 2013

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Array Initialization

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

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 {

Pointers. Pointer References

ECE264 Fall 2013 Exam 3, November 20, 2013

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Section 2: Processes

CS 61c: Great Ideas in Computer Architecture

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

ECE264 Summer 2013 Exam 1, June 20, 2013

File Access. FILE * fopen(const char *name, const char * mode);

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Fundamental of Programming (C)

Final CSE 131B Spring 2005

CS 11 C track: lecture 5


Lecture 03 Bits, Bytes and Data Types

Lecture 07 Debugging Programs with GDB

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

25.2 Opening and Closing a File

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

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

Dynamic Memory Allocation

Memory, Arrays & Pointers

211: Computer Architecture Summer 2016

Kurt Schmidt. October 30, 2018

CSE 333 Midterm Exam 2/12/16. Name UW ID#

Dynamic Data Structures. CSCI 112: Programming in C

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers. CSE 2031 Fall November 11, 2013

LSN 4 GUI Programming Using The WIN32 API

CS 61c: Great Ideas in Computer Architecture

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

CSE 303 Midterm Exam

CS240: Programming in C

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Pointers and File Handling

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

High Performance Programming Programming in C part 1

CSE 303 Winter 2008 Midterm Key

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

ECE264 Fall 2013 Exam 2, October 24, 2013

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Contents. A Review of C language. Visual C Visual C++ 6.0

C for C++ Programmers

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Processes. Johan Montelius KTH

Arrays and Pointers (part 2) Be extra careful with pointers!

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CS C Primer. Tyler Szepesi. January 16, 2013

primitive arrays v. vectors (1)

Programming. Pointers, Multi-dimensional Arrays and Memory Management

A process. the stack

Arrays and Pointers (part 2) Be extra careful with pointers!

Approximately a Test II CPSC 206

CSE 124 Discussion (10/3) C/C++ Basics

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

CS61, Fall 2012 Section 2 Notes

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

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

ECEN 449 Microprocessor System Design. Review of C Programming

C programming for beginners

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

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

just a ((somewhat) safer) dialect.

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

CSE 333 Lecture 7 - final C details

CSE 12 Spring 2016 Week One, Lecture Two

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

CS 61C: Great Ideas in Computer Architecture Introduction to C

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CS 237 Meeting 19 10/24/12

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

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

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

Transcription:

C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1

Outline Last time: basic syntax, compilation This time: pointers, libraries, I/O,... Goal: soundness, not completeness http://www.cs.umass.edu/~ransford/reuc/ 2

Pointers There s nothing to fear. int i = 12345; int* location_of_i = &i; 3

Pointers: You already know them. 1: public class Hello { 2: private int value; 3: public void setvalue (int val) { this.value = val; } 4: public int getvalue () { return this.value; } 5: Hello (int val) { this.setvalue(val); } 6: } 1: public class foo { 2: public static void main(string[] args) { 3: Hello a = new Hello(5); 4: Hello b = a; 5: b.setvalue(10); 6: 7: System.out.println(a.getValue()); 8: System.out.println(b.getValue()); 9: System.exit(0); 10: } 11: } 4

Pointers: You already know them. 1: public class Hello { 2: private int value; 3: public void setvalue (int val) { this.value = val; } 4: public int getvalue () { return this.value; } 5: Hello (int val) { this.setvalue(val); } 6: } 1: public class foo { 2: public static void main(string[] args) { 3: Hello a = new Hello(5); 4: Hello b = a; 5: b.setvalue(10); 6: 7: System.out.println(a.getValue()); 8: System.out.println(b.getValue()); 9: System.exit(0); 10: } 11: } 4

Pointers in C: syntax int i = 12345; 5

Pointers in C: syntax int i = 12345; This code allocates enough space on the stack for one integer. 5

Pointers in C: syntax int i = 12345; This code allocates enough space on the stack for one integer. foo 999 i 12345 sizeof(int) [4 bytes]...... 5

int i = 12345; On the stack, in memory: 0x0000 foo 999 0x0004 i 12345 0x0008...... Memory locations (addresses) 6

int i = 12345; On the stack, in memory: 0x0000 foo 999 0x0004 i 12345 0x0008...... Memory locations (addresses) We say: &i == 0x0004 6

Pointers are types int i; int* pointer_to_i; pointer_to_i will hold the memory address of an int. 7

Pointers are types int i; int* pointer_to_i; pointer_to_i will hold the memory address of an int. Like this: pointer_to_i = &i; 7

Pointers are references int i; int* addr = &i; Referencing: addr stores a reference to i. Dereferencing: *addr is the thing whose address is in addr (namely i) i = 5; *addr = 5; 8

Pointers are references int i; int* addr = &i; Referencing: addr stores a reference to i. Dereferencing: *addr is the thing whose address is in addr (namely i) i = 5; *addr = 5; Equivalent! 8

Example 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: int i = 12345; 5: int* location_of_i = &i; 6: 7: printf("%d\n", i); 8: printf("%d\n", *location_of_i); 9: 10: return 0; 11: } 9

Example 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: int i = 12345; 5: int* location_of_i = &i; 6: 7: printf("%d\n", i); 8: printf("%d\n", *location_of_i); 9: 10: return 0; 11: } 9

Pointers: why we care Hint: argument passing. 10

Pointers: why we care Hint: argument passing. Call-by-value semantics: function gets a copy of whatever information you pass it. No side effects. Call-by-reference semantics: function gets a reference to the real object you want it to manipulate. 10

Argument passing example 1: #include <stdio.h> 2: 3: void triple (int* i) { 4: *i = *i * 3; 5: } 6: 7: int main (int argc, char** argv) { 8: int x = 11111; 9: printf("%d\n", x); 10: triple(&x); 11: printf("%d\n", x); 12: } 11

Argument passing example 1: #include <stdio.h> 2: 3: void triple (int* i) { 4: *i = *i * 3; 5: } 6: 7: int main (int argc, char** argv) { 8: int x = 11111; 9: printf("%d\n", x); 10: triple(&x); 11: printf("%d\n", x); 12: } 11

Example: time Goal: print 1:30 12

String is char* There is no built-in string type. Use char*. 1: #include <stdio.h> 2: #include <stdlib.h> 3: #include <string.h> 4: 5: int main (int argc, char** argv) { 6: char* foo = "This is foo."; 7: char* bar = (char*) malloc(100); 8: char* foobar; 9: bar[0] = B ; 10: bar[1] = A ; 11: bar[2] = R ; 12: bar[3] = \0 ; // string functions work on null-terminated strings 13: 14: printf("%s\n%d\n", foo, strlen(foo)); 15: printf("%s\n%d\n", bar, strlen(bar)); 16: 17: foobar = (char*) malloc(strlen(foo) + strlen(bar) + 1); 18: strcat(foobar, foo); 19: strcat(foobar, bar); 20: printf("%s\n%d\n", foobar, strlen(foobar)); 21: } 13

String is char* There is no built-in string type. Use char*. 1: #include <stdio.h> 2: #include <stdlib.h> 3: #include <string.h> 4: 5: int main (int argc, char** argv) { 6: char* foo = "This is foo."; 7: char* bar = (char*) malloc(100); 8: char* foobar; 9: bar[0] = B ; 10: bar[1] = A ; 11: bar[2] = R ; 12: bar[3] = \0 ; // string functions work on null-terminated strings 13: 14: printf("%s\n%d\n", foo, strlen(foo)); 15: printf("%s\n%d\n", bar, strlen(bar)); 16: 17: foobar = (char*) malloc(strlen(foo) + strlen(bar) + 1); 18: strcat(foobar, foo); 19: strcat(foobar, bar); 20: printf("%s\n%d\n", foobar, strlen(foobar)); 21: } 13

Arrays are pointers char c[5]; c[0] = A ; c[2] = C ; Is equivalent to *c = A ; *(c+2) = C ; 14

Pointer math Avoid this when possible. Error-prone. int arr[3]; *arr = 12345; arr++; *arr = 45678; arr^ arr^??? 12345?? 12345?? arr^ 12345 45678? arr^ 15

Libraries A library is precompiled code you can use. Libraries come with headers (.h files) so you know what s in them. Most nontrivial software uses libraries. E.g.: every Windows program uses Windows header files and libraries. 16

Library example Hello world for Windows: 1: #include <windows.h> // Windows header file 2: 3: int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hprevinstance, 4: LPSTR lpcmdline, int ncmdshow) { 5: MessageBox (NULL, "Hello world!", "Title Bar Text", MB_OK); 6: return 0; 7: } 8: Header file (windows.h) + library (some DLL). 17

Linking To use a library, you link your program to it. Static linking (compile time): incorporate libraries code into your executable. Dynamic linking At compile time, store a placeholder OS must find appropriate library at runtime 18

Linking strategies compared Statically linked executables are: Larger Self-contained (independent of environment) Dynamically linked executables: Are smaller Promote code reuse (shared libraries) 19

Library example X11 is a UNIX graphics system. To write an X11 program, you use libx11. 1: #include <X11/Xlib.h> 2: #include <stdio.h> 3: 4: int main (int argc, char** argv) { 5: Display* d; 6: d = XOpenDisplay(":0.0"); 7: printf("hello!\n"); 8: XCloseDisplay(d); 9: } 20

File I/O Write to a file: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: if (argc!= 2) { 5: fprintf(stderr, "Usage: %s file\n", argv[0]); 6: return 1; 7: } 8: 9: FILE* f = fopen(argv[1], "w"); 10: fprintf(f, "Hello, I am %s\n", argv[1]); 11: fclose(f); 12: } 21

File I/O Write to a file: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: if (argc!= 2) { 5: fprintf(stderr, "Usage: %s file\n", argv[0]); 6: return 1; 7: } 8: 9: FILE* f = fopen(argv[1], "w"); 10: fprintf(f, "Hello, I am %s\n", argv[1]); 11: fclose(f); 12: } 21

File I/O Read from a file (and print its contents): 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: if (argc!= 2) { 5: fprintf(stderr, "Usage: %s file\n", argv[0]); 6: return 1; 7: } 8: 9: FILE* f = fopen(argv[1], "r"); 10: char c; 11: while ((c = fgetc(f))!= EOF) { 12: putchar(c); 13: } 14: fclose(f); 15: } 22

File I/O Read from a file (and print its contents): 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: if (argc!= 2) { 5: fprintf(stderr, "Usage: %s file\n", argv[0]); 6: return 1; 7: } 8: 9: FILE* f = fopen(argv[1], "r"); 10: char c; 11: while ((c = fgetc(f))!= EOF) { 12: putchar(c); 13: } 14: fclose(f); 15: } 22

That s it. C is small C is everywhere You know some C now 23

What to do now Practice writing some C programs! Read some C source code -- wherever you can find it. Find and read K&R. 24