Dynamically Allocated Arrays May 2, 2011. Quiz 5 Today.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
CSE 332: Memory management with C++ classes Memory Management with Classes Review: for non-static built-in (native) types –default constructor and destructor.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Memory Management.
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Memory Management with Classes
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Dynamic Memory CSCE 121 J. Michael Moore.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Constructors and Destructors
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Essential Class Operations
CS148 Introduction to Programming II
C Programming Lecture-8 Pointers and Memory Management
Class: Special Topics 2 For classes using memory allocation
Essential Class Operations
Dynamic Memory CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS3 C++ Classes.
Presentation transcript:

Dynamically Allocated Arrays May 2, 2011

Quiz 5 Today

Intro to Pointers A pointer is a variable that holds an address Declaration uses * int* p; int i; & is “Address of” operator. Gets the address of a variable. p = &i;

Intro to Pointers * is used to dereference a pointer: *p = 15; Above is that what p points to is assigned the value, 15. pointers.cpp

Arrays Are Pointers In a program, an array, a, and a pointer, p, are the same kind of pointer. int* p; //p is an int pointer int a[10]; They both point at an int.

Arrays Are Pointers p can get the address that a is pointing at. p = a; It is illegal to change a. a = p; Once p is assigned the value of a it can be used like an array variable. arrayPtr.cpp

Dynamically Allocated Arrays Sometimes the amount of memory needed by an array can vary greatly.  e.g. Number of campers in Ponderosa park in January vs. August. To save memory, use dynamic array.

Dynamically Allocated Arrays Declare using a pointer Can return to memory Can vary size //allocate array with 20 ints int *pt = new int[20];

Initialize Array double *buff = new double[10]; for (int i=0; i<10; i++)‏ { *buff = 100.0; buff++; }

Initialize Array Alternative double *buff = new double[10]; for (int i=0; i<10; i++)‏ { buff[i] = 100.0; }

Release memory delete []pt; delete []buff; If an array is no longer needed this can free up memory for other programs.

NULL Can assign a pointer to a NULL value, which is pointing to nothing.

Memory Leak void myfunction( ){ int *pt; pt = new int[100];. //no delete } //in main: while(cond.)‏ myfunction();

Destructors Return data memory  When object goes out of scope. Look at messageDest.cpp

Copying Objects with Dynamic Arrays Assignment Operator copies objects  message2 = message;  Copies a pointer, not the array Example in message.cpp  Messy error.

Copying Objects with Dynamic Arrays Deep Copying  Create a new array and copy contents to the array messageFixed.cpp  copyFrom function fixes

Overload == To make sure no problems happen, overload == Do deep copy when an assignment is done.

Copy Constructor Copying of objects occur:  Passing a copy of an argument using pass-by- value.  Returning an object from a function: return msg1; By default these are shallow copying. Better to provide a copy constructor that does a deep copy. messageCopyConstr.cpp

Copy Constructor Odd things can happen without copy constructor. If two objects point to same array, something done to one object effects the other.  Like the problem with message

Next Time Review For Final  Info is on website.  Topics don't include linux, vim nor hardware.