Dynamic Memory Allocation

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
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.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
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.
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.
Object-Oriented Programming in C++
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
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.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers and Dynamic Memory Allocation
Object Lifetime and Pointers
Dynamic Storage Allocation
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers.
Pointers and Pointer-Based Strings
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
Chapter 10: Pointers Starting Out with C++ Early Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
Pointers and References
Object Oriented Programming COP3330 / CGS5409
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers.
9-10 Classes: A Deeper Look.
Pointers and Pointer-Based Strings
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Standard Version of Starting Out with C++, 4th Edition
Pointers and References
Pointers and References
Dynamic Memory CSCE 121.
Pointers and pointer applications
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Presentation transcript:

Dynamic Memory Allocation

Pointers Revisited What is variable address, name, value? What is a pointer? How is a pointer declared? What is address-of and dereference operators? How can a pointer be assigned a value? How can a value of a memory location referred to by a pointer be accessed? What is a constant pointer? What is a pointer to a constant? What is the relationship between array name and a pointer? What is null pointer? What is lose pointer problem? Can pointers be used with objects? How can a method be invoked on an object using pointer? What is -> operator?

Why Dynamic Memory When a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated Not all memory can be allocated or efficiently allocated at the compilation time we are manipulating an set of data of arbitrary size what if we allocate an array too small? what if we allocate an array too large? static memory allocation - memory allocation at compile time dynamic memory allocation - allocation at execution time Dynamic memory allocation is more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow heap - the system structure where the memory is allocated heap is separate for every program and it is removed when the program finishes

new and delete new and delete - operations are used to dynamically manipulate memory new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declare pointer ip = new int; // ip points to integer variable Note: the variable has no name and the only way to access the variable is though pointer: cin >> *ip; *ip += 20; cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the variable delete - releases memory back to heap so that it can be reused delete ip; Note: the memory pointed to by ip goes away not the pointer variable itself dynamic variable - variable created with new automatic variable – local variables, parameters, temp. variables

Memory Leak Problem Note: the pointer that points to the dynamic variable is the only way to access this variable If the pointer is reassigned the dynamic variable is “lost”. This is called a memory leak problem int *ptr = new int; ptr = new int; // ERROR - memory leak Is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int;

Pointers and Arrays Array name is equivalent to: base_type * const That is array name is a pointer that cannot be changed Array name points to the first element of the array Array name can be used as pointer Pointer can be used as an array name int a[10], *p1, *p2; p1=a; // where does p1 point now? p1[2]=5; // which element does p1 access a=p2; // is this legal? p1[20]; // is this legal?

Dynamic Arrays Arrays can be created dynamically just as non-aggregate variables new has to be passed the number of elements of the array int *p1, p2, num; p1 = new int[10]; The number of array elements need not be constant: cin >> num; p2 = new int[num]; new returns the pointer to the first element of the dynamically allocated array. This pointer can be used just like regular array name: p1[2]=42; Note: unlike regular array name this pointer can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1;