Arrays and Pointers in C. Alan L. Cox

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

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

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

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

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

Arrays and Memory Management

Fundamental of Programming (C)

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Array Initialization

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

CS 0449 Sample Midterm

Lecture 04 Introduction to pointers

Memory, Data, & Addressing II CSE 351 Spring

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

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

CS 241 Data Organization Pointers and Arrays

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

CS 61c: Great Ideas in Computer Architecture

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

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

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

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 {

Intermediate Programming, Spring 2017*

Pointers. Pointer References

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

Pointers. Pointers. Pointers (cont) CS 217

primitive arrays v. vectors (1)

CS61C : Machine Structures

Lab 3. Pointers Programming Lab (Using C) XU Silei

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Personal SE. Functions, Arrays, Strings & Command Line Arguments

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.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

PRINCIPLES OF OPERATING SYSTEMS

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

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

C Pointers. 6th April 2017 Giulio Picierro

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Understanding Pointers

Systems Programming and Computer Architecture ( )

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

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

Dynamic memory allocation

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

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

Dynamic memory allocation (malloc)

Pointers (continued), arrays and strings

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

FUNCTIONS POINTERS. Pointers. Functions

ECE 15B COMPUTER ORGANIZATION

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

Approximately a Test II CPSC 206

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Course organization. Course introduction ( Week 1)

ELEC 377 C Programming Tutorial. ELEC Operating Systems

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Pointers (continued), arrays and strings

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

Lectures 5-6: Introduction to C

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Lecture 07 Debugging Programs with GDB

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

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

Intro to C: Pointers and Arrays

Memory Corruption 101 From Primitives to Exploit

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

Binghamton University. CS-211 Fall Pointers vs. Arrays

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

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

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

Introduction to Scientific Computing and Problem Solving

Variables and literals

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

Arrays, Strings, and Pointers

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Memory, Arrays & Pointers

CSC 1600 Memory Layout for Unix Processes"

Lectures 5-6: Introduction to C

CS61C : Machine Structures

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

Kurt Schmidt. October 30, 2018

Pointers in C/C++ 1 Memory Addresses 2

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Hardware: Logical View

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Programming. Pointers, Multi-dimensional Arrays and Memory Management

211: Computer Architecture Summer 2016

Quick review pointer basics (KR ch )

Arrays and Pointers (part 1)

CS61, Fall 2012 Section 2 Notes

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Transcription:

Arrays and Pointers in C Alan L. Cox alc@rice.edu

Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including their similarities and differences Cox / Fagan Arrays and Pointers 2

Arrays in C All elements of same type homogenous Unlike Java, array size in declaration int array[10]; int b; array[0] = 3; array[9] = 4; array[10] = 5; array[-1] = 6; Compare: C: Java: First element (index 0) Last element (index size - 1) int array[10]; int[] array = new int[10]; No bounds checking! Allowed usually causes no obvious error array[10] may overwrite b Cox / Fagan Arrays and Pointers 3

Array Representation Homogeneous Each element same size s bytes An array of m data values is a sequence of m s bytes Indexing: 0 th value at byte s 0, 1 st value at byte s 1, m and s are not part of representation Unlike in some other languages s known by compiler usually irrelevant to programmer m often known by compiler if not, must be saved by programmer 0x1008 0x1004 0x1000 a[2] a[1] a[0] int a[3]; Cox / Fagan Arrays and Pointers 4

Array Representation char c1; int a[3]; char c2; int i; 0x1014 i 0x1010 c2 0x100C 0x1008 0x1004 0x1000 c1 a[2] a[1] a[0] Could be optimized by making these adjacent, and reducing padding (by default, not) Array aligned by size of elements Cox / Fagan Arrays and Pointers 5

Array Sizes int array[10]; What is returns the size of an object in bytes sizeof(array[3])? sizeof(array)? 4 40 Cox / Fagan Arrays and Pointers 6

Multi-Dimensional Arrays int matrix[2][3]; matrix[1][0] = 17; Recall: no bounds checking 0x1014 0x1010 0x100C 0x1008 0x1004 0x1000 matrix[1][2] matrix[1][1] matrix[1][0] matrix[0][2] matrix[0][1] matrix[0][0] What happens when you write: matrix[0][3] = 42; Row Major Organization Cox / Fagan Arrays and Pointers 7

Variable-Length Arrays int function(int n) { int array[n]; New C99 feature: Variable-length arrays defined within functions Global arrays must still have fixed (constant) length Cox / Fagan Arrays and Pointers 8

Memory Addresses Storage cells are typically viewed as being byte-sized Usually the smallest addressable unit of memory Few machines can directly address bits individually Such addresses are sometimes called byteaddresses Memory is often accessed as words Usually a word is the largest unit of memory access by a single machine instruction CLEAR s word size is 8 bytes (= sizeof(long)) A word-address is simply the byte-address of the word s first byte Cox / Fagan Arrays and Pointers 9

Pointers Special case of bounded-size natural numbers Maximum memory limited by processor word-size 2 32 bytes = 4GB, 2 64 bytes = 16 exabytes A pointer is just another kind of value A basic type in C int *ptr; The variable ptr stores a pointer to an int. Cox / Fagan Arrays and Pointers 10

Pointer Operations in C Creation & variable Returns variable s memory address Dereference * pointer Returns contents stored at address Indirect assignment * pointer = val Stores value at address Of course, still have... Assignment pointer = ptr Stores pointer in another variable Cox / Fagan Arrays and Pointers 11

Using Pointers int i1; int i2; int *ptr1; int *ptr2; 0x1014 0x1010 ptr2: 0x1000 i1 = 1; i2 = 2; 0x100C 0x1008 ptr1: 0x1000 ptr1 = &i1; 0x1004 i2: 23 ptr2 = ptr1; 0x1000 i1: 31 *ptr1 = 3; i2 = *ptr2; Cox / Fagan Arrays and Pointers 12

Using Pointers (cont.) int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; *int_ptr1 = int_ptr2; *int_ptr1 = int2; What happens? int1 becomes 8 Type check warning: int_ptr2 is not an int Cox / Fagan Arrays and Pointers 13

Using Pointers (cont.) int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; int_ptr1 = *int_ptr2; int_ptr1 = int_ptr2; What happens? Type check warning: *int_ptr2 is not an int * Changes int_ptr1 doesn t change int1 Cox / Fagan Arrays and Pointers 14

Pointer Arithmetic pointer + number pointer number E.g., pointer + 1 adds 1 something to a pointer char *p; char a; char b; p = &a; p += 1; In each, p now points to b (Assuming compiler doesn t reorder variables in memory) int *p; int a; int b; p = &a; p += 1; Adds 1*sizeof(char) to the memory address Adds 1*sizeof(int) to the memory address Pointer arithmetic should be used cautiously Cox / Fagan Arrays and Pointers 15

A Special Pointer in C Special constant pointer NULL Points to no data Dereferencing illegal causes segmentation fault To define, include <stdlib.h> or <stdio.h> Cox / Fagan Arrays and Pointers 16

Generic Pointers void *: a pointer to anything void *p; int i; char c; p = &i; p = &c; putchar(*(char *)p); Lose all information about what type of thing is pointed to Reduces effectiveness of compiler s type-checking Can t use pointer arithmetic type cast: tells the compiler to change an object s type (for type checking purposes does not modify the object in any way) Dangerous! Sometimes necessary Cox / Fagan Arrays and Pointers 17

Pass-by-Reference void set_x_and_y(int *x, int *y) { } *x = 1001; *y = 1002; void f(void) { int a = 1; int b = 2; a b x y 1001 1 1002 2 } set_x_and_y(&a, &b); Cox / Fagan Arrays and Pointers 18

Arrays and Pointers Dirty secret : Array name a pointer to the initial (0th) array element a[i] *(a + i) An array is passed to a function as a pointer The array size is lost! Usually bad style to interchange arrays and pointers Avoid pointer arithmetic! Really int *array int foo(int array[], { } int unsigned int size) array[size - 1] main(void) { } Passing arrays: int a[10], b[5]; Must explicitly pass the size foo(a, 10) foo(b, 5) Cox / Fagan Arrays and Pointers 19

Arrays and Pointers int foo(int array[], unsigned int size) { printf( %d\n, sizeof(array)); } What does this print?... because array is really a pointer 8 int main(void) { int a[10], b[5]; foo(a, 10) foo(b, 5) printf( %d\n, sizeof(a)); } What does this print? 40 Cox / Fagan Arrays and Pointers 20

Arrays and Pointers int i; int array[10]; int *p; int array[10]; for (i = 0; i < 10; i++) { array[i] = ; } for (p = array; p < &array[10]; p++) { *p = ; } These two blocks of code are functionally equivalent Cox / Fagan Arrays and Pointers 21

Strings In C, strings are just an array of characters Terminated with \0 character Arrays for bounded-length strings Pointer for constant strings (or unknown length) char str1[15] = Hello, world!\n ; char *str2 = Hello, world!\n ; C, H e l l o, w o r l d!\nterminator C terminator: \0 Pascal, Java, length H e l l o, w o r l d!\n Cox / Fagan Arrays and Pointers 22

String length Must calculate length: array access to pointer! int strlen(char str[]) { int len = 0; can pass an array or pointer Check for terminator } while (str[len]!= \0 ) len++; return (len); What is the size of the array??? Provided by standard C library: #include <string.h> Cox / Fagan Arrays and Pointers 23

Pointer to Pointer (char **argv) Passing arguments to main: int main(int argc, char **argv) { }... Suppose you run the program this way UNIX%./program hello 1 2 3 argc == 5 (five strings on the command line) size of the argv array/vector an array/vector of char * Recall when passing an array, a pointer to the first element is passed Cox / Fagan Arrays and Pointers 24

char **argv 0x1020 0x1018 0x1010 0x1008 0x1000 argv[4] argv[3] argv[2] argv[1] argv[0] 3 2 1 hello./program These are strings!! Not integers! Cox / Fagan Arrays and Pointers 25

Next Time Structures and Unions Cox / Fagan Arrays and Pointers 26