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

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

Pointers, Dynamic Data, and Reference Types

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Pointers II. Class 31

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3

Object-Oriented Programming for Scientific Computing

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

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

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

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

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

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS201 Some Important Definitions

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

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.

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

04-17 Discussion Notes

Homework #3 CS2255 Fall 2012


Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Vector and Free Store (Vectors and Arrays)

Ch. 11: References & the Copy-Constructor. - continued -

CPSC 427: Object-Oriented Programming

Class Information ANNOUCEMENTS

a data type is Types

Short Notes of CS201

Intermediate Programming, Spring 2017*

CS201- Introduction to Programming Current Quizzes

Intermediate Programming, Spring 2017*

Exam 3 Chapters 7 & 9

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

CS201 - Introduction to Programming Glossary By

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

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

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

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CSC1322 Object-Oriented Programming Concepts

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

Variables, Memory and Pointers

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Pointers and Dynamic Memory Allocation

Lecture 7: Binding Time and Storage

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

Launchpad Lecture -10

CS 11 C track: lecture 5

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

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

CS24 Week 3 Lecture 1

Separate Compilation Model

CSC 1600 Memory Layout for Unix Processes"

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

CS61C : Machine Structures

Understanding Pointers

Programming Abstractions

1. Which of the following best describes the situation after Line 1 has been executed?

C++ for Java Programmers

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

Pointers and References

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Pointers! Arizona State University 1

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

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

Dynamic Allocation of Memory

CS31 Discussion 1E Spring 17 : week 08

Vector and Free Store (Pointers and Memory Allocation)

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Array Elements as Function Parameters

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

Reference operator (&)

Midterm Review. PIC 10B Spring 2018

Memory and C++ Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Ch. 12: Operator Overloading

Pointers and Memory 1

Pointers. Developed By Ms. K.M.Sanghavi

Lecture 15a Persistent Memory & Shared Pointers

Goals of this Lecture

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

CPSC 427: Object-Oriented Programming

[0569] p 0318 garbage

Dynamic Allocation in C

Consider the program...

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CS 101: Computer Programming and Utilization

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Transcription:

CSCI 262 Data Structures 9 Dynamically Allocated Memory POINTERS AND ARRAYS 2 Arrays Arrays are just sequential chunks of memory: Arrays and Pointers Array variables are secretly pointers: x19 x18 x17 x16 x15 x1 x13 x12 x11 x1 o e H 22 253 17 88 char s[5] = {'H','e','l','l','o'}; x19 x18 x17 x16 x15 x1 x13 x12 x11 x1 o e H 16 s[] p char s[5] = {'H','e','l','l','o'}; char *p = s; cout << *p << endl; // prints 'H' cout << *s << endl; // also prints 'H' xfff xfff 3 Arrays and Pointers Pointers are also secretly array variables: x19 x18 x17 x16 x15 x1 x13 x12 x11 o e H 16 s[] p char s[5] = {'H','e','l','l','o'}; char *p = s; for (int j = ; j < 5; j++) { cout << p[j] << endl; } To the compiler: p[j] == *(p + j) POINTER ARITHMETIC x1 xfff 5 6 1

Pointer Arithmetic: char Suppose: char s[] = {'H','e','l','l','o'}; char* p = s; We ve stated that: p[j] == *(p + j) Another way to look at it is: p[j] == s[j] when p == s Thus: p[]=='h', p[1]=='e', p[2]=='l', etc. Pointer Arithmetic: int Now, suppose we have: int arr[] = {2, 17, 33, 6}; int* q = arr; It can be demonstrated that: q[j] == *(q + j) == arr[j] This implies that: q[1] == *(q + 1) == arr[1] == 17 Then q + 1 is not simply 1 byte address beyond q, but must be bytes beyond q. 7 8 Pointer Arithmetic Pointer arithmetic depends on type char* p p++ advances by 1 byte int* q q++ advances by bytes (size of int) You can add or subtract: q-- go back bytes q + 3 q plus 12 bytes Just keep in mind the array/pointer equivalence: sometype* ptr; *(ptr + k) == ptr[k] i.e. (ptr + k) == &(p[k]) // k * sizeof(sometype) // bytes after ptr Miscellaneous POINTER NOTES 9 1 C-style Strings In C, strings are simply arrays of char: char *s = "Hello!"; // valid in C; for C++ add const This array has size 7, not 6: the last entry stores byte value, or '\': Pointers and Reference Parameters Reference parameters are not pointers! Reference parameters are not pointers! Reference parameters are not pointers! 'H' 'e' 'l' 'l' 'o' '!' '\' Without this value, there s no way to detect the end of a string! With it, though, we can do: for (char* p = s; *p!= '\'; p++) { } String literals in C++ are still stored this way, but convert to the string type: string foo = "Hello"; or string("hello") If you have a function void foo(int &x) { } Inside foo, you cannot do *x = 1; // incorrect! You just do x = 1; // correct Sources of confusion: & denotes a reference parameter & also used as address-of operator References use pointers under the covers 11 12 2

Array Limitations in C++ Standard C++ does not let you do this*: DYNAMIC ARRAY ALLOCATION int sz; cout << "What size do you need?" << endl; cin >> sz; int arr[sz]; // compiler error *Strangely, later versions of C do allow this. Confusingly, so does g++ (some versions). 13 1 Dynamic Array Allocation So what if you know you ll need an array, but not the size (at compile time)? Where Does Memory Come From? Stack The stack: local variables, function arguments, return values. Grows down. int sz; cout << "What size do you need?" << endl; cin >> sz; int *arr = new int[sz]; Note that new gives us a pointer to our memory. Heap Data Segment Text Segment The heap: dynamically allocated memory (using new). Grows up. Global and static variables, constants. Program code. Read only! 15 16 Data Segment/BSS Global and static variables: Only ever one instance of them Get stored in their own special area Memory is pre-allocated, fixed in size The Stack Holds stack frames aka activation records Each function call results in a new stack frame Each stack frame contains memory for: Local variables declared in the function Parameters passed into function Return address for function When the function is exited, all of this memory is returned to the stack automatically. 17 18 3

A big ol hunk of memory! The Heap Get pieces of it ( allocate memory ) using new Pieces stay allocated until explicitly released by use of delete Heap memory has a lifetime independent of scope it can be used after a function that created it returns. You can t do that with local variables! Stack vs Heap Stack: local aka automatic variables and arrays: int z; foo f; double darray[1]; Heap: dynamically allocated objects and arrays: int* p = new int; foo* fp = new foo; double* dptr = new double[1]; Memory for these is allocated on the stack when they come into scope, is returned to the stack when they go out of scope (e.g., when function returns). All of these live on the heap. They will exist until explicitly deallocated by user code. 19 2 Dynamic Arrays Allocate dynamic arrays using new: double *darray = new double[12]; Use the array pointer just like a regular array: for (int j = ; j < 12; j++) darray[j] = j; Always clean up (deallocate) when you are done: delete[] darray; DYNAMIC OBJECT ALLOCATION 21 22 Pointers, Objects, and Dynamic Memory Consider this simple class: class student { public: string name; student() { ; } student(string n) { name = s; } void eat(); void sleep(); }; Creating New Objects: Stack If we want to create a student locally: student student1; student student2("kirk"); These are created on the stack. They will vanish when exiting the current scope. student1 is created using the default constructor: student(); student2 is created using another constructor: student(string s); 23 2

Creating New Objects: Heap We can also create single objects dynamically: Pointers! student* sp1 = new student; student* sp2 = new student("picard"); These are created on the heap. They will live forever unless deleted: delete sp1; delete sp2; Note, again, the two different constructors. Note no square brackets when calling delete on a single object. Working With Object Variables Consider: student student1; student* p = new student; We know that we can do: student1.name = "Sisko"; student1.eat(); What can we do with p? 25 26 Working with Object Pointers We have: student* p = new student; We could just dereference (perfectly fine!) (*p).name = "Janeway"; (*p).sleep(); Note that this won t work correctly: *p.name = "Janeway"; The. has higher precedence than * C++ gives us another operator we can use directly: p->name = "Archer"; p->sleep(); The Destructor The counterpart to the constructor: No return type Name is ~ followed by class name, e.g., ~student(); Never takes a parameter! The destructor is called automatically when: A local (stack allocated) object goes out of scope delete is called on a dynamically allocated object 27 28 Arrays of Objects We can also use new to create arrays of objects: int n = 1; student* arr = new student[n]; The default constructor is used to create every object in the array. Now we can do, e.g.: for (int i = ; i < n; i++) arr[i].gpa =.; As with base types, we use delete[] on dynamically allocated arrays of objects: delete[] arr; The destructor is called on every object in the array. WRAPPING UP 29 3 5

Dynamic Memory Don ts Never: Dereference a pointer which has not been set to valid memory (using new or &) Dereference a pointer to memory which has already been deallocated (a dangling pointer) Change or lose a pointer which is pointing to dynamically allocated memory (or you won t be able to deallocate this causes a memory leak) Use delete on a pointer which isn t pointing to dynamically allocated memory (e.g., a dangling or NULL pointer) Up Next Friday, February 9 Lab 5 TBD APT 3 Due Project 2 Postfix Calculator assigned (due in 2 weeks) Monday, February 12 Midterm Review Lab 5 Due Wednesday, February 1 Midterm 1 (in class) 31 32 6