Systems Programming and Computer Architecture ( )

Similar documents
CMPSC 311- Introduction to Systems Programming Module: Arrays and Pointers

Pointers, Pointers, Pointers

CSE 333 Lecture 3 - arrays, memory, pointers

Pointers, Pointers, Pointers

CSE 333 Lecture 2 - arrays, memory, pointers

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Stack Discipline Jan. 19, 2018

Intermediate Programming, Spring 2017*

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

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

Systems I. Machine-Level Programming V: Procedures

CS 61c: Great Ideas in Computer Architecture

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

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

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

Arrays and Pointers in C. Alan L. Cox

SYSC 2006 C Winter 2012

Lecture 04 Introduction to pointers

Course organization. Course introduction ( Week 1)

Understanding Pointers

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

Systems Programming and Computer Architecture ( )

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

C - Func(on, Pointer, Array. Zhaoguo Wang


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

Kurt Schmidt. October 30, 2018

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

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 {

A brief introduction to C programming for Java programmers

Memory, Arrays & Pointers

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

CS C Primer. Tyler Szepesi. January 16, 2013

CS 11 C track: lecture 5

CS61C : Machine Structures

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Procedural programming with C

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

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

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

Lecture 2: C Programm

PROGRAMMAZIONE I A.A. 2017/2018

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Processes. Johan Montelius KTH

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

A process. the stack

System Software Assignment 1 Runtime Support for Procedures

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

CSE 303: Concepts and Tools for Software Development

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

ECE 15B COMPUTER ORGANIZATION

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

EM108 Software Development for Engineers

CSE 333 Lecture 2 Memory

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

Fundamentals of Programming

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS61, Fall 2012 Section 2 Notes

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

Dynamic Allocation in C

Dynamic Memory Allocation. Zhaoguo Wang

CSE 333 Lecture 4 - malloc, free, struct, typedef

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

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

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

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

ELEC 377 C Programming Tutorial. ELEC Operating Systems

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

Informatica e Sistemi in Tempo Reale

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department

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

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

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Lecture 07 Debugging Programs with GDB

FUNCTIONS POINTERS. Pointers. Functions

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers. Pointers. Pointers (cont) CS 217

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

Arrays and Pointers (part 1)

Fundamental of Programming (C)

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

Lecture 3: C Programm

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

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

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

Decimal Representation

Lab 1: Introduction to C Programming

Pointers. Pointer References

Lecture 03 Bits, Bytes and Data Types

Exercise Session 2 Systems Programming and Computer Architecture

CSE 333. Lecture 4 - malloc, free, struct, typedef. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Exercise Session 3 Systems Programming and Computer Architecture

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

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

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

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Transcription:

Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1

4: Pointers Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 2

Process address space 0xffffffffffffffff OS gives each process an address space Contains process virtual memory, visible only to it 2 32 bytes on 32 bit host 2 64 bytes on 64 bit host (shown) code, data, libraries, stack, etc. AS 2016 3 0x0000000000000000

When the OS loads a program, it: creates an address space inspects the executable file to see what s in it (lazily) copies regions of the file into the right place in the address space does any final linking, relocation, or other needed preparation Loading 0xffffffff 0xc0000000 0x40000000 0x08048000 Kernel virtual memory User stack (created at runtime) Memory-mapped region for shared libraries Run-time heap (created by malloc) Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused Memory inaccessible to user code Stack pointer brk Loaded from the executable file AS 2016 4 0x00000000

4.1: Recap: the stack Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 5

Stack-Based Languages Languages supporting recursion (C, C#, Eiffel, Java..) Code must be reentrant Multiple simultaneous instantiations of single procedure Need to store state of each instantiation Arguments, local variables, return pointer Stack discipline State for given procedure needed for limited time From when called to when returns Callee returns before caller does Stack allocated in Frames state for single procedure activation 6

Call Chain Example yoo( ) who(); who( ) ami(); ami(); ami( ) ami(); Example call chain yoo who ami ami ami ami These are all different activations Procedure ami is recursive AS 2016 7

Stack Frames Contents Local variables Return information Temporary space Previous Frame Management Space allocated when enter procedure Set-up code Deallocated when return Finish code Frame Pointer: Stack Pointer: Frame for proc Stack Top 8

Example Stack who( ) ami(); ami(); yoo who ami ami ami Frame Stack yoo who ami 9

Example Stack ami( ) ami(); yoo who ami ami ami yoo who ami ami ami Frame ami AS 2016 10 Stack

Example Stack ami( ) yoo who ami ami ami Frame yoo who ami ami Stack 11

Example Stack you( ) who(); yoo who ami ami ami Frame Stack yoo ami 12

4.2: Pointers in C Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 13

Addresses and & &x produces the virtual address where the value of x is stored. You can use %p in printf() to print it out. addresses.c #include <stdio.h> int foo(int x) return x+1; int main(int argc, char *argv[]) int x, y; int a[2]; printf("x is at %p\n", &x); printf("y is at %p\n", &y); printf("a[0] is at %p\n", &a[0]); printf("a[1] is at %p\n", &a[1]); printf("foo is at %p\n", &foo); printf("main is at %p\n", &main); return 0; AS 2016 14

Pointers type *name; // declare a pointer type *name = address; // declare + initialize a pointer A variable that contains a memory address points to somewhere in the process virtual address space AS 2016 int main(int argc, char **argv) int x = 42; int *p; // p is a pointer to an integer p = &x; // p now stores the address of x printf("x is %d\n", x); printf("&x is %p\n", &x); printf("p is %p\n", p); return 0; pointy.c

Dereferencing a pointer v = *pointer; *pointer = value; // dereference a pointer // dereference / assign dereference: access the memory referred to by a pointer #include <stdio.h> AS 2016 int main(int argc, char **argv) int x = 42; int *p; // p is a pointer to an integer p = &x; // p now stores the address of x printf("x is %d\n", x); *p = 99; printf("x is %d\n", x); return 0; deref.c

NULL a guaranteed-to-be-invalid memory location typeof(null) is void * In C on Linux: NULL is 0x0000000000000000 (or 0x00000000) Any attempt to dereference NULL segmentation fault #include <stdio.h> AS 2016 int main(int argc, char *argv[]) int *p = NULL; *p = 1; // causes a segmentation fault return 0; segfault.c

Something curious Run this several times: #include <stdio.h> asr.c int main(int argc, char *argv[]) int x = 1; int *p = &x; AS 2016 printf( &x: %p; p: %p; &p: %p\n, &x, p, &p); return 0; troscoe@cixous:~$ gcc -Wall asr.c troscoe@cixous:~$./a.out &x: 0x7fff33b06314; p: 0x7fff33b06314; &p: 0x7fff33b06318 troscoe@cixous:~$./a.out &x: 0x7fffee820f34; p: 0x7fffee820f34; &p: 0x7fffee820f38 troscoe@cixous:~$./a.out &x: 0x7fff230d8b64; p: 0x7fff230d8b64; &p: 0x7fff230d8b68

Address Space Layout Randomization Linux randomizes: Base of stack Shared library locations Security feature We ll see some overflow attacks later Makes debugging tougher Kernel virtual memory User stack shared libraries Run-time heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused 0xffffffffffffffff 0x0000000000000000 AS 2016 19

Summary Every variable in C has: Name: what is it called? Address: where in memory is it? Type: how to interpret the value Value: what is stored at the address C pointers are variables that contain the addresses of other variables. Java has no pointers (only object references), C# has them only in unsafe code // px is a ptr to an integer int *px; // x is an integer int x; // px gets the address of x // or px points to x px = &x; // x gets the contents of // whatever x points to x = *px; 20

4.3: Box-and-arrow diagrams Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 21

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 3 0x7fffbe90f868 arr[2] 4 0x7fffbe90f858 p 0x7fffbe90f864 22

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f868 arr[2] 4 0x7fffbe90f864 arr[1] 3 0x7fffbe90f860 arr[0] 2 0x7fffbe90f858 p 0x7fffbe90f864 0x7fffbe90f854 x 1 23 main() stack frame

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 3 0x7fffbe90f868 arr[2] 4 0x7fffbe90f858 p 0x7fffbe90f864 24

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; boxarrow2.c *(*dp) += 1; p += 1; *(*dp) += 1; return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f868 arr[2] 4 0x7fffbe90f848 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f864 25

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; boxarrow2.c *(*dp) += 1; p += 1; *(*dp) += 1; return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f868 arr[2] 4 0x7fffbe90f848 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f868 26

Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; *(*dp) += 1; p += 1; *(*dp) += 1; return 0; boxarrow2.c Pointers are typed: int *int_ptr; char *char_ptr; int **ptr_ptr; Pointer arithmetic follows pointer type address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f848 0x7fffbe90f868 arr[2] 5 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f868 27

4.4: Pointer arithmetic Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 28

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 29

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 30

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 int_ptr: 0x7fff19be2aa4; *int_ptr: 2 31

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 char_ptr return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 int_ptr: 0x7fff19be2aa4; *int_ptr: 2 int_ptr: 0x7fff19be2abc; *int_ptr: 32767 32

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 33

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 char_ptr: 0x7fff19be2aa1; *char_ptr: 0 char_ptr: 0x7fff19be2aa3; *char_ptr: 0 34

Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) 3 0 0 0 2 0 0 0 1 0 0 0 char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 char_ptr: 0x7fff19be2aa1; *char_ptr: 0 char_ptr: 0x7fff19be2aa3; *char_ptr: 0 35

How much memory does a value take up? Depends on machine and compiler! Use: sizeof(type) or sizeof(value) Evaluates at compile time to size in bytes e.g. int nr = 1919; int size = sizeof(nr); 36

Summary No checks on where pointers point to! You can add an integer n to a pointer p. p now points to n objects further on Or back, in n is negative New value of p (not *p!) depends on type of *p sizeof() This looks a bit like array indexing 37

4.5: Arrays and pointers Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 38

Arrays and pointers An array name in an expression is treated as a pointer to the first element of the array int a[10]; assert(a == &(a[0])); int get_1(int i) int *p = a; return p[i]; void set_1(int i, int v) int *p = a; p[i] = v; int get_2(int i) int *p = a; return *(p+i); void set_2(int i, int v) int *p = a; *(p+i) = v; int get_3(int i) int *p = a+i; return *p; void set_3(int i, int v) int *p = a+i; *p = v; AS 2016 39

except when 1. The array s address is taken with & int a[10]; assert( &a == a ); 2. The array is a string literal initializer 3. The array is an operand of sizeof() int a[10]; assert( sizeof(a) == 10*sizeof(int)); assert( sizeof(&a[0]) == sizeof(int *)); 40

In fact A[i] is always rewritten *(A+i) in the compiler int a[10]; assert(a == &(a[0])); assert(a[5] == 5[a]); int get_2(int i) int *p = a; return i[p]; void set_2(int i, int v) int *p = a; i[p] = v; AS 2016 41

An array name as a function parameter is a pointer The following are all precisely equivalent: int arrfun( int *myarray ) int arrfun( int myarray[] ) int arrfun( int myarray[42] ) Functions are also converted to pointers like this! 42

and can be called in any of these ways: int some_int; int *some_ptr; int some_array[10]; arrfun( &some_int ); arrfun( some_ptr ); arrfun( some_array ); arrfun( &some_array[i] ); all of which turn into pointers. 43

You can t rename an array Compile-time error: int array1[42], array2[42]; main(int argc, char *argv[]) array1 = array2; return 0; But this is OK (it s a pointer): void arrfun(int array[]) array = array2; 44

4.6: Passing by reference Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe 45

Pass-by-value C passes function arguments by value Callee gets a copy of the argument If callee modifies an argument, caller s copy isn t modified #include <stdio.h> void swap(int a, int b) int tmp = a; a = b; b = tmp; int main(int argc, char *argv[]) int a = 42, b = -7; swap(a, b); printf("a: %d, b: %d\n", a, b); return 0; brokenswap.c 46

Pass-by-reference You can use pointers to pass by reference Callee still receives a copy of the argument But it s now a pointer Pointer s value points to the variable in scope of the caller Callee can therefore modify a variable in the scope of the caller #include <stdio.h> void swap(int *a, int *b) int tmp = *a; *a = *b; *b = tmp; int main(int argc, char *argv[]) int a = 42, b = -7; swap(&a, &b); printf("a: %d, b: %d\n", a, b); return 0; swap.c 47

A simple strcpy() char *strcpy(char *dest, char *src) char *r = dest; while(*dest++ = *src++); return r; strcpy.c Lots going on here! Strings are arrays of characters terminated by null bytes Assigment is an expression, not a statement Non-zero values evaluate to true, zero evaluates to false Post-increment operators bind more tightly than pointer dereference A semicolon is statement terminator, not a separator This is also a dangerous function: use strncpy() instead. 48

C Pointer Declarations int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() p is a pointer to int p is an array[13] of pointer to int p is an array[13] of pointer to int p is a pointer to a pointer to an int p is a pointer to an array[13] of int f is a function returning a pointer to int f is a pointer to a function returning int f is a function returning ptr to an array[13] of pointers to functions returning int int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints 49