Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.

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.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
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.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
CSE 332: Memory management with C++ classes Memory Management with Classes Review: for non-static built-in (native) types –default constructor and destructor.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
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 // 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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
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
CS148 Introduction to Programming II
Class: Special Topics 2 For classes using memory allocation
Dynamic Memory CSCE 121.
SPL – PS3 C++ Classes.
Presentation transcript:

Dynamically Allocated Arrays December 4, 2013

Skip the Rest of this PowerPoint

Next Time Read Chapter 8,pp

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

Another Example int* p = new int[10]; for (int i = 0; i< 10; i++) { p[i] = i * i; } cout << *p << " " << *(p+1) << " " << p[2] << endl;

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(Skip) Assignment Operator copies objects  message2 = message;  Copies a pointer, not the array Example in message.cpp  Messy error.

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

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

Copy Constructor(Skip) 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