CS/COE 0449 Jarrett Billingsley

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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
Pointers Applications
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Variables, Primitives, and Objects A Visual Learner’s Guide.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Dynamic Allocation in C
Programs – Calling Conventions
CS/COE 0449 (term 2174) Jarrett Billingsley
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CS/COE 0447 (term 2181) Jarrett Billingsley
Hexadecimal, Signed Numbers, and Arbitrariness
CS/COE 0449 (term 2174) Jarrett Billingsley
Motivation and Overview
Memory, Data, & Addressing II CSE 351 Autumn 2017
Functions and the Stack
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
C Programming Tutorial – Part I
Foundations of Programming: Arrays
CS/COE 1541 (term 2174) Jarrett Billingsley
CS/COE 0447 (term 2181) Jarrett Billingsley
COSC 220 Computer Science II
Programs – Dynamic Linking and Loading
Week 7 Part 2 Kyle Dewey.
CS/COE 0449 (term 2174) Jarrett Billingsley
CS/COE 0447 (term 2184) Jarrett Billingsley
Arrays in C.
Programmazione I a.a. 2017/2018.
Introduction to Pointers
Programs – Dynamic Linking and Loading
Introduction to Pointers
References and Pointers
C – Structs, Unions, Enums, Typedefs
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
C – Scope, Lifetime, and the Stack
Object Oriented Programming COP3330 / CGS5409
CS 0449 Jarrett Billingsley
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Programming in C Pointer Basics.
Arrays, Control flow, and Loops
Pointers.
Memory, Data, & Addressing II CSE 351 Winter 2018
More Functions and the Stack
Memory, Data, & Addressing II CSE 351 Summer 2018
Programming in C Pointer Basics.
OS – Memory Deallocation
Memory, Data, & Addressing II CSE 351 Winter 2018
Arrays an array of 5 ints is filled with 3,2,4,1,7
Overloading functions
Lists.
Data Structures and Algorithms Introduction to Pointers
Programming in C Pointer Basics.
Introduction to Pointers
CS/COE 0449 Jarrett Billingsley
Week 7 - Monday CS 121.
CS/COE 0449 Jarrett Billingsley
Presentation transcript:

CS/COE 0449 Jarrett Billingsley C – Pointers and Arrays CS/COE 0449 Jarrett Billingsley

fgets(input, sizeof(input), stdin); // do stuff with input, it's fine, Class announcements project 1 due by this Saturday night! remember I have office hours Fridays 12-2 in 6509 SENSQ oh yeah: when getting input in a loop, you don't have to clear the input buffer char input[100]; while(1) { fgets(input, sizeof(input), stdin); // do stuff with input, it's fine, // you don't have to clear it out or // anything before the next loop } CS449

What are pointers? CS449

Memory! (déjà vu) memory is a big one-dimensional array of bytes every byte value has an address this is its "array index" addresses start at 0, like arrays in C/Java for values bigger than a byte, we use consecutive bytes the address of any value, regardless of size, is the address of the first byte (smallest address) here is an int variable what is its sizeof()? what is its address? what is its value? :^) Addr Val ... DC0B 44 DC0A 4 DC09 2 DC08 88 DC07 BE DC06 EF DC05 C0 DC04 DE DC03 2A DC02 27 DC01 0E DC00 42 DC0D 8B DC0E BB - we often draw memory with the addresses increasing upwards – this will make more sense when we get to the stack - its sizeof() is 4 in this case - 4 bytes long - its address is 0xDC04 - its value could be either 0xDEC0EFBE (big-endian) or 0xC0DEBEEF (little-endian) CS449

5! x 5 Split personalities x variables are not the same thing as the values they hold this variable x holds 5 but x is, itself, "a thing" we can do things with the variable, not just the value it holds it's like a car the car can have a name you can put things in it the car is its own thing though 5 x 5! x CS449

&x x Pointy things a pointer is like the key to the car it's easy to hand the key around it's easy* to copy the key the key isn't the car but it lets you access the car and the things inside it and you can give the key to someone else but be careful who you give them to! &x x * stretching the metaphor a bit, but it's far easier to copy a key than it is to copy a car - you have to be careful when passing pointers around too, since someone might hang onto a copy of a pointer you didn't expect - and then they try to use it - and things go wrong CS449

Pointers in C CS449

You've kinda used pointers in Java... remember writing linked lists? class Link { Link next; int value; } Link list = new Link(); list.next = new Link(); list.next.next = new Link(); next: value: 0 next: value: 0 next: value: 0 what about a reference that doesn't refer to anything? C has null too, but you have to yell it: NULL! - if you don't remember linked lists, welllllllll project 2 is gonna have them :^) CS449

int[] int* int[][] int** Declaring pointers in Java, how do you declare an array of any type X? you put square brackets after the type: X[] int[] int* an array that holds ints. an pointer to an int. int[][] int** an array that holds arrays, and each of those holds ints. a pointer to a pointer, which points to an int. a C pointer can point to either a single value or an array of that type. - the meaning of the pointer – single value or array – depends on context. - really, non-array variables are just arrays of length 1… ;) - often, but not always, you can think of a pointer as pointing to an array of values… and a double-pointer is almost always a 2D array CS449

The address-of operator when used as a prefix operator, & means "address of" it gives you the memory address of any variable, array item, etc. the address is given to you as a pointer type i.e. it "adds a star" I know it seems backwards, why wouldn't they make * add a star, or name pointers int& right? use it on an int? you get an int* use it on an int*? you get an int** YOU GET THE IDEA I hope you can use it on just about anything with a name &x &arr[10] &main (yep!) We'll talk about function pointers at a later date CS449

Pointers are variables too… a variable is a named piece of memory a pointer is a variable that holds a memory address int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; Name Address Value x DC00 0100 y DC04 0200 px DC08 DC00 py DC0C DC04 the addresses of these variables are given to us automatically by the compiler-ish since pointers are variables, can you get their addresses? - "ish" because when we learn about the stack, you'll see that the addresses of local variables can differ depending on where on the stack they are executing - but the compiler gives you the offsets of those variables from the stack pointer. - yes. yes, you can. CS449

Like a locker room, but without the awkward nudity every variable is a locker each locker has a number its address! and each locker has its contents its value! and one locker can hold the number of another locker a pointer! float 3.14 locker 25 - this is a better metaphor than the car, huh - especially since lockers can have keys too… - next time I teach it guess CS449

Accessing the value(s) at a pointer CS449

The value-at (or "dereference") operator * is the value-at operator it dereferences a pointer that is, it accesses the memory that a pointer points to it's the inverse of & every time you use it, you remove a star again, this feels backwards? int** ppx = ... int* px = *ppx; int x = *px; goes to the address that ppx contains, and gets the int* there goes to the address that px contains, and gets the int there - you might think of it like a little breadcrumb… it's the "follow the breadcrumb" operator (look up "Hansel and Gretel") - so an int** is two breadcrumbs that lead you to an int… - idk I thought it was cute CS449

Follow the breadcrumbs (animated) now let's see what happens when we dereference these pointers: int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; *px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value x DC00 0100 y DC04 0200 px DC08 py DC0C 0300 these two lines read the same value from the same location in memory! - since they read the same value, both printfs will print 0x300. - really, a regular variable is a bit like an "automatic pointer"… - the compiler knows the address, and whenever you read from or write to a variable, it changes the value in memory at that address - pointers just give us a way to do it manually, on our own terms CS449

Watch out.. (animated) int x = 0x100; int* px = &x; px = 0x300; What does this program do? int x = 0x100; int* px = &x; px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value x DC00 0100 px DC04 0300 - the second printf WILL CRASH – it will try to go to address 0x0300, which will, in almost any case, be an invalid address - here's an example of a pointer which is neither null nor valid! CS449

Another way of accessing the value at a pointer arrays are just pointers. char buffer[100]; printf("%p\n", buffer); // oh. when you write array[i], what's happening? well, let's use the address-of operator to help us see... when you use the array indexing operator, you're really just adding an offset to the pointer, and using that as the address to access. a[i] is 100% interchangeable with *(a + i). we can do math on pointers… this is called pointer arithmetic. - the %p specifier prints the pointer's value in hex. - see 5_arrays_are_pointers.c CS449

Pointer arithmetic array[3] DC0C 3 array[2] DC08 2 array[1] DC04 1 if we write this: int array[] = {0, 1, 2, 3}; memory looks like this: if we want to access array[2]... what is that equivalent to? *(array + 2) but how big is each item in the array? (what is sizeof(int)?) when we write array + 2, we don't get 0xDC02, we get 0xDC08 it adds the size of 2 items to the address when you add or subtract offsets to pointers, C "scales" the offsets by multiples of the size of the type they point to. Name Address Value array[3] DC0C 3 array[2] DC08 2 array[1] DC04 1 array[0] DC00 - each item is, in this case, 4 bytes long (sizeof(int) == 4) - keep in mind that array indexes and byte addresses are different things!! - this is why C has to know what a pointer points to – so it can do this math correctly. (in addition to catching bugs. (sorta.)) CS449

Oh yeah, and that stupid -> operator if you have a pointer to a struct, you must access its fields with -> Food* pgrapes = &produce[0]; pgrapes->price = 2.99; (*pgrapes).price = 2.99; these are identical in meaning. - go look at 4_food_struct.c again! CS449

Common pointer patterns CS449

Every problem in CS... ...can be solved with another level of indirection/references/pointers. pointers are the basis of: strings arrays object-oriented programming dynamic memory management pretty much everything your operating system does pretty much everything... everything does. higher level languages often give you more abstract, safer ways of achieving the same things that you can do with pointers CS449

Multi-dimensional arrays we already saw single-dimensional arrays, but… int** arr2d = ... … 1 9 4 3 7 6 a Java int[][] works exactly the same way! 2 5 3 9 6 4 3 7 1 2 3 4 5 6 7 - THIS IS ACTUALLY COMPLETELY DIFFERENT FROM AN "int arr[4][6];" OR SOMETHING - see 5_2d_arrays.c for a comparison CS449

function_that_returns_two_values(&x, &y); Pass-by-reference often you want to give another function access to your variables. fgets(buffer, 100, stdin); int x, y; function_that_returns_two_values(&x, &y); since these functions have access to buffer, x, and y, they can change their values. - again, see 4_struct_food.c for a function that takes a Food by reference! CS449