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

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

[0569] p 0318 garbage

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

What is an algorithm?

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

Homework #3 CS2255 Fall 2012

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.

Goals of this Lecture

Pointers. 10/5/07 Pointers 1

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

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

C Pointers. 6th April 2017 Giulio Picierro

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

Computer Programming

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

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

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

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):

Pointers in C/C++ 1 Memory Addresses 2

Pointers, Dynamic Data, and Reference Types

Introduction to Scientific Computing and Problem Solving

CSC 211 Intermediate Programming. Arrays & Pointers

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Lecture 05 POINTERS 1

POINTER & REFERENCE VARIABLES

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

Variation of Pointers

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

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

CS 161 Exam II Winter 2018 FORM 1

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

A program execution is memory safe so long as memory access errors never occur:

CS 2461: Computer Architecture I

CA31-1K DIS. Pointers. TA: You Lu

Pointers! Arizona State University 1

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Reference operator (&)

Course organization. Course introduction ( Week 1)

ECE 15B COMPUTER ORGANIZATION

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

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

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Exam 3 Chapters 7 & 9

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

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

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

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

Pointers, Arrays and C-Strings

APSC 160 Review Part 2

Programming in C - Part 2

EL2310 Scientific Programming

DECLARAING AND INITIALIZING POINTERS

REFERENCES, POINTERS AND STRUCTS

Arrays and Pointers in C. Alan L. Cox

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Dynamic Memory Allocation

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

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

C++ for Java Programmers

Dynamic Allocation in C

CS201- Introduction to Programming Current Quizzes

Lecture Notes on Memory Layout

C Programming Basics II

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

CS31 Discussion 1E Spring 17 : week 08

Pointers and Strings. Adhi Harmoko S, M.Komp

Introduction to C: Pointers

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Example: Pointer Basics

CS61C : Machine Structures

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

More about BOOLEAN issues

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

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

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Chapter 1: Object-Oriented Programming Using C++

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

C++ Programming Chapter 7 Pointers

Pointers. Pointers. Pointers (cont) CS 217

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

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

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

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

Decimal Representation

MYcsvtu Notes LECTURE 34. POINTERS

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

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

Introduction to Computer Science Midterm 3 Fall, Points

Basic Pointers. CSCI 112: Programming in C

Pointers and Arrays 1

Transcription:

Pointers

Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable s location in memory We can later dereference a pointer to have direct access to the object or variable the pointer points to.

Pointers Each memory address refers to the location of a single byte of data and consecutive memory addresses refer to contiguous bytes (i.e. bytes and are next to each other). Another way to think of memory is like a giant char array. }//return

Pointers The compiler reserves a memory location for each variable declared The number of bytes reserved for a variable depend on the size of the variable type (an int is represented by 4 bytes in many systems). int Val; int *ValPtr; int **ValPtrPtr; }//return Val

Pointers A pointer is allocated space in memory just like any other variable. Since a pointer holds an address, 32-bit systems use 32 bit addresses and therefore need 4 bytes to represent an address (32 bits * 1byte/8bits = 4 bytes). **Note that all pointers are allocated the same amount of memory independent of type (char*,int*,double*). int Val; int *ValPtr; int **ValPtrPtr; }//return Val ValPtr

Pointers A pointer pointer holds the address of a pointer of that same type and therefore is allocated the same space as a pointer. An int* holds the address of an int, and int** holds the address of an int*, an int*** holds the address of an int** etc. int Val; int *ValPtr; int **ValPtrPtr; }//return Val ValPtr ValPtrPtr

Pointers When the compiler performs assignment, it goes to the address of a variable and updates the value. More generally, when a programmer writes the name of a variable X it can be interpreted as the value in the memory reserved for X. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return Val ValPtr ValPtrPtr 16

Pointers By placing the & operator in front of a variable name, it is possible to refer to the address of that variable rather than its value. In other words, &X is interpreted as the address of the memory reserved for X. The address of a variable is the address of the first byte it occupies in memory. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return Val ValPtr ValPtrPtr 16

Pointers For variables that are pointers, the * operator allows a programmer access to the value at the address stored in the pointer. This is called dereferencing a pointer and the process is known as indirection. Indirection is one reason pointers have types, although all pointers hold addresses, the compiler must know what type is stored at an address to access the value. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return Val ValPtr ValPtrPtr 5

Pointers Since a pointer pointer holds an address, it can be used almost exactly as a pointer. The extra pointer just says that the value at the address is an int* rather than an int. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return Val ValPtr ValPtrPtr 5

Pointers Since a pointer pointer holds an address, it can be used almost exactly as a pointer. The extra pointer just says that the value at the address is an int* rather than an int. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return Val ValPtr ValPtrPtr 5 0 sample1.cpp

Pointers When a function returns, the memory reservations made for local variables are revoked. In programming, reserving memory is known as memory allocation and revoking memory reservations is known as memory deallocation or freeing memory. The memory still exists, but it may be overwritten by another function or no longer accessible to your program. int Val; int *ValPtr; int **ValPtrPtr; Val = 16; ValPtr = &Val; *ValPtr = 5; ValPtrPtr = &ValPtr; *ValPtrPtr = NULL; }//return sample2.cpp

Arrays and Pointer Arithmetic When an array is declared the compiler allocates enough space for all the elements of that array in memory. The array name now acts like a pointer to the first element in the array although this is just an abstraction because there is actually no address (pointer) stored in memory. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; } MyArray a b c

Arrays and Pointer Arithmetic An actual pointer will reserve memory to hold an address value. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; } MyArray Ptr a b c

Arrays and Pointer Arithmetic When an array is referenced in a program without an index [] the compiler pretends the name refers to a pointer that stores the address of the first element of the array. This allows us to assign a pointer to the value of MyArray. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; } MyArray Ptr a b c

Arrays and Pointer Arithmetic In C++, pointers can be modified with +,-,++, and -- operators. A key difference between a pointer and an integer is that the statement Ptr = Ptr + Num, is translated to Ptr = Ptr + Num*(the size of the element the pointer points to). In this case, the pointer points to a char that is only allocated a single byte in memory. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; Ptr = Ptr + 1; *Ptr = *MyArray *(Ptr+1) = d ; Ptr[1] = a ; } MyArray Ptr a b c

Arrays and Pointer Arithmetic Changing the value of a pointer with arithmetic operations is known as pointer arithmetic. Among other things, it can be useful for traversing an array. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; Ptr = Ptr + 1; *Ptr = *MyArray *(Ptr+1) = d ; Ptr[1] = a ; } MyArray Ptr a a c

Arrays and Pointer Arithmetic Pointer arithmetic and dereference can be combined into one statement to access offsets of an array. When the compiler encounters an indexed array, the statement is translated from array[index] into *(array+index). char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; Ptr = Ptr + 1; *Ptr = *MyArray *(Ptr+1) = d ; Ptr[1] = a ; } MyArray Ptr a a d

Arrays and Pointer Arithmetic The left hand side of the last two statements are equivalent. char MyArray[3] ={ a, b, c }; char *Ptr; Ptr = MyArray; Ptr = Ptr + 1; *Ptr = *MyArray *(Ptr+1) = d ; Ptr[1] = a ; MyArray Ptr a a a } sample3.cpp sample4.cpp

M ore about Pointers One pointer can be assigned to another if they are the same type or a typecast is used Any pointer type may be assigned to a void* type pointer Dereferencing an uninitialized pointer or a NULL (0) pointer will cause a segmentation fault sample5.cpp

R eferences A subclass of pointers specific to C++ Basically a restricted pointer that only points to one object or variable Declared using & operator instead of * (int& IntRef) Only 1 dimensional (no int&&) Must be initialized (int& IntRef; will cause compiler error ) Once initialized, a reference cannot be reseated to another object or variable No arithmetic operations allowed Automatically dereferenced so that only the value of the variable referenced is accessible sample6.cpp

R eferences Another way to think of a reference is giving a particular object or variable another name or alias (sample7.cpp) C++ automatically creates a reference for function calls that specify a reference in their parameter list (sample8.cpp) (sample9.cpp)