Pointers (continued), arrays and strings

Similar documents
Pointers (continued), arrays and strings

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

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

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

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

Limitations of the stack

Basic Types and Formatted I/O

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

Arrays and Pointers in C. Alan L. Cox

Memory, Arrays & Pointers

A brief introduction to C programming for Java programmers

C - Basics, Bitwise Operator. Zhaoguo Wang

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

VARIABLES AND TYPES CITS1001

Aliasing restrictions of C11 formalized in Coq

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Section Notes - Week 1 (9/17)

Programming refresher and intro to C programming

Memory, Data, & Addressing II CSE 351 Spring

Introduction to Programming Using Java (98-388)

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CSE 307: Principles of Programming Languages

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Pointers, Dynamic Data, and Reference Types

Heap Arrays. Steven R. Bagley

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Lecture Notes on Types in C

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

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

Contents of Lecture 3

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

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

Heap Arrays and Linked Lists. Steven R. Bagley

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

EL2310 Scientific Programming

2 rd class Department of Programming. OOP with Java Programming

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

CS Programming In C

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Variables. Store information needed by the program

LAB A Translating Data to Binary

Topic 9: Type Checking

Topic 9: Type Checking

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Fundamental of Programming (C)

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

Pierce Ch. 3, 8, 11, 15. Type Systems

Exercise: Using Numbers

Java and C CSE 351 Spring

CSI33 Data Structures

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CSC 1107: Structured Programming

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

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Hacking in C. The C programming language. Radboud University, Nijmegen, The Netherlands. Spring 2018

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Important From Last Time

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Principles of C and Memory Management

Elementary Programming

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Important From Last Time

Programming in C and C++

THE FUNDAMENTAL DATA TYPES

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Lecture 2: C Programm

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Full file at

PRINCIPLES OF OPERATING SYSTEMS

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

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

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Lectures 5-6: Introduction to C

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Ruby: Objects and Dynamic Types

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Introduction to Programming (Java) 2/12

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA


Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

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

CPS122 Lecture: From Python to Java

Transcription:

Pointers (continued), arrays and strings 1

Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2

Pointer arithmetic You can use + and - with pointers. The semantics depends on the type of the pointer: adding 1 to a pointer will go to the next location, given the size and the data type that it points to. 3

Pointer arithmetic You can use + and - with pointers. The semantics depends on the type of the pointer: adding 1 to a pointer will go to the next location, given the size and the data type that it points to. For example, if int *ptr; char *str; then ptr + 2 means ptr + 2 * sizeof(int) str + 2 means str + 2 because sizeof(char) is 1 3

Using pointers as arrays The way pointer arithmetic works means that a pointer to the head of an array behaves like an array. Suppose int a[10] = {1,2,3,4,5,6,7,8,9,19}; int *p = (int *) &a; // the address of the head of a // treated as pointer to an int 4

Using pointers as arrays The way pointer arithmetic works means that a pointer to the head of an array behaves like an array. Suppose int a[10] = {1,2,3,4,5,6,7,8,9,19}; int *p = (int *) &a; // the address of the head of a // treated as pointer to an int Now p + 3 points to a[3] So we use addition to pointer p to access the array 4

Pointer arithmetic for strings What is the output of char *msg = "hello world"; char *t = msg + 6; printf("t points to the string %s.", t); 5

Pointer arithmetic for strings What is the output of char *msg = "hello world"; char *t = msg + 6; printf("t points to the string %s.", t); This will print t points to the string world. 5

Arrays vs pointers Arrays and pointers behavearrays similarly, vsbut pointers are very different in memory Consider Arrays and pointers behave similarly, but are very different in memory int a[]; int *p Consider int a[]; int *p; a a[0] a[1]... p *p *(p+1)... A difference: a will always refer to the same array, whereas p can point to different arrays over time hic 6 6

Arrays vs pointers Arrays and pointers behavearrays similarly, vsbut pointers are very different in memory Consider Arrays and pointers behave similarly, but are very different in memory int a[]; int *p Consider int a[]; int *p; a a[0] a[1]... p *p *(p+1)... A difference: a will always refer to the same array, whereas p can point to different arrays over time A difference: a will always refer to the same array, whereas p can point to different arrays over time hic 6 6

Using pointers as arrays Suppose int a[10] = {1,2,3,4,5,6,7,8,9,10}; Then int sum = 0; for (int i = 0; i!= 10; i++) { sum = sum + a[i]; } can also be implemented using pointer arithmetic 7

Using pointers as arrays Suppose Then int a[10] = {1,2,3,4,5,6,7,8,9,10}; int sum = 0; for (int i = 0; i!= 10; i++) { sum = sum + a[i]; } can also be implemented using pointer arithmetic This cast is needed because a is an integer array, so &a is a pointer to int[], not pointer to an int. An alternative would be to write *p = &(a[0]) int sum = 0; for (int *p = (int sum = sum + &p; } *)&a; p!= &(a[10]); p++) { Instead of p!= &(a[10]) we could also write p!= ((int *)&a)+10 7

Using pointers as arrays Suppose Then int a[10] = {1,2,3,4,5,6,7,8,9,10}; int sum = 0; for (int i = 0; i!= 10; i++) { sum = sum + a[i]; } can also be implemented using pointer arithmetic This cast is needed because a is an integer array, so &a is a pointer to int[], not pointer to an int. An alternative would be to write *p = &(a[0]) int sum = 0; for (int *p = (int *)&a; p!= &(a[10]); p++) { sum = sum + &p; } Instead of p!= &(a[10]) we could also write But nobody in their right mind would p!= ((int *)&a)+10 7

A problem with pointers:... int i; int j; int *x;... // lots of code omitted i = 5; j++ // what is the value of i here? (*x)++; // what is the value of i here? 8

A problem with pointers:... int i; int j; int *x;... // lots of code omitted i = 5; j++ // what is the value of i here? (*x)++; // what is the value of i here? 5 5 or 6, depending on whether *x points to i 8

Two pointers are called aliases if they point to the same location int i = 5; int *x = &i; int *y = &i; // x and y are aliases now (*x)++; // now i and *y have also changed to 6 9

Two pointers are called aliases if they point to the same location int i = 5; int *x = &i; int *y = &i; // x and y are aliases now (*x)++; // now i and *y have also changed to 6 Keeping track of pointers, in the presence of potential aliasing, can be really confusing, and really hard to debug... 9

Recap so far We have seen pointers, e.g. of type char *p with the operations * and & These are tricky to understand, unless you draw pictures 10

Recap so far We have seen pointers, e.g. of type char *p with the operations * and & These are tricky to understand, unless you draw pictures We can have aliasing, where two names, say *p and c, can refer to the same variable (location in memory) 10

Recap so far We have seen pointers, e.g. of type char *p with the operations * and & These are tricky to understand, unless you draw pictures We can have aliasing, where two names, say *p and c, can refer to the same variable (location in memory) We can use pointer arithmetic, and e.g. write *(p+1), and use this to access arrays 10

Recap so far We have seen pointers, e.g. of type char *p with the operations * and & These are tricky to understand, unless you draw pictures We can have aliasing, where two names, say *p and c, can refer to the same variable (location in memory) We can use pointer arithmetic, and e.g. write *(p+1), and use this to access arrays Confusingly, the meaning of addition for pointers depends on their type, as +1 for pointers of type int * really means +sizeof(int) 10

The potential of pointers: inspecting raw memory To inspect a piece of raw memory, we can cast it to a unsigned char * and then inspect the bytes float f = 3.14; unsigned char *p = (unsigned char *) &f; printf("the representation of float %f is", f); for (int i = 0; i < sizeof(float); i++, p++);) { printf("%d", *p); } printf("\n"); 11

Turning pointers into numbers intptr_t defined in stdint.h is an integral type that is guaranteed to be wide enough to hold pointers. int *p; // p points to an int intptr_t i = (intptr_t) p; // the address as a number p++; i++; // Will i and p be the same? 12

Turning pointers into numbers intptr_t defined in stdint.h is an integral type that is guaranteed to be wide enough to hold pointers. int *p; // p points to an int intptr_t i = (intptr_t) p; // the address as a number p++; i++; // Will i and p be the same? // No! i++ increases by 1, p++ with sizeof(int) 12

Turning pointers into numbers intptr_t defined in stdint.h is an integral type that is guaranteed to be wide enough to hold pointers. int *p; // p points to an int intptr_t i = (intptr_t) p; // the address as a number p++; i++; // Will i and p be the same? // No! i++ increases by 1, p++ with sizeof(int) There is also an unsigned version of intptr_t: uintptr_t 12

Strings 13

Strings Having seen arrays and pointers, we can now understand C strings char *s = "hello world\n"; 14

Strings Having seen arrays and pointers, we can now understand C strings char *s = "hello world\n"; C strings are char arrays, which are terminated by a special null character, aka a null terminator, which is written as \0 14

Strings Having seen arrays and pointers, we can now understand C strings char *s = "hello world\n"; C strings are char arrays, which are terminated by a special null character, aka a null terminator, which is written as \0 There is a special notation for string literals, between double quotes, where the null terminator is implicit. 14

Strings Having seen arrays and pointers, we can now understand C strings char *s = "hello world\n"; C strings are char arrays, which are terminated by a special null character, aka a null terminator, which is written as \0 There is a special notation for string literals, between double quotes, where the null terminator is implicit. As other arrays, we can use both the array type char[] and the pointer type char * for them. 14

String problems Working with C strings is highly error prone! There are two problems 15

String problems Working with C strings is highly error prone! There are two problems 1. As for any array, there are no array bounds checks so it s the programmer s responsibility not to go outside the array bounds 15

String problems Working with C strings is highly error prone! There are two problems 1. As for any array, there are no array bounds checks so it s the programmer s responsibility not to go outside the array bounds 2. It is also the programmer s responsibility to make sure that the string is properly terminated with a null character. If a string lacks its null terminator, e.g. due to problem 1, then standard functions to manipulate strings will go off the rails. 15

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 16

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 16

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 2. Which will be resized automatically if they do not fit (e.g. Python) 16

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 2. Which will be resized automatically if they do not fit (e.g. Python) 3. The language will ensure that all strings are null-terminated (e.g. C++, Java and Python) 16

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 2. Which will be resized automatically if they do not fit (e.g. Python) 3. The language will ensure that all strings are null-terminated (e.g. C++, Java and Python) More precisely, the programmer does not even have to know how strings are represented, and whether null-terminator exists and what they look like: the representation of strings is completely transparant/invisible to the programmer. 16

Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 2. Which will be resized automatically if they do not fit (e.g. Python) 3. The language will ensure that all strings are null-terminated (e.g. C++, Java and Python) More precisely, the programmer does not even have to know how strings are represented, and whether null-terminator exists and what they look like: the representation of strings is completely transparant/invisible to the programmer. Moral of the story: if you can, avoid using standard C strings. E.g. in C++, use C++ type strings; in C, use safer string libraries. 16

A final string pecularity Strig literals, as in char *msg = "hello, world"; are meant to be constant or read-only: you are not supposed to change the character that made up a string literal. 17

A final string pecularity Strig literals, as in char *msg = "hello, world"; are meant to be constant or read-only: you are not supposed to change the character that made up a string literal. Unfortunately, this does not mean that C will prevent this. It only means that the C standard defines changing a character in a string literal as having undefined behaviour 17

A final string pecularity Strig literals, as in char *msg = "hello, world"; are meant to be constant or read-only: you are not supposed to change the character that made up a string literal. Unfortunately, this does not mean that C will prevent this. It only means that the C standard defines changing a character in a string literal as having undefined behaviour E.g. char *t = msg + 6; *t = ; ; Has undefined behaviour, i.e. anything may happen. 17

A final string pecularity Strig literals, as in char *msg = "hello, world"; are meant to be constant or read-only: you are not supposed to change the character that made up a string literal. Unfortunately, this does not mean that C will prevent this. It only means that the C standard defines changing a character in a string literal as having undefined behaviour E.g. char *t = msg + 6; *t = ; ; Has undefined behaviour, i.e. anything may happen. Compilers can emit warnings if you change string literals, e.g. gcc -Wwrite-strings 17

Recap We have seen 18

Recap We have seen The different C types 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & C strings, as special char arrays 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & C strings, as special char arrays Their representation 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & C strings, as special char arrays Their representation How these representations can be broken, i.e. how we can inspect and manipulate the underlying representation (e.g. with casts) 18

Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & C strings, as special char arrays Their representation How these representations can be broken, i.e. how we can inspect and manipulate the underlying representation (e.g. with casts) Some things that can go wrong e.g. due to access outside array bounds or integer under/overflow 18