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

Similar documents
Pointers II. Class 31

Homework #3 CS2255 Fall 2012

Dynamic Allocation of Memory

CS201- Introduction to Programming Current Quizzes

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Pointer Data Type and Pointer Variables

Pointers, Dynamic Data, and Reference Types

C++ for Java Programmers

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

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Dynamic Allocation in C

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

[0569] p 0318 garbage

Class Information ANNOUCEMENTS

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

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

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

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Lectures 6/7 Pointers and Dynamic Arrays

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

C++ for Java Programmers

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Intermediate Programming, Spring 2017*

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

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

Variables, Memory and Pointers

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Exam 3 Chapters 7 & 9

Dynamic Allocation in C

CSE 307: Principles of Programming Languages

a data type is Types

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

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

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

CS201 Latest Solved MCQs

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CSC 1600 Memory Layout for Unix Processes"

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

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.

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

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

Dynamic Allocation of Memory

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Computer Programming

Short Notes of CS201


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

CS201 - Introduction to Programming Glossary By

Intro to C: Pointers and Arrays

REFERENCES, POINTERS AND STRUCTS

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

CS349/SE382 A1 C Programming Tutorial

ECE 15B COMPUTER ORGANIZATION

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.

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

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

CS 101: Computer Programming and Utilization

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

What have we learned about when we learned about function parameters? 1-1

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

PROGRAMMAZIONE I A.A. 2017/2018

Computer Programming

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

DECLARAING AND INITIALIZING POINTERS

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

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

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

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

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

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

Structured Data. CIS 15 : Spring 2007

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

CS C Primer. Tyler Szepesi. January 16, 2013

CS61C : Machine Structures

TYPES, VALUES AND DECLARATIONS

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

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

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS61C : Machine Structures

Pointers in C/C++ 1 Memory Addresses 2

Arrays, Pointers and Memory Management

Dynamic Memory Allocation

CS2255 HOMEWORK #1 Fall 2012

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

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

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Lecture Notes on Memory Layout

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Chapter 1: Object-Oriented Programming Using C++

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

Memory and C++ Pointers

Memory and Pointers written by Cathy Saxton

POINTER & REFERENCE VARIABLES

Transcription:

Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1

Outline Static memory allocation Pointer variables Pointers Dynamic memory allocation new and delete garbage and dangling references Dynamic arrays Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 2

Static Memory Allocation If we want to implement a class like vector<t>, we need dynamically allocated memory; otherwise the data structure could not grow indefinitely Consider the following declarations int i = 3, j = 4; char a = 'x', b = 'y'; double d = 2.5, e = 3.6; The sizes of these variables depends on the architecture, but often 4, 1, and 8 or 10 bytes. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 3

Static Memory Allocation Exactly how memory is addressed also depends on the architecture. E.g., byte, 16-bit word, 32-bit word. Will assume byteaddressable, 32-bit words. Addresses will be written in hexadecimal (base 16) => 8 digits. Statically allocated memory is allocated at compile-time before the program runs. It is allocated from a stack, which is usually at one end of memory. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 4

Static Memory Allocation 0x00001220 i 0x0000122a d 0x00001221 3 0x00001222 0x00001223 0x00001224 j 2.5 4 0x00001228 0x00001229 'x' 'y' a b 0x00001232 e 3.6 address variable name Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 5

Pointer Variables Pointer variables are regular variables and also are allocated at compile-time. Consider the following additional declarations: // note the repeated *'s int *iptr1, *ptr2; char *cptr1, *cptr2; double *dptr1, *dptr2; Pointers are usually the same size as an int; 4 bytes in our example. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 6

Pointer Variables 0x0000122a d 0x0000123a iptr1 0x0000124a dptr1 2.5 0x0000123e iptr2 0x0000124e dptr2 0x00001232 e 0x00001242 cptr1 3.6 0x00001246 cptr2 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 7

Pointers What is a pointer? An address. How do we get an address? One way is to use the & operator to get the address of a variable. E.g., iptr1 = &i; // iptr1 points to i iptr2 = &j; cptr1 = &a; cptr2 = &b; dptr2 = &d; dptr2 = &e; As in C, the type of the pointer must match the type of the pointer variable. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 8

Pointers 0x0000122a 0x00001232 2.5 3.6 d e 0x0000123a 0x0000123e 0x00001242 0x00001246 iptr1 iptr2 cptr1 cptr2 0x0000124a 0x0000124e 0x1232 0x1220 0x1224 0x1228 0x1229 0x122a dptr1 dptr2 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 9

Pointers Generally, we do not care about the actual address. Represent a pointer as an arrow starting in a pointer variable and ending at the "pointee" (i.e., the thing being pointed to) iptr1 i pointer pointer variable "pointee" (i.e., the thing being pointed to) Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 10

Using Pointers How do we access the "pointee"? Use the unary * operator to dereference the pointer variable. E.g. double f = *dptr2; // set f to 3.6 cout << "iptr1 points to " << *iptr1 << endl; // displays 3 Note: need to be careful with char pointers, since C/C++ interprets them as null-terminated strings. Won't use them much. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 11

Using Pointers What happens when the following code is executed? // (a) pointee assignment *iptr1 = *iptr2; // (b) pointer variable assignment iptr1 = iptr2; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 12

Using Pointers iptr1 XX 3 4 i (b) (a) iptr2 4 j Can test two pointer variables for equality (==, point to same location) or inequality (!=). E.g. before statement (b) is executed, iptr1 == iptr2 is false, but afterwards in the situation shown above, iptr == iptr2 is true. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 13

Using Pointers Special value to represent not pointing to anything. Called null pointer and is written as 0 (not NULL). E.g. iptr1 = 0; Drawn in a few different ways: iptr1 iptr2 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 14

Dynamic Memory Allocation Pointing to an already existing variable is not very interesting. Power of pointers comes with dynamically allocated memory. I.e., memory allocated during program execution (aka runtime). Dynamically allocated memory comes from a heap, usually located at the other end of memory from the stack. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 15

Dynamic Memory Allocation In C++, dynamic memory allocation is done using the new operator. (DO NOT use malloc in this course.) It has the following syntax new <type>; This allocates an anonymous variable for one item of the given type and returns the address of (i.e., pointer to) that piece of memory. E.g., iptr1 = new int; iptr2 = new int; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 16

Dynamic Memory Allocation iptr1 iptr2 The anonymous variable is just like a statically allocated one, except that it has no name, so the only way to access the "pointee" is through dereferencing a pointer variable that points to it. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 17

Dynamic Memory Deallocation After the program is done using an anonymous variable, it needs to deallocate the variable. I.e., give it back to the system. This is done using the delete operator, which has syntax: delete <pointer var>; E.g. delete iptr1; // dealloc. the pointee delete iptr2; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 18

Dynamic Memory Deallocation iptr1 iptr2 Note delete deallocates the anonymous pointee, not the named pointer variable, as shown above. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 19

Garbage & Dangling References Note that if we execute iptr1 = iptr2; before doing the deletes, we lose the pointer to the first allocated space and cannot deallocate it. This is called a memory leak or garbage. And the fact that both iptr1 and iptr2 point to deallocated space is called dangling references. iptr1 iptr2 XX garbage dangling references Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 20

Dynamic Arrays Dynamically allocating single variables is not very interesting either. The real power is in dynamically allocating arrays. C++ syntax is: <type> *ptr = new <type>[<size>]; The size can be any positive integer expression including a variable. E.g. cin >> n; // assume user enters 5 int *intarrptr = new int[n]; intarrptr [0] [1] [2] [3] [4] Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 21

Array Addressing Back up a bit. Here is a statically allocated array: int array[5]; array [0] [1] [2] [3] [4] As noted before, the name of an array is the address of the first element of the array. Also, pointer arithmetic is scaled: (array+0) // address of array[0] (array+1) // address of array[1] (array+2) // address of array[2] // etc. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 22

Array Addressing We can dereference these addresses to access the elements, meaning that: *(array+0) == array[0] *(array+1) == array[1] // etc. This also is true for dynamic arrays using the pointer variable to provide the pointer to the first element of the dynamic array. *(intarrptr+0) == intarrptr[0] *(intarrptr+1) == intarrptr[1] // etc. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 23

Dynamic Arrays Thus the syntax for accessing a dynamic array is exactly the same as for a static array. E.g., for (int i = 0; i < n; i++) intarrptr[i] = 0; Can even say intarrptr = array; Must tell the compiler when deallocating an array by giving [ ] after delete. E.g., delete [] intarrptr; Note: the [ ] is empty, and delete must match up with corresponding new Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 24