Pointers in C/C++ 1 Memory Addresses 2

Similar documents
C++ for Java Programmers

Lecture 04 Introduction to pointers

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

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

12. Pointers Address-of operator (&)

Pointers. Memory. void foo() { }//return

Solution Manual for Data Structures and Problem Solving Using C++ 2nd edition by Mark A. Weiss

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

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

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Introduction to C: Pointers

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

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

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

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

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

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

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

The Dynamic Typing Interlude

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

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

CS61C : Machine Structures

CS 103 Lab - Party Like A Char Star

Dynamic Allocation in C

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

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

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

Pointers. 10/5/07 Pointers 1

Arrays and Pointers in C. Alan L. Cox

Pointers. Variable Declaration. Chapter 10

Reference operator (&)

Intermediate Programming, Spring 2017*

Lecture 2: C Programm

Data Representation and Storage

C++ Programming Chapter 7 Pointers

Homework #3 CS2255 Fall 2012

What is an algorithm?

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

CS61C : Machine Structures

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

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

Goals of this Lecture

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

Pointers. Reference operator (&) ted = &andy;

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Introduction to Computer Science Midterm 3 Fall, Points

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

CSC 211 Intermediate Programming. Arrays & Pointers

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Homework #2 Think in C, Write in Assembly

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS61C : Machine Structures

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

ECE 15B COMPUTER ORGANIZATION

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

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

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

[0569] p 0318 garbage

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

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

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

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

For example, let s say we define an array of char of size six:

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Dynamic Allocation in C

Heap Arrays and Linked Lists. Steven R. Bagley

Lectures 13 & 14. memory management

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Understanding Pointers

QUIZ. What is wrong with this code that uses default arguments?

pointers + memory double x; string a; int x; main overhead int y; main overhead

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Data Structure Series

C Arrays, Strings, More Pointers Instructor: Steven Ho

Variables and Constants

1007 Imperative Programming Part II

CS61C : Machine Structures

CMSC202 Computer Science II for Majors

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

SYSC 2006 C Winter 2012

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

FUNCTIONS POINTERS. Pointers. Functions

Lecture 05 POINTERS 1

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

6. Pointers, Structs, and Arrays. 1. Juli 2011

Heap Arrays. Steven R. Bagley

Kurt Schmidt. October 30, 2018

A brief introduction to C programming for Java programmers

3. Simple Types, Variables, and Constants

Transcription:

Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!................................... 6 2.3 Pointers and const................................................ 7 2.4 The sizeof Operator.............................................. 8 3 Passing Pointers to Functions 9 4 A Deeper Look at Arrays 10 5 Pointer Arithmetic 12 6 C-style Strings 13 7 Function Pointers 14 1

1 Memory Addresses So far when we have declared variables such as int age; double grade; string myname; we haven t really thought about where or how these variables were stored Now, we re going to dig a little deeper, and look into these topics When we declare a variable such at int x; in a program, a chunk of main memory is allocated however much space an integer requires How large that chunk is depends on the type of variable we declare Note that all memory is allocated in some number of bytes, not bits In the case of integers (ints), most modern architectures allocate 4 bytes per int Each byte of main memory can be accessed by a specific address When we declare variables in programs as above and those programs are loaded into memory, the variable names are essentially bound to whatever memory locations those values were loaded into C++ provides a method of declaring variables which simply store the memory locations of another variables That is, they point to another variable Reference parameters to functions use this method In C, there are no reference parameters... instead, if you wanted to pass parameters by reference, you passed them by passing their locations in memory 2

2 Pointers and Indirection Variable that stores the location of another variable are called pointers They are declared by preceding the variable name with a * in the declaration int *numptr; In this case, this tells C++ that numptr will hold the memory location of an int If we wanted to create a pointer to a double, we would do something like double *otherptr; As the book mentions, the * in the variable name is not part of the type... as such, the * only applies to the variable whose name immediately comes after For example, the following int *numptr, num; defines numptr to be a pointer to an int, and num is defined as an int (not a pointer!) If you wanted to define two pointers to integers, you would have to do int *numptr1, *numptr2; Make no mistake! In the above example... numptr simply stores some memory location It s value is just a number indicating where something is stored As a good practice, pointer names contain the suffix Ptr in their name, as this makes it immediately obvious that the variable is a pointer 3

2.1 The & and * Operators So now that we can create pointers? How do we set them to specific memory locations? How do we retrieve the memory locations of other variables? As we saw in the previous examples, we can use the & operator For any variable v, the & operator returns the numeric memory location of v E.g. given int v; &v returns the numeric version of the memory location for v revisit old examples... So, now that we have that... We can assign a pointer to point to another variable (that is, set the pointer to the memory location of the variable) by doing the following... int v; int *vptr = &v; // now "vptr" contains the location of "v" // (we say "vptr points to v") Given two pointers, we can copy one into another through the usual assignment... int v; int *vptr1, *vptr2; vptr1 = &v; vptr2 = vptr1; // now vptr points to v // now vptr2 also points to v 4

But what can we do with a pointer? This is where the * operator (sometimes called the dereferencing operator) comes in... Given a pointer/memory-location, the * operator dereferences the pointer, and refers to the actual value in the memory location That is, *vptr is the value in the memory location of vptr Note that we can also define pointers to pointers... int v = 10; int *vptr = &v; // point vptr to v int **vptr2 = &vptr; // point vptr2 to vptr Note that the type of vptr2 is int**, since it s a double pointer (a pointer to a pointer) There is no limit to the level of indirection 5

2.2 A Comment on Types - Muy Importante! READ THIS. RE-READ THIS. LEARN THIS. It s exceedingly important that you understand the following... Given the following two declarations... int v = 10; // set v to 10 int *vptr = &v; // point vptr to v Do NOT mistake the types of the following expressions... Expression v vptr &v *vptr &vptr Type int int* int* int int** Notice that the above expressions can be build upon indefinitely That is, *&*&v is a valid expression! What type is it? MOST pointer errors are due to a misunderstanding of something above 6

2.3 Pointers and const There are two different ways to make pointers const 1. The first creates a pointer to constant data const int *xptr; Think of this as follows... xptr is a pointer to a constant integer (const int). We can tell xptr to point anywhere, but whatever it points to, it must be a constant integer. Note that the value of xptr can change (it can point to different things). 2. The second creates a constant pointer, which means it must always point to the same thing (i.e. its value is constant) int * const xptr = &x Here, xptr is fixed to point to x, and it can never point anywhere else. That does not mean, however, that the value of x is constant! Note that like a normal constant, one must give xptr a value (assign it to something) in its declaration! So, for pointers to constant data, the const qualifier comes before the * For pointers which are constant in what they point to, the const qualifier comes after the * The two methods of const pointers above can also be combined I.e. we can create a pointer which points to constant data, and can never point to anything else... const int * const xptr = &x; 7

2.4 The sizeof Operator C++ provides an operator called sizeof(x), which evaluates to the size of a data type, variable/constant, or an array. It measures the size in the number of bytes So you can use sizeof to determine how many bytes are used for specific data types, which may change from system to system 8

3 Passing Pointers to Functions Reference parameters, which we covered earlier, are specific to C++ They don t exist in C In C, if you wanted to pass something by reference, you would pass it by pointer As you ve seen with pointers, manipulating them can be a little tricky at times C++ compilers really just translate these reference parameters to pointer parameters, and the compiler does all of the tricky manipulation for you Passing by pointers still have their place, but they re few and far between (and certainly not necessary for anything in this class) Like reference parameters, if you are passing something by pointer for the sole purpose of reducing overhead, you should make the pointer parameter constant! 9

4 A Deeper Look at Arrays As the book covers in 8.9, there is a close relationship between arrays and pointers An array such as int foo[10]; really just allocates a chunk of memory to store 10 integers, and the array name foo is simply a pointer to the start of this piece of memory How much memory is allocated? Enough for 10 integers. How much for each integer? Simply sizeof(int) bytes! So, an array declaration like type arrayname[arraysize];, will allocate bytes, and set arrayname to point to the first element sizeof(type) * arraysize So an array name is just a pointer! What about brackets? If the name is just a pointer, what do the brackets do? Brackets are an operator in C++, just like +, -, or even sizeof() Specifically, they translate as follows. Given something like array[index] this is translated as *(array + index) What does this do? 10

So the brackets are just operators for doing simple pointer arithmetic! 11

5 Pointer Arithmetic many many examples... 12

6 C-style Strings 8.13 in the book covers pointer-based strings, which were the original way strings were stored and worked with (and still are in C) Strings are stored essentially as arrays of chars, which as we saw earlier, are just pointers to chunks of memory They have the property that a proper C-style string must end in a null character ( \0 ) To determine the length of such a string, you essentially iterate through the array until you find the null character, and keep and ongoing count of each character you pass There are functions for all of this (strlen for calculating the length), which is pretty standard across systems You should read this chapter and be aware of what it talks about, but this is the most we ll cover in class If you work with any older software (or any software in C), you will see these Any questions on the quiz on Friday or on the Final Exam regarding this section will be very high-level (read: easy), such as How are C-style strings stored and manipulated? 13

7 Function Pointers When a program containing functions is compiled, every function is compiled as well (obviously) and when the resulting binary is loaded into memory, each compiled function is loaded as well Each compiled function can be referenced in memory by the location in memory where its code begin As such, we can point to functions just like we can point to variables This can be extremely useful when you don t just want to pass values to functions, but also other functions examples You can also create arrays of function pointers, where each element in the array points to a different function (each of the same type) This can be extremely handy for menu driven applications, event handling, and a number of other things Covering function pointers completely would take at least an entire class, simply because the syntax for them can be quite difficult at first I encourage you to read 8.12 of the book for examples Also, a great reference for function pointers (using them in virtually every situation you could possibly think of), is... http://www.function-pointer.org/ 14