Dynamic Memory.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Informática II Prof. Dr. Gustavo Patiño MJ
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Basic Semantics Associating meaning with language entities.
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.
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.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Pointers and Dynamic Memory Allocation
Object Lifetime and Pointers
Pointers and Dynamic Arrays
Pointers and Dynamic Arrays
Overview 4 major memory segments Key differences from Java stack
Day 03 Introduction to C.
Introduction to Programming
Linked lists.
Day 03 Introduction to C.
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Global & Local Identifiers
Pointers and Dynamic Variables
Dynamically Allocated Memory
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
Overview 4 major memory segments Key differences from Java stack
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Destructors.
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointer Data Type and Pointer Variables III
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
DYNAMIC MEMORY MANAGEMENT
Pointers: The Basics.
Linked lists.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Dynamic Memory

Objective The efficient use of Memory Use no more than is needed at any given time.

Classifications of Memory 1. Static 2. Automatic 3. Dynamic Classified by: - When objects are allocated and deallocated - How objects are allocated and deallocated - The Memory Section on which objects reside

Static Memory Allocated: before main() begins Deallocated: after main() ends Section: Data Segment Example: Global Variables

Automatic Memory Allocated: when the declaration statement is executed inside a block Deallocated: at the end of the block in which it is declared Section: Stack Examples: Local Variables, Formal Arguments

Dynamic Memory Allocated: on demand by the programmer using the new operator. Deallocated: on demand by the programmer using the delete operator Section: Heap Examples: See below!

new operator Purpose: To allocate a data object "on demand" Syntax: pointer = new datatype; Semantics: 1. allocates an object of the datatype 2. points pointer to the new object

new operator Example 1: class frac { public: int num; int den;} void main() { frac * p; // p is automatic p = new frac; // new frac is dynamic p->num = 2; p->den = 3; }

new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; } Note the object does not have its own name!

new operator Example 1: class frac { int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

new[] operator Purpose: To allocate an array of data objects "on demand" Syntax: pointer = new datatype[intValue ]; The may be a variable, constant or literal. Once allocated, use as any Array. Semantics: same as new

new[] operator Example 2: void main() { int *p, num, i; cout << "How many numbers? "; cin >> num; p = new int[num]; for (i=0; i<n; i++) p[i] = 0; }

delete operator Purpose: To deallocate a data object "on demand" Syntax: delete pointer; Semantics: deallocates the data object pointed to by p. Does not set p to NULL!

delete operator Example 3: void main() { frac *p = new frac; ... delete p; // p is NOT set to NULL! p = NULL; // to be safe }

delete[] operator Purpose: To deallocate an array of data objects "on demand" Syntax: delete[] pointer; Semantics: deallocates the entire array of data objects pointed to by p. Does not set p to NULL!

delete[] operator Example 4: void main() { int *p = new int[5]; ... delete[] p; // deallocate all 5 ints p = NULL; // to be safe }

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

Garbage A dynamically allocated memory object that has no pointer pointing to it. It cannot be referenced, so it cannot be used and cannot be deallocated, but still exists in memory (wasting space). Also called a Memory Leak Don't make Garbage!!

Garbage Example 5: void main() { int *p, i=4; p = new int; *p = 6; p = &i; }

Dangling Reference A pointer to a data object that no longer exists Attempting to use a Dangling Reference will cause a run time error. Don't create Dangling References!!

Dangling Reference Example 6: void main() { int *p, *q; p = new int; q = p; *p = 6; delete q; q = NULL; cout << *p; // CRASH! }

Vocabulary Term Definition Static Memory that is allocated on the Data Segment before the program begins and deallocated after it ends. Ex: Global Variables Automatic Memory that is allocated on the Stack when the data object is declared and is deallocated at the end of the block in which it is declared. Dynamic Memory that is allocated on the Heap on-demand by the programmer (using the new operator), and deallocated on-demand by the programmer (using the delete operator) Garbage Dynamically-allocated data object that has no pointer pointing to it, and so can not be used or deleted. Dangling Reference A pointer to a data object that no longer exists (has been deleted).