Intro to C: Pointers and Arrays

Similar documents
CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

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

Pointers, Arrays, Memory: AKA the cause of those Segfaults

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

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

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

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

CS61C : Machine Structures

CS61C : Machine Structures

Topics Introduction to Microprocessors

CS61C : Machine Structures

CS61C : Machine Structures

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS61C : Machine Structures

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS61C : Machine Structures

CS61C : Machine Structures

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

CS61C : Machine Structures

CS61C : Machine Structures

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

Number Representation & I See The C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to C (Part I) We are here!

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

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

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

Class Information ANNOUCEMENTS

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

First of all, it is a variable, just like other variables you studied

C Arrays, Strings, More Pointers Instructor: Steven Ho

Arrays and Pointers in C. Alan L. Cox

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS 61c: Great Ideas in Computer Architecture

Memory and C++ Pointers

More C Pointer Dangers

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CSE 307: Principles of Programming Languages

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

EL2310 Scientific Programming

CA31-1K DIS. Pointers. TA: You Lu

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

ECE 15B COMPUTER ORGANIZATION

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Memory, Data, & Addressing II CSE 351 Spring

Lectures 5-6: Introduction to C

REFERENCES, POINTERS AND STRUCTS

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS 415 Midterm Exam Spring 2002

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

TYPES, VALUES AND DECLARATIONS

CS61C : Machine Structures

Memory, Data, & Addressing II

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS31 Discussion 1E Spring 17 : week 08

CS24 Week 2 Lecture 1

Kurt Schmidt. October 30, 2018

Intermediate Programming, Spring 2017*

Data III & Integers I

Computer Systems Principles. C Pointers

CSE 12 Spring 2016 Week One, Lecture Two

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

a data type is Types

Character Strings. String-copy Example

CS3157: Advanced Programming. Announcement

377 Student Guide to C++

CMSC202 Computer Science II for Majors

review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

CS16 Exam #1 7/17/ Minutes 100 Points total

C++ for Java Programmers

Data III & Integers I

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Contents of Lecture 3

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Programming in C - Part 2

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Arrays and Memory Management

Transcription:

Lecture 4 Computer Science 61C Spring 2017 January 25th, 2017 Intro to C: Pointers and Arrays 1

Administrivia Teaching Assistants: Let s try that again. Lectures are recorded. Waitlist/Concurrent Enrollment may have to view recordings. But please assume you are in. My office hours: Monday 11-12, 424 SDH. People with university- related time conflict with lectures should contact the head GSIs. Let head GSIs know about exam conflicts by the end of this week. 2

Agenda Pointers Arrays in C 3

Address vs. Value Consider memory to be a single huge array Each cell of the array has an address associated with it Each cell also stores some value For addresses do we use signed or unsigned numbers? Negative address?! Don t confuse the address referring to a memory location with the value stored there... 101 102 103 104 105...... 23 42 4

Pointers An address refers to a particular memory location; e.g., it points to a memory location Pointer: A variable that contains the address of a variable Location (address)... 101 102 103 104 105...... 23 42 104 x y p name 5

Pointer Syntax int *p; Tells compiler that variable p is address of an int p = &y; Tells compiler to assign address of y to p & called the address operator in this context z = *p; Tells compiler to assign value at address in p to z * called the dereference operator in this context 6

Creating and Using Pointers How to create a pointer: & operator: get address of a variable p? x? Note the * gets used 2 different ways in this example. In the int *p, x; x = 3; p? x 3 p = &x; p x 3 declaration to indicate that p is going to be a pointer, and in the printf to get the value pointed to by p. How get a value pointed to? * (dereference operator): get the value that the pointer points to printf( p points to %d\n,*p); 7

Using Pointer for Writes How to change a variable pointed to? Use the dereference operator * on left of assignment operator = p x 3 *p = 5; p x 5 8

Pointers and Parameter Passing Java and C pass parameters by value : Procedure/function/method gets a copy of the parameter, so changing the copy cannot change the original void add_one (int x) { } x = x + 1; int y = 3; add_one(y); y remains equal to 3 9

Pointers and Parameter Passing How can we get a function to change the value held in a variable? void add_one (int *p) { *p = *p + 1; } int y = 3; add_one(&y); y is now equal to 4 10

Types of Pointers Pointers are used to point to any kind of data (int, char, a struct, etc.) Normally a pointer only points to one type (int, char, a struct, etc.). void * is a type that can point to anything (generic pointer) Use void * sparingly to help avoid program bugs, and security issues, and other bad things! 11

More C Pointer Dangers Declaring a pointer just allocates space to hold the pointer it does not allocate the thing being pointed to! Local variables in C are not initialized, they may contain anything (aka garbage ) What does the following code do? void f() { int *ptr; *ptr = 5; } 12

Pointers and Structures typedef struct { int x; int y; /* dot notation */ int h = p1.x; p2.y = p1.y; } Point; /* arrow notation */ Point p1; Point p2; int h = paddr->x; int h = (*paddr).x; Point *paddr; /* This works too */ p1 = p2; 13

Pointers in C Why use pointers? If we want to pass a large struct or array, it s easier / faster / etc. to pass a pointer than the whole thing In general, pointers allow cleaner, more compact code So what are the drawbacks? Pointers are probably the single largest source of bugs in C, so be careful anytime you deal with them Most problematic with dynamic memory management coming up next week Dangling references and memory leaks 14

Why Pointers in C? At time C was invented (early 1970s), compilers often didn t produce efficient code Computers 25,000 times faster today, compilers better C designed to let programmer say what they want code to do without compiler getting in way Even give compilers hints which registers to use! Today s compilers produce much better code, so may not need to use pointers in application code Low-level system code still needs low-level access via pointers 15

Video: Fun with Pointers https://www.youtube.com/watch?v=6pmwojism_e 16

Peer Instruction Time void foo(int *x, int *y) { int t; if ( *x > *y ) { t = *y; *y = *x; *x = t; } } int a=3, b=2, c=1; foo(&a, &b); foo(&b, &c); foo(&a, &b); printf("a=%d b=%d c=%d\n", a, b, c); Result is: A: a=3 b=2 c=1 B: a=1 b=2 c=3 C: a=1 b=3 c=2 D: a=3 b=3 c=3 E: a=1 b=1 c=1 17