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

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

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

Basic Types and Formatted I/O

Arrays and Pointers in C. Alan L. Cox

Memory, Arrays & Pointers

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

A brief introduction to C programming for Java programmers

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

C - Basics, Bitwise Operator. Zhaoguo Wang

VARIABLES AND TYPES CITS1001

Limitations of the stack

Section Notes - Week 1 (9/17)

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

Memory, Data, & Addressing II CSE 351 Spring

Introduction to Programming Using Java (98-388)

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

Programming refresher and intro to C programming

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

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

Pointers, Dynamic Data, and Reference Types

CSE 307: Principles of Programming Languages

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

Heap Arrays. Steven R. Bagley

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

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

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

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

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

Lecture Notes on Types in C

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

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

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

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

Fundamental of Programming (C)

Contents of Lecture 3

Heap Arrays and Linked Lists. Steven R. Bagley

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

EL2310 Scientific Programming

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

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

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

CS Programming In C

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

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

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

Variables. Store information needed by the program

2 rd class Department of Programming. OOP with Java Programming

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

Exercise: Using Numbers

LAB A Translating Data to Binary

Principles of C and Memory Management

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

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

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

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

Topic 9: Type Checking

Topic 9: Type Checking

Java and C CSE 351 Spring

Important From Last Time

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

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

THE FUNDAMENTAL DATA TYPES

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

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

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

CSI33 Data Structures

CSC 1107: Structured Programming

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

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

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.

Important From Last Time

Elementary Programming

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

Lecture 2: C Programm

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

PRINCIPLES OF OPERATING SYSTEMS

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

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

Programming in C and C++

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

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

Full file at

Pointers, Arrays, and Strings. CS449 Spring 2016

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

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

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

1d: tests knowing about bitwise fields and union/struct differences.

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

Week 3 Lecture 2. Types Constants and Variables

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

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

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

Ruby: Objects and Dynamic Types

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. 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 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); This will print t points to the string world. 5

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

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