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

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

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Memory, Arrays & Pointers

Arrays and Pointers in C. Alan L. Cox

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

ECE 15B COMPUTER ORGANIZATION

Variation of Pointers

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

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

CS 61c: Great Ideas in Computer Architecture

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS 11 C track: lecture 5

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

COMP 2355 Introduction to Systems Programming

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

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

Fundamentals of Programming Session 20

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

[0569] p 0318 garbage

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

... Lecture 12. Pointers

PROGRAMMAZIONE I A.A. 2017/2018

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

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

by Pearson Education, Inc. All Rights Reserved.

Intermediate Programming, Spring 2017*

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

CS 2461: Computer Architecture I

Contents of Lecture 3

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

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

EM108 Software Development for Engineers

Principles of C and Memory Management

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

C++ for Java Programmers

Pointers and Arrays 1

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

Introduction to Scientific Computing and Problem Solving

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Programming in C - Part 2

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Procedural programming with C

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

Lecture 04 Introduction to pointers

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

Fundamental of Programming (C)

Computer Programming Lecture 12 Pointers

Kurt Schmidt. October 30, 2018

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

EL2310 Scientific Programming

Goals of this Lecture

Lectures 5-6: Introduction to C

Pointers, Dynamic Data, and Reference Types

Lecture 2: C Programm

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Pointers. Pointers. Pointers (cont) CS 217

More about BOOLEAN issues

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

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

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Character Strings. String-copy Example

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

Pointers. Pointer References

In Java we have the keyword null, which is the value of an uninitialized reference type

Data Representation and Storage. Some definitions (in C)

CS61C : Machine Structures

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

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

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

High Performance Computing in C and C++

Class Information ANNOUCEMENTS

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?!

Pointers. 10/5/07 Pointers 1

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

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

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!

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Programming in C++ 5. Integral data types

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CS24 Week 2 Lecture 1

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Introduction to C++ with content from

Pointers as Arguments


Transcription:

CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers from today Read Reek, Chapter 6: Pointers CMSC 313 - Wood, Sussman, Herman 2 Chapter 6, Reek POINTERS Pointer example, again The * operator can be used on both the left and right sides of an assignment int i = 6; Memory Address int j; i 4 1084 int *p; j 6 1088 p = &i; p 1084 1092 j = *p; printf("%d %d\n", i, j); *p = 4; printf("%d %d\n", i, j); CMSC 313 - Wood, Sussman, Herman 3 CMSC 313 - Wood, Sussman, Herman 4

Garbage pointers When a pointer is declared, it points to whatever address was in the memory location allocated for the pointer (no initialization) Trying to dereference this random address will generally result in one of three Bad Things: accessing a memory location you don't have permission to access (a "segmentation fault") violating the computer's alignment policies (a "bus error") silent failure: everything appears to work right... for now NULL pointer This is a pointer that points to the address 0, where nothing is allowed to be accessed Defined in stddef.h,, which is included by many other header files Analogue to Java's null What happens when you try to call a method of an object which is null? Very similar thing happens in C when trying to dereference a NULL pointer; it's usually a segfault Just like in Java, you have to check pointers to see if they're NULL before dereferencing them: void f(int *p) { if (p!= NULL) *p = 55; CMSC 313 - Wood, Sussman, Herman 5 CMSC 313 - Wood, Sussman, Herman 6 Pointers to Pointers You can also obtain the address of a pointer variable: int i = 4; Memory Address int j = 6; int *p = &i; i j 4 6 1084 1088 int *q = &j; int **r = &p; p 1088 1092 printf("%d\n", **r); q 1088 1096 *r = &j; r 1092 1100 printf("%d\n", *p); This technique will be useful later when working with pointers as parameters Pointers as parameters You can also pass addresses into a function: void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp;... int x = 2; int y = 3; swap(&x, &y); printf("%d %d\n", x, y); Why do we need to use pointers here? CMSC 313 - Wood, Sussman, Herman 7 CMSC 313 - Wood, Sussman, Herman 8

Structures and pointers We can also have pointers to structures: typedef struct { int number, num_students, start_time; time; Section; void add_students(section d t ti *sec, int students_to_add) { (*sec).num_students += students_to_add;... Section s = {101, 25, 1300; add_to_students(&s, 5); printf("%d\n", " s.num_students); t The -> operator Dereferencing of a pointer to a structure must occur before accessing a field of the structure; due to precedence, parentheses are needed Section s = {101, 25, 1300; Section *sp = &s; *sp.num_students students += 5; /* WRONG */ (*sp).num_students += 5; /* RIGHT */ C has a special operator to make this easier: "(*sp).num_students" is equivalent to "sp->num_students" CMSC 313 - Wood, Sussman, Herman 9 CMSC 313 - Wood, Sussman, Herman 10 Don't forget to check for NULL A common error is to do something like this: assume abs_val() is supposed to return the absolute value of the number pointed to by cp, or return -1 if cp is NULL typedef struct t { double real; double imag; Complex;... double abs_val(complex *cp) { double r = cp->real * cp->real; double i = cp->imag * cp->imag; if (cp == NULL) return -1; return r + i; Remember that the -> is doing dereferencing; you must perform the NULL check before the pointer is dereferenced! d! Generic pointers Pointers to void (void *) can point to any type: void *vp; int a, *ip; double b, *dp; vp = &a; ip = vp; vp = &b; dp = vp; vp = ip; No casts needed with void * pointers CMSC 313 - Wood, Sussman, Herman 11 CMSC 313 - Wood, Sussman, Herman 12

Generic pointers, cont. You can't dereference a void * - you first need to cast or assign it to a real pointer type the value obtained from a dereference depends on the type of pointer NULL is really defined as (void *) 0 These allow use of generic code, but misuse can lead to the kinds of errors we've seen before: void *vp; int *ip; double a = 3.14159; vp = &a; ip = vp; printf("%d\n", " *ip); /* -266631570 */ Type conversion with pointers Converting from one type to a pointer has some uses: unsigned int i; unsigned char *ch; i = 0x543210ab; ch = (unsigned char *) &i; printf( %d\n, *ch); printf( %d\n, * (unsigned char *) &i); Prints out either MSB or LSB of i, depending on architecture CMSC 313 - Wood, Sussman, Herman 13 CMSC 313 - Wood, Sussman, Herman 14 Type conversion, cont. Type conversion is very similar to what happens when we access an inactive union field union { int i; double dbl; a; double fp_val = 3.14159; a.dbl = 3.14159; printf( %d\n, a.i); /* -266631570 */ printf( %d\n, * (int *) &fp_val); Type conversion, cont. The two lines are the same! Although h we stored a double-precision i floating-point number at an address in memory, interpreting ti that t address as a 4-byte integer results in a wildly different value from the floating-point i t number 10010101 01010110 10101010 11010100 00101010 01010100 10101010 10010011 CMSC 313 - Wood, Sussman, Herman 15 CMSC 313 - Wood, Sussman, Herman 16

The const modifier Indicates that a variable can't be changed, and enforced by compiler int const i = 4; const int j = 5; i++; /* ERROR */ j++; /* ERROR */ Order of type specifier and const modifier does matter when dealing with pointers: int i = 4, j = 5; const int *p = &i; /* pointer to constant int */ int * const q = &j; /* constant pointer to int */ p = &j; /* OK */ *p += 5; /* ERROR */ q = &i; /* ERROR */ *q += 23; /* OK */ The program cdecl can be useful for decoding some more complex declarations Incrementing pointers Pointers can be incremented/decremented just like integer type variables, "moving" one element at a time how much is added to the address depends on the size of the type to which the pointer points (as declared) Recall arrays are contiguous memory What does this function do? int mystery(int array[]) { int *p = &(array[0]); int sum = 0; 11 22 1148 1152 while (*p!= -1) { sum += *p; p++; 5-1 1156 1160 return sum; 2394 45346 1164 1168 CMSC 313 - Wood, Sussman, Herman 17 CMSC 313 - Wood, Sussman, Herman 18 Incrementing pointers, cont. size_t strlen(const char *str) { size_t len = 0; while (*str) { len++; str++; return len; Why can we move the str parameter? Why does this return the string'ss length? Incrementing pointers, cont. The postfix operators take precedence over the dereference operator and prefix operators * and prefix ops are at the same precedence level, and associate right to left ++*p increments the value at the location to which p points, and evaluates to the incremented value *p++ evaluates to the value at the location to which p points, and then advances p (*p)++ evaluates to the value at the location to which p points, and then increments that value CMSC 313 - Wood, Sussman, Herman 19 CMSC 313 - Wood, Sussman, Herman 20

Pointer arithmetic With two pointers in the same array, we can determine how far apart they are size_t strlen(const char *str) { const char *ptr; for (ptr = str; *ptr; ptr++) ; return (size_t) (ptr str); h e l l o \0 str ptr Pointer arithmetic, cont. By adding an integer n to a pointer, we can get the address of the n th element past the element to which the pointer currently points int arr[] = {2, 3, 5, 7, 11; int *p = &(arr[0]); int *q = p + 4; printf("%d\n", " *q); Output: 11 Only valid forms of pointer arithmetic: ti pointer - pointer pointer ± integer CMSC 313 - Wood, Sussman, Herman 21 CMSC 313 - Wood, Sussman, Herman 22 Pointer arithmetic, cont. We can also use relational and equality operators when working with multiple pointers void sum_subarray(int array[], int idx1, int idx2) { int *ptr; int sum = 0; ptr = array + idx1; while (ptr <= array + idx2) { sum += *ptr; ptr++; return sum; CMSC 313 - Wood, Sussman, Herman 23