CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

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.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Memory Management Issues, Solutions, and Examples.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Learners Support Publications Operators.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear.
Memory Management in Java Mr. Gerb Computer Science 4.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
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.
Standard Version of Starting Out with C++, 4th Edition
Linked lists.
CSCI206 - Computer Organization & Programming
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Local Variables, Global Variables and Variable Scope
Dynamic Memory.
Dynamic Memory Allocation
Linked lists.
Presentation transcript:

CMSC 202 Computer Science II for Majors

CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions

CMSC 202UMBC Memory Management Operators Control allocation and deallocation of memory for any data-type Used when we don’t know in advance how much memory space is needed C++ defines two unary operators – new and delete for allocating and deallocating memory

CMSC 202UMBC Memory Management Operators …cont new operator General Form: pointer-variable = new data-type pointer-variable is pointer of type data-type data-type is any valid data type (including objects)

CMSC 202UMBC Memory Management Operators …cont Example 1. float *floatPtr; floatPtr = new float; 2. int *arrayPtr; arrayPtr = new int[10]; 3. Time *timePtr; timePtr = new Time;

CMSC 202UMBC Memory Management Operators …cont Consider pointer to array example It creates a memory space for an array of 10 integers arrayPtr[0] refers to first element, arrayPtr[1] refers second element… Multidimensional arrays int **arrayPtr = new int[5][4];

CMSC 202UMBC Memory Management Operators …cont Consider the Time object example new creates an object of proper size of type Time Calls the default constructor Returns a pointer to Time object Initialize a newly created object Time *timePtr = new Time (12,0,0);

CMSC 202UMBC Memory Management Operators …cont delete operator General Form: delete pointer-variable For arrays delete [ ] pointer-variable

CMSC 202UMBC Memory Management Operators …cont delete destroys dynamically allocated object and frees memory space Example delete floatPtr; delete [ ] arrayPtr; delete timePtr; Don’t forget to use delete [ ] for arrays

CMSC 202UMBC Memory Management Operators …cont new *intPtr = new int (12);.. delete intPtr; While deallocating memory, following happens implicitly: Value of intPtr is checked against NULL that is zero If intPtr is not NULL, the memory segment to which intPtr points is deallocated

CMSC 202UMBC Memory Management Operators …cont Memory leak for (int i=0; i < 10; i++) { int *intPtr = new int( 256 ); //... Some code } intPtr is internal to for loop, it doesn’t exist outside the loop Thus link to memory allocated is permanently lost – Memory Leak Underlines the importance of matching new and delete

CMSC 202UMBC Memory Management Operators …cont Segmentation Fault int *intPtr; for (int i=0; i < 10; i++) { intPtr = new int( 256 ); //... Some code delete intPtr; } //... delete intPtr; // segmentation fault Beware of such faults … core dump files

CMSC 202UMBC Memory Management Operators …cont Good programming practice  Initialize pointer to NULL after declaration and after delete int *intPtr = NULL; for (int i=0; i < 10; i++) { intPtr = new int( 256 ); //... Some code delete intPtr; intPtr = NULL; } //... delete intPtr; intPtr = NULL;

CMSC 202UMBC Dynamic Memory Variable representation int num; num : int n[5]; n :

CMSC 202UMBC Dynamic Memory struct sampleStruct { int p; char c; }; sampleStruct

CMSC 202UMBC Dynamic Memory … cont Pointer int *intPtr = new int (12); intPtr : 12

CMSC 202UMBC Dynamic Memory … cont Consider the code int *foo = new int* [4]; *foo[0] = 17; foo[1] = NULL; foo[2] = foo[1]; *foo[3] = 42; What would be the memory picture generated ?

CMSC 202UMBC Dynamic Memory … cont foo: 17 42

CMSC 202UMBC Dynamic Memory … cont 12 Memory Picture Which code generates this picture ? NBA