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.

Similar documents
Dynamic Memory Allocation

Pointers, Dynamic Data, and Reference Types

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

Pointers II. Class 31

Introduction to Computer Science Midterm 3 Fall, Points

Exam 3 Chapters 7 & 9

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

Homework #3 CS2255 Fall 2012

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CS201 Some Important Definitions

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

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

a data type is Types

COMP 11 Class 17 Outline

Pointers in C/C++ 1 Memory Addresses 2

C++ for Java Programmers

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

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

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

CMSC 202 Midterm Exam 1 Fall 2015

CS201- Introduction to Programming Current Quizzes

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

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

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

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Memory and Pointers written by Cathy Saxton

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

Chapter 7 Array. Array. C++, How to Program

C++ for Java Programmers

CS 103 Unit 11. Linked Lists. Mark Redekopp

Arrays. Week 4. Assylbek Jumagaliyev

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

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Variables, Memory and Pointers

Exercise 1.1 Hello world

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

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

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


MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

Dynamic Allocation of Memory

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays in C++ Instructor: Andy Abreu

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

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

Pointers. Developed By Ms. K.M.Sanghavi

Arrays and Linked Lists

Object-Oriented Principles and Practice / C++

Fast Introduction to Object Oriented Programming and C++

04-17 Discussion Notes

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Dynamic Allocation of Memory

ECE 15B COMPUTER ORGANIZATION

Memory, Arrays, and Parameters

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

C++ Final Exam 2017/2018

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

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

Object-Oriented Programming for Scientific Computing

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

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

Lecture on pointers, references, and arrays and vectors

Vector and Free Store (Vectors and Arrays)

Lecture 12. We have already used strings. Strings. Hello Class is a string.

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

Pointer Data Type and Pointer Variables

Kurt Schmidt. October 30, 2018

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

CS 161 Exam II Winter 2018 FORM 1

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.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

C Pointers. 6th April 2017 Giulio Picierro

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Goals of this Lecture

CS 103 Unit 11. Linked Lists. Mark Redekopp

Tokens, Expressions and Control Structures

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

CMPS 221 Sample Final

211: Computer Architecture Summer 2016

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

CS 103 Lab - Party Like A Char Star

COEN244: Class & function templates

C++ Arrays. C++ Spring 2000 Arrays 1

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

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

Transcription:

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

Consider the bubble function one more time - void bubble (int a[ ], int arraysize) int i, j; int temp; for (i = 0; i < (arraysize - 1); i++) for (j = 0; j < (arraysize - (i+1)); j++) if (a[j] < a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; This thing can only Sort arrays of int Type things! 2

What if we wanted to generalize the sorting function so that it worked with arrays of any simple type? Write a function template for it: template <typename T> void bubble1 (T a[], int arraysize) int i, j; T temp; for (i = 0; i < (arraysize - 1); i++) for (j = 0; j < (arraysize - (i+1)); j++) if (a[j] < a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; 3

and use the template function in your code: template <typename T> void bubble1(t a[], int arraysize); int main() double x1[5] = 5.5, 3.32, -1.7, 10.11, 2.2; int y1[7] = 9, 1, 6, -13, 22, 5, 15; int i; bubble1 (x1, 5); cout << " sorted contents of array x1: " ; for (i = 0; i < 5; i++) cout << " " << x1[i]; cout << endl << endl; bubble1(y1, 7); cout << " sorted contents of array y1: " ; for (i = 0; i < 7; i++) cout << " " << y1[i]; cout << endl; return 0; 4

Dynamic Arrays Standard C++ arrays are static things: Once created, they stay the same size the declaration const int SIZE = 5; double nums[size]; tells the compiler: that a block of memory big enough to hold 5 double values has to be allocated and the variable nums is to store the starting address of this block Standard C++ arrays don t know their own size Most C++ compilers do not by default check whether indices are in range at runtime Sometimes situations arise in which we don t know the size an array should be until runtime. We may want to enter the array s size at runtime use dynamic arrays 5

What do we need in order to have dynamic arrays? When your program is running, there is part of memory that is unused, and this space is called the heap or free store To deal with dynamic arrays, we need: a mechanism for acquiring new memory locations when they are needed the C++ operator new is used to request a block of memory at runtime a way to release memory locations when they are no longer needed the C++ operator delete is a request for the system to return memory to the heap where it can be used in later reallocation 6

The new operator syntax: new int[6]; new double[10]; what does it do? requests allocation of a block of memory of proper size if the request can be satisfied, new returns the starting address of the block of memory usage: char * cptr; cptr = new char[20]; // cptr is a pointer to chars // now cptr points to // a block of memory big // enough for 20 chars 7

Example Here we first make an array of integers 5 long, give it values and print it int *array1 = NULL; Then we deallocate it int size = 5; Then we do the same thing for an array of size 10 array1 = new int[size]; for (int i = 0; i < size; i++) array1[i] = i; cout << "value of array1 is " << array1 << " and size is " << size << endl; for (int i = 0; i < size; i++) cout << "i = " << i << " and array1[i] = " << array1[i]<< endl; cout << endl; delete[] array1; array1 = NULL; size = 10; array1 = new int[size]; for (int i = 0; i < size; i++) array1[i] = i+25; cout << " value of array1 is: " << array1 << " and size is " << size << endl; for (int i = 0; i < size; i++) cout << "i = " << i << " and array1[i] = " << array1[i] << endl; cout << endl; Notice that the array is referenced through its pointer just as though it were a static array, but it isn t 8

Example: Words of varying length int length; char * word = NULL; char ans = 'y'; while (ans == 'y') cout << "Enter the word length: "; cin >> length; word = new char[length + 1]; for (int i = 0; i < length; i++) cin >> word[i]; word[length] = '\0'; cout << endl << "The word entered is: " << word; delete [] word; cout << endl << "Want to enter another word? "; cin >> ans; Notice that this code is not particularly robust! It s really easy to break!! What happens if this statement is not there? 9

A Little More on Arrays and Pointers Arrays and pointers are very closely related concepts Consider the following code snippet int numbers[25]; int *p; We ve declared an array of 25 integers and a pointer to int Now consider the following statement p = numbers; The assignment is perfectly legal But this one is not: numbers = p; The array name is a constant pointer can t be changed 10

Why are Pointers Important? You used reference parameters and references last semester, so why bother with pointers? Pointers to data improve performance for repetitive operations such as traversing strings and lists it is much cheaper in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point Pointers are used to hold the addresses of entry points for called functions and for run-time linking to dynamic link libraries (DLLs) In Object-oriented programming, pointers to functions are used for binding methods Basic concept in polymorphism the single most powerful contribution of Object Oriented programming 11

One More Example: Arrays of C-strings More Dynamic Data char aword[25]; char* words[50]; int n = 0; // number of words cout << "enter a set of words; terminate by entering #" << endl; while (cin >> aword) if (aword[0]!= '#') int len = strlen(aword) + 1; // amount of space needed char * space = new char[len]; // allocate enough space strcpy (space, aword); // copy into the new space words[n] = space; n++; else break; cout << endl << "The number of words entered is: " << n << endl; for (int i = 0; i < n; i++) cout << endl << words[i]; Note that there is still the constraint that there be no more than 50 words in this example. 12

Need for Arbitrary Sized Arrays The fixed size of arrays is a problem what if we want to do something as simple as read a set of integers and print them, but we have no clue how many integers there are? The following code breaks if there are more than 1000 integers entered: const int MAX = 1000; int a[max]; int j = 0; while (cin >> a[j]) j++ for (int i=0; i<j; i++) cout << a[i] << endl; How can we find a way around this problem? 13

Expanding an Array Solve the problem using dynamic allocation: int max = 10; int* a= new int[max]; int j = 0; while (cin >> a[j]) j++; if (j >= max) max = max*2; int *temp = new int[max]; for (int k=0; k<j; k++) temp[k] = a[k]; delete [] a; a = temp; for (int i=0; i<j; i++) cout << a[i] << endl; // make bigger array // copy into new array // free old array memory What can we say about the scope of the variable temp? 14