Instructor: Justin Hsia 6/25/2013Summer 2013 -- Lecture #21 CS 61C: Great Ideas in Computer Architecture Introduction to C, Pointers.

Slides:



Advertisements
Similar presentations
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS61C L2 Number Representation & Introduction to C (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
CS 61C L2 Introduction to C (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 2: Introduction To C
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Guide To UNIX Using Linux Third Edition
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C L2 Number Representation & Introduction to C (1) Beamer, Summer 2007 © UCB Scott Beamer Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2014 © UCB Senior Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Imperative Programming
CIS Computer Programming Logic
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
DAT602 Database Application Development Lecture 5 JAVA Review.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
IT 251 Computer Organization and Architecture Introduction to the C Programming Language Part 1 Chia-Chi Teng.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Introduction to Programming
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
CPS120: Introduction to Computer Science Variables and Constants.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Instructor: Justin Hsia 6/20/2012Summer Lecture #31 CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Computer Organization and Design Pointers, Arrays and Strings in C
The Machine Model Memory
CS 61C: Great Ideas in Computer Architecture Introduction to C
C Basics.
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Instructor Paul Pearce
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Instructor: Justin Hsia 6/25/2013Summer Lecture #21 CS 61C: Great Ideas in Computer Architecture Introduction to C, Pointers

Six Great Ideas in Computer Architecture Number Representation – Bits can represent anything! – n bits can represent up to 2 n things – Unsigned, biased, 1’s, 2’s – Overflow – Sign extension: same number using more bits From Discussion: Powers of 2 Review of Last Lecture 6/25/2013Summer Lecture #22

Question: Take the 4-bit number x = 0b1010. Which of the following numbers does x NOT represent in the schemes discussed last lecture? unsigned sign and magnitude biased notation one’s complement two’s complement -4 (A) -6 (B) 10 (C) (D) 3

Great Idea #1: Levels of Representation/Interpretation 6/25/2013Summer Lecture #24 lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) Higher-Level Language Program (e.g. C) Assembly Language Program (e.g. MIPS) Machine Language Program (MIPS) Hardware Architecture Description (e.g. block diagrams) Compiler Assembler Machine Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; Logic Circuit Description (Circuit Schematic Diagrams) Architecture Implementation We are here_

Agenda Basic C Concepts – Compilation – Variable Types Administrivia C Syntax and Control Flow Pointers – Address vs. Value 6/25/2013Summer Lecture #25

Experience with C Official prerequisites: “Some” C experience is required before CS61C – C++ or JAVA okay Average CS61C class: ≈ 9/10 already know JAVA ≈ 1/2 already know C++ ≈ 1/3 already know C ≈ 1/10 already know C# ≈ 1/20 have not taken 61B or equivalent If you have no experience in these languages, then start early and ask a lot of questions in discussion! 6/25/2013Summer Lecture #26

Disclaimer You will not learn how to fully code in C in these lectures, so make use of C references! – K&R is THE resource – “JAVA in a Nutshell” by David Flanagan – Brian Harvey’s notes (on course website) NotesC1-3.pdf NotesC1-3.pdf – Other online sources 6/25/2013Summer Lecture #27

Introducing C C is not a “very high level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. – Kernighan and Ritchie With C we can write programs that allow us to exploit underlying features of the architecture 6/25/2013Summer Lecture #28

C Concepts These concepts distinguish C from other programming languages that you may know: CompilerCreates useable programs from C source code Typed variablesMust declare the kind of data the variable will contain Typed functionsMust declare the kind of data returned from the function Header files (.h )Allows you to declare functions and variables in separate files StructsGroups of related values EnumsLists of predefined values PointersAliases to other variables 6/25/2013Summer Lecture #29

Compilation Overview C is a compiled language C compilers map C programs into architecture- specific machine code (string of 0s and 1s) – Unlike Java, which converts to architecture- independent bytecode (run by JVM) – Unlike most Scheme environments, which directly interpret the code – These differ mainly in exactly when your program is mapped to low-level machine instructions 6/25/2013Summer Lecture #210

Compilation Advantages Excellent run-time performance: Generally much faster than Scheme or Java for comparable code because it optimizes for the given architecture Fair compilation time: enhancements in compilation procedure ( Makefile s) allow us to recompile only the modified files 6/25/2013Summer Lecture #211

Compilation Disadvantages Compiled files, including the executable, are architecture-specific (CPU type and OS) – Executable must be rebuilt on each new system – i.e. “porting your code” to a new architecture “Change  Compile  Run [repeat]” iteration cycle can be slow 6/25/2013Summer Lecture #212

Typed Variables in C int x = 2; float y = 1.618; char z = 'A'; TypeDescriptionExamples int signed integer5,-12,0 short int ( short )smaller signed integer long int ( long )larger signed integer char single text character or symbol'a', 'D', '?’ float floating point non-integer numbers0.0, 1.618, -1.4 double greater precision FP number Integer sizes are machine dependant! – Common size is 4 or 8 bytes (32/64-bit), but can’t ever assume this Can add “unsigned” before int or char 6/25/2013Summer Lecture #213 You must declare the type of data a variable will hold Declaration must come before or simultaneously with assignment declaration assignment

sizeof() If integer sizes are machine dependent, how do we tell? Use sizeof() function – Returns size in bytes of variable or data type name Examples: int x; sizeof(x); sizeof(int); Acts differently with arrays and structs, which we will cover later – Arrays: returns size of whole array – Structs: returns size of one instance of struct (sum of sizes of all struct variables + padding) 6/25/2013Summer Lecture #214

Characters Encode characters as numbers, same as everything! ASCII standard defines 128 different characters and their numeric encodings ( – char representing the character ‘a’ contains the value 97 – char c = ‘a’; or char c = 97; are both valid A char takes up 1 byte of space – 7 bits is enough to store a char (2 7 = 128), but we add a bit to round up to 1 byte since computers usually deal with multiples of bytes 6/25/2013Summer Lecture #215

Typecasting in C (1/2) C is a “weakly” typed language – You can explicitly typecast from any type to any other: int i = -1; if(i < 0) printf(“This will print\n”); if((unsigned int)i < 0) printf(“This will not print\n”); This is possible because everything is stored as bits! – Can be seen as changing the “programmer’s perspective” of the variable 6/25/2013Summer Lecture #216

Typecasting in C (2/2) C is a “weakly” typed language – You can explicitly typecast from any type to any other: int i = -1; if(i < 0) printf(“This will print\n”); if((unsigned int)i < 0) printf(“This will not print\n”); Can typecast anything, even if it doesn’t make sense: struct node n; /* structs in a few slides */ int i = (int) n; – More freedom, but easier to shoot yourself in the foot 6/25/2013Summer Lecture #217

Typed Functions in C // function prototypes int my_func(int,int); void sayHello(); // function definitions int my_func(int x,int y) { sayHello(); return x*y; } void sayHello() { printf(“Hello\n”); } You have to declare the type of data you plan to return from a function Return type can be any C variable type or void for no return value – Place on the left of function name Also necessary to define types for function arguments Declaring the “prototype” of a function allows you to use it before the function’s definition 6/25/2013Summer Lecture #218 int my_func(int,int); int my_func(int x,int y) int void int void

Structs in C typedef struct { int lengthInSeconds; int yearRecorded; } Song; Song song1; song1.lengthInSeconds = 213; song1.yearRecorded = 1994; Song song2; song2.lengthInSeconds = 248; song2.yearRecorded = 1988; 6/25/2013Summer Lecture #219 Way of defining compound data types A structured group of variables, possibly including other struct s

CJava Type of Language Function OrientedObject Oriented Program- ming Unit FunctionClass = Abstract Data Type Compilation Creates machine-dependent code Creates machine-independent bytecode ExecutionLoads and executes programJVM interprets bytecode Hello World #include int main(void) { printf("Hello\n"); return 0; } public class HelloWorld { public static void main(String[] args) { System.out.printl("Hello"); } } Memory manage- ment Manual ( malloc, free )Automatic (garbage collection) 6/25/2013Summer Lecture #220 From

Agenda Basic C Concepts – Compilation – Variable Types Administrivia C Syntax and Control Flow Pointers – Address vs. Value 6/25/2013Summer Lecture #221

Code on Piazza Most questions on Piazza can be asked without posting code Show only relevant lines of code – Posting an entire function is BAD Lecture examples, provided code, & output/error messages OK Why do we care? – Giving away answers prevents learning – If copied, you are culpable as the enabler – We are keeping track; could affect your P&A grade 7/09/2013Summer Lecture #922

Administrivia Lab 1 is today – Get class account and register – Find partner for labs HW0 is due tonight – Need lab account to submit Start HW1 (due Sunday) Don’t forget about office hours! 6/25/2013Summer Lecture #223

Agenda Basic C Concepts – Compilation – Variable Types Administrivia C Syntax and Control Flow Pointers – Address vs. Value 6/25/2013Summer Lecture #224

C and Java operators nearly identical arithmetic: +, -, *, /, % assignment: = augmented assignment: +=, -=, *=, /=, %=, &=, |=, ^=, >= bitwise logic: ~, &, |, ^ bitwise shifts: > boolean logic: !, &&, || equality testing: ==, != subexpression grouping: ( ) order relations:, >= increment and decrement: ++ and -- member selection:., -> conditional evaluation: ? : 6/25/2013Summer Lecture #225 For precedence/order of execution, see Table 2-1 on p. 53 of K&R

Generic C Program Layout #include #include “local_files” #define macro_name macro_expr /* declare functions */ /* declare external variables and structs */ int main(int argc, char *argv[]) { /* the innards */ } /* define other functions */ 6/25/2013Summer Lecture #226 Dumps other files here (.h and.o )__  Macro substitutions Handled by Preprocessor Remember rules of scope! (internal vs. external) Programs start at main() main() must return int

Sample C Code #include #define REPEAT 5 int main(int argc, char *argv[]) { int i; int n = 5; for (i = 0; i < REPEAT; i = i + 1) { printf("hello, world\n"); } return 0; } 6/25/2013Summer Lecture #227

C Syntax: main To get arguments to the main function, use: – int main(int argc, char *argv[]) What does this mean? – argc contains the number of strings on the command line (the executable name counts as one, plus one for each argument). – argv is an array containing pointers to the arguments as strings (more on pointers later) 6/25/2013Summer Lecture #228

main Example % foo hello 87 Here argc = 3 and the array argv contains pointers to the following strings: argv[0] = "foo" argv[1] = "hello" argv[2] = "87" We will cover pointers and strings later 6/25/2013Summer Lecture #229

C Syntax: Variable Declarations All variable declarations must appear before they are used (e.g. at the beginning of a block of code) A variable may be initialized in its declaration; if not, it holds garbage! Variables of the same type may be declared on the same line Examples of declarations: – Correct: int x; int a, b=10, c; − Incorrect: for(int i=0; i<10; i++); short x=1, y=1.0; 6/25/2013Summer Lecture #230

C Syntax: True or False No explicit Boolean type in C (unlike Java) What evaluates to FALSE in C? – 0 (integer) – NULL (a special kind of pointer: more on this later) What evaluates to TRUE in C? – Anything that isn’t false is true – Same idea as in Scheme: only #f is false, anything else is true! 6/25/2013Summer Lecture #231

C Syntax: Control Flow Should be similar to what you’ve seen before – if-else if (expression) statement if (expression) statement1 else statement2 – while while (expression) statement do statement while (expression); 6/25/2013Summer Lecture #232

C Syntax: Control Flow Should be similar to what you’ve seen before – for for (initialize; check; update) statement – switch switch (expression) { case const1: statements case const2: statements default: statements } break 6/25/2013Summer Lecture #233

switch and break Case statement ( switch ) requires proper placement of break to work properly – “Fall through” effect: will execute all cases until a break is found – In certain cases, can take advantage of this! 6/25/2013Summer Lecture #234 switch(ch){ case ‘+’: … /* does + and - */ case ‘-’: … break; case ‘*’: … break; default: … }

Has there been an update to ANSI C? Yes! It’s called the “C99” or “C9x” std – Use option “ gcc -std=c99 ” at compilation References Highlights: – Declarations in for loops, like Java (#15) – Java-like // comments (to end of line) (#10) – Variable-length non-global arrays (#33) – for explicit integer types (#38) – for boolean logic definitions (#35) 6/25/2013Summer Lecture #235

Get To Know Your Staff Category: Cal 6/25/2013Summer Lecture #236

Agenda Basic C Concepts – Compilation – Variable Types Administrivia C Syntax and Control Flow Pointers – Address vs. Value 6/25/2013Summer Lecture #237

Address vs. Value Consider memory to be a single huge array – Each cell/entry of the array has an address – Each cell also stores some value Don’t confuse the address referring to a memory location with the value stored there 6/25/2013Summer Lecture #

Pointers A pointer is a variable that contains an address – An address refers to a particular memory location, usually also associated with a variable name – Name comes from the fact that you can say that it points to a memory location 6/25/2013Summer Lecture #239 p x y Location (address) Variable names Data

Pointer Syntax int *x; – Declare variable x the address of an int x = &y; – Assigns address of y to x – & called the “address operator” in this context z = *x; – Assigns the value at address in x to z – * called the “dereference operator” in this context 6/25/2013Summer Lecture #240

Pointer Example int *p,x,y; x=3; y=4; p = &x; *p = 5; y = *p; Declare Assign vals Assign ref Dereference (1) Dereference (2) 6/25/2013Summer Lecture #241 p?x?y? p?x3y4 px3y4px5y4px5y5

Pointer Types (1/2) Pointers are used to point to one kind of data ( int, char, a struct, etc.) – Pointers to pointers? Oh yes! (e.g. int **pp ) Exception is the type void *, which can point to anything (generic pointer) – Use sparingly to help avoid program bugs and other bad things! 6/25/2013Summer Lecture #242

Pointer Types (2/2) Functions can return pointers char *foo(char data) { return &data; } Placement of * does not matter to compiler, but might to you – int* x is the same as int *x – int *x,y,z; is the same as int* x,y,z; but NOT the same as int *x,*y,*z; 6/25/2013Summer Lecture #243

Pointers and Parameter Passing Java and C pass parameters “by value” – Procedure/function/method gets a copy of the parameter, so changing the copy does not change the original Function: void addOne (int x) { x = x + 1; } Code: int y = 3; addOne(y); 6/25/2013Summer Lecture #244 y remains equal to 3

Pointers and Parameter Passing How do we get a function to change a value? – Pass “by reference”: function accepts a pointer and then modifies value by dereferencing it Function: void addOne (int *p) { *p = *p + 1; } Code: int y = 3; addOne(&y); 6/25/2013Summer Lecture #245 y is now equal to 4

Pointers in C Why use pointers? – When passing a large struct or array, it’s easier/faster to pass a pointer than a copy of the whole thing – In general, pointers allow cleaner, more compact code Careful: Pointers are likely the single largest source of bugs in C – Most problematic with dynamic memory management, which we will cover later – Dangling references and memory leaks 6/25/2013Summer Lecture #246

Pointer Bugs Local variables in C are not initialized, they may contain anything (a.k.a. “garbage”) Declaring a pointer just allocates space to hold the pointer – it does not allocate the thing being pointed to! 6/25/2013Summer Lecture #247 void f2() { int *ptr; *ptr = 5; } void f() { int *p,x; x = *p; } BAD

#include void flip-sign(int *n){ *n = -(*n);} void main(); { int *p, x=5, y; // init y = *(p = &x) + 1; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n", x, y,*p); } 48 Question: How many errors (syntax and logic) in this C code (assume C99)? 2 (A) 3 (B) 4 (C) (D)

#include (1) void flip-sign(int *n){ *n = -(*n);} void main(); { int *p, x=5, y; // init y = *(p = &x) + 1; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n", x, y,*p); } 49 Answer: How many errors (syntax and logic) in this C code (assume C99)? (4) (5) (2) 2 (A) 3 (B) 4 (C) (D) * (6) ; (3)

#include void flip_sign(int *n){*n = -(*n);} int main() { int *p, x=5, y; // init y = *(p = &x) + 1; int z; flip_sign(p); printf("x=%d,y=%d,*p=%d\n",x,y,*p); } 50 Question: What is output from the corrected code below? 5, 6, -5 (A) -5, 6, -5 (B) -5, 4, -5 (C) (D) x, y, *p

Summary C is an efficient (compiled) language, but leaves safety to the programmer – Weak type safety, variables not auto-initialized – Use pointers with care: common source of bugs! Pointer is a C version (abstraction) of a data address – Each memory location has an address and a value stored in it – * “follows” a pointer to its value – & gets the address of a value C functions “pass by value” 6/25/2013Summer Lecture #251