COMP 11 Class 17 Outline

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

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.

LAB 4.1 Relational Operators and the if Statement

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Exam 3 Chapters 7 & 9

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

Variables, Memory and Pointers

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

Computer Programming

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

a data type is Types

Computer Programming

6.S096 Lecture 2 Subtleties of C

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

Computer Programming

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

CS 222: Pointers and Manual Memory Management

FORM 2 (Please put your name and form # on the scantron!!!!)

Lecture 15a Persistent Memory & Shared Pointers

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

Pointers, Dynamic Data, and Reference Types

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

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

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

COMP 524 Spring 2018 Midterm Thursday, March 1

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

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Computer Programming

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

CMSC 341 Lecture 7 Lists

Computer Programming

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Dynamic Allocation of Memory

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Welcome! COMP s1. Programming Fundamentals

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

Lecture 2, September 4

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Memory Allocation

CS 11 C track: lecture 5

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

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

CS 103 Unit 11. Linked Lists. Mark Redekopp

CSCE 2014 Final Exam Spring Version A

University of Toronto

INTRODUCTION TO COMPUTER SCIENCE - LAB

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS201- Introduction to Programming Current Quizzes

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

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

Dynamic Allocation in C

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

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

Programming in C/C Lecture 2

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

CSCE 2004 Midterm Exam Spring 2017

Arrays. Week 4. Assylbek Jumagaliyev

University of Toronto

Dynamic Allocation in C

COMP Introduction to Programming Midterm Review

Introduction to Computer Science Midterm 3 Fall, Points

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

C++ PROGRAMMING SKILLS Part 4: Arrays

CS 61c: Great Ideas in Computer Architecture

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)

Memory and C++ Pointers

Pointers II. Class 31

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Tema 6: Dynamic memory

Linked lists Tutorial 5b

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Memory and Pointers written by Cathy Saxton

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

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

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.


UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

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

Transcription:

COMP 11 Class 17 Outline Topics: Dynamic Arrays and Memory 2 Approach: Main Ideas: Discussion, Explanation, Discussion Memory allocation and pointers 1. Admin a) Sentegy Study Announcment b) Project 2A hand out c) Midterm exam grading issues d) Reading assignment, chapters 9.1 and 9.2 2. Recap on previous lecture and the lab a) Dynamic arrays i. Addressing the problems of (a) exact memory size, (b) persistent memory, (c) changing memory requirement, (d) allocating from the heap instead of the stack b) The concept of hermit crab i. Find a larger shell ii. Move all the stuff iii. Delete the old shell c) Syntax: new and delete i. Example using new and delete (new.cpp) ii. See comparison chart 3. Today s problem: dynamically allocated memory in general a) Remember: For every new, there must be a delete!!! b) A visual explanation of dynamic array (new.cpp) c) The concept of memory leaking i. Using valgrind to see what happens without the delete (new.cpp) d) Some advantages of using dynamic array: i. Allocate just enough memory to fit your need (efficiency) ii. Returning memory from function (new2.cpp) 1. Pointer diagram iii. Arrays that can change size! (new3.cpp) 1. Introducing NULL (memory address of: 0x00) 2. Passing pointers to change values 3. Returning new memory that is allocated from a function 4. Who is responsible for deleting? 5. Pointer diagram iv. Discussion: efficiency of new and delete is terrible 1. How can we make new3.cpp better? (new3-b.cpp) 2. Pointer diagram 4. New/delete with basic variable types and structs (new4.cpp) a) Difference between delete and delete [] 5. Exercise: Allocating a two-dimensional array a) Deleting a two dimensional array b) Pointer diagram c) How about a three-dimensional array? 6. Stack vs. Heap

:::::::::::::: new.cpp ::::::::::::: int size = 5; int* tarray = new int [size]; int sarray[size]; tarray[i] = i; sarray[i] = i; cout << "tarray[" << i << "]: " << tarray[i] << endl; cout << "sarray[" << i << "]: " << sarray[i] << endl; cout << "size of the dynamic array: " << sizeof(tarray) << endl; cout << "size of the static array: " << sizeof(sarray) << endl; cout << "size of an integer: " << sizeof(int) << endl; cout << "size of an integer pointer: " << sizeof(int*) << endl; delete [] tarray; //delete [] sarray; :::::::::::::: new2.cpp ::::::::::::: int* allocatesomememory(int); int size = 5; int* returnedarray = allocatesomememory(size); cout << "array[" << i << "]: " << returnedarray[i] << endl; delete [] returnedarray; int* allocatesomememory(int size) { int* array = new int [size]; array[i] = i; return array;

:::::::::::::: new3.cpp ::::::::::::: void printarray (int*, int); int* growarraybyone (int*, int); int input; int* array = NULL; int arraysize = 0; char print = 'n'; //initializing the pointer to point to NULL while (1) { cout << "give me a positive integer, type -1 to exit: "; cin >> input; if (input < 0) { break; else { array = growarraybyone (array, arraysize); arraysize++; array[arraysize-1] = input; cout << "print the array? "; cin >> print; if (print == 'y') { printarray(array, arraysize); if (array!= NULL) { delete [] array; void printarray (int* array, int size) { cout << "array[" << i << "]: " << array[i] << endl; int* growarraybyone (int* currentarray, int size) { int* temparray = new int [size+1]; temparray[i] = currentarray[i]; if (currentarray!= NULL) { delete [] currentarray;

return temparray; :::::::::::::: new4.cpp ::::::::::::: struct Course { int coursenum; int numstudents; ; int* myint = new int; float* myfloat = new float; Course* mycourse = new Course; cout << "type in an int: "; cin >> *myint; cout << "type in a float: "; cin >> *myfloat; cout << "type in course num: "; cin >> (*mycourse).coursenum; cout << "type in num students: "; cin >> mycourse->numstudents; cout << "int: " << *myint << endl; cout << "float: " << *myfloat << endl; cout << "course, coursenum: " << mycourse->coursenum << endl; cout << "course, numstudents: " << (*mycourse).numstudents << endl; delete myint; delete myfloat; delete mycourse; :::::::::::::: new3-b.cpp ::::::::::::: void printarray (int*, int); int* growarrayifnecessary (int*, int*, int); int input; int* array = new int [1]; int arraysize = 1; int curindex = 0; char print = 'n'; while (1) { cout << "give me a positive integer, type -1 to exit: "; cin >> input;

if (input < 0) { break; else { array = growarrayifnecessary (array, &arraysize, curindex); array[curindex] = input; curindex++; cout << "print the array? "; cin >> print; if (print == 'y') { printarray(array, curindex); if (array!= NULL) { delete [] array; void printarray (int* array, int index) { for (i=0; i<index; i++) { cout << "array[" << i << "]: " << array[i] << endl; int* growarrayifnecessary (int* currentarray, int* size, int curindex) { int* temparray = currentarray; if (curindex == *size) { int oldsize = *size; int newsize = *size * 2; cout << "growing array from " << oldsize << " to " << newsize << endl; temparray = new int [newsize]; for (i=0; i<oldsize; i++) { temparray[i] = currentarray[i]; *size = newsize; if (currentarray!= NULL) { delete [] currentarray; return temparray;

POINTER SUMMARY address pointer variable & WHAT ISAPOINTER? TERMS AND OPERATORS Where in memory a variable is stored A variable that holds an address The C/C++ operator that finds the address of a variable example int x, *p; // create an int and a pointer to an int p = &x; // store address of x in pointer variable p * The C/C++ operator that takes an address and gives the variable example *p = 3; // store 3 in int pointed to by p WHAT CAN POINTERS POINT AT? a. Existing Variables b. Dynamically Allocated Variables // existing vars int x; char name[100]; struct Trip t1; int *p4 = new int; char *p5 = new char[100]; struct Trip *p6 = new struct Trip; int *p1 = &x; char *p2 = name; // arrays don t need & struct Trip *p3 = &t1; QUEST:HOW DOIDEREFERENCE POINTERS? ANS:DEPENDS ON TYPE OF POINTEE. Simple Variables Arrays Structs // create storage int x; float f; int *p1; float *p2; int *p3; // assign *p1 = &x; *p2 = &f; *p3 = new double; // use *p1 = 4; *p2 = 1.3923; *p1 += 2; *p3 = *p2 + *p2; // create storage struct Trip sched[10]; int temps[31]; struct Trip *p4; int *p5; float *p6; // assign p4 = sched; p5 = temps; p6 = new float[7]; // use p4[2].orig = "BOS"; p5[23] = 21; p6[2] = 29.23; // create storage struct Trip t1; struct Trip *p7; struct Person *p8; // assign p7 = &t1; p8 = new Person; // use p7->dest = "DFW"; p8->lastname = "Lee"; Mar 23, 2009 13:10