Dynamic Allocation of Memory

Similar documents
Dynamic Allocation of Memory

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Scope and Parameter Passing

Scope and Parameter Passing

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

List Iterator Implementation

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

CS201- Introduction to Programming Current Quizzes

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

In Java we have the keyword null, which is the value of an uninitialized reference type

Implementing Linked Lists

Friends and Unary Operators

The Class Construct Part 1

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

CA31-1K DIS. Pointers. TA: You Lu

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Recursive Linked Lists

The Critical-Path Algorithm

Stacks and their Applications

Building the Abstract Syntax Trees

Minimal Spanning Trees

Pointers, Dynamic Data, and Reference Types

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Traveling Salesman Problem Brute Force Method

Fundamental Data Types

Programming Languages

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Binary Tree Applications

LR Parsing - Conflicts

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Recursive Descent Parsers

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

The Class Construct Part 2

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Plurality-with-Elimination Method

Homework #3 CS2255 Fall 2012

C11: Garbage Collection and Constructors

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Magnification and Minification

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Graphics Pipeline

Programming Languages

Lectures 6/7 Pointers and Dynamic Arrays

XQuery FLOWR Expressions Lecture 35

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Scheduling and Digraphs

CS 11 C track: lecture 5

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

Pointers and References

Recognition of Tokens

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Class Information ANNOUCEMENTS

Density Curves Sections

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Pointer Data Type and Pointer Variables

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS24 Week 2 Lecture 1

Sampling Distribution Examples Sections 15.4, 15.5

o Code, executable, and process o Main memory vs. virtual memory

Solving Recursive Sequences by Iteration

Rotations and Translations

CS 161 Exam II Winter 2018 FORM 1

1. Which of the following best describes the situation after Line 1 has been executed?

The x86 Architecture

Inheritance: The Fundamental Functions

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

CSC 1600 Memory Layout for Unix Processes"

Object-Oriented Programming

Data Structures and Algorithms for Engineers

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

CMSC 341 Lecture 2 Dynamic Memory and Pointers

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

CSCI 171 Chapter Outlines

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

C10: Garbage Collection and Constructors

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Introduction to Compiler Design

Memory Management. CSC215 Lecture

CS 101: Computer Programming and Utilization

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

The Graphics Pipeline

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

CSC 1300 Exam 4 Comprehensive-ish and Structs

Transcription:

Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 1 / 29

1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 2 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 3 / 29

The Vectr Class We would like to design a class of objects that would be like arrays, but whose size could be declared at run time, not compile time. We will call this class Vectr. (There is already a vector class.) Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 4 / 29

Arrays vs. Vectrs Example (Arrays) const int cnstsize = 10; int varsize = 10; int a[cnstsize]; // Legal int b[varsize]; // Illegal a[5] = 123; // Legal a = b; // Illegal cout << a[5] << endl; // Legal cout << a << endl; // Illegal Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 5 / 29

Arrays vs. Vectrs Example (Vectrs) const int cnstsize = 10; int varsize = 10; Vectr v(cnstsize); // Legal Vectr w(varsize); // Legal v[5] = 123; // Legal v = w; // Legal cout << v[5] << endl; // Legal cout << v << endl; // Legal Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 6 / 29

Vectr Objects Non-empty Vectr size element 5 Empty Vectr size element 0 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 7 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 8 / 29

Allocation of Memory: C Style The malloc() Prototype void* malloc(int number-of-bytes); The library function malloc() allocates a specified number of bytes of memory and returns a pointer to it. Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 9 / 29

Allocation of Memory: C Style malloc() Usage int* iptr = (int*)malloc(sizeof(int)); Point* pptr = (Point*)malloc(sizeof(Point)); malloc() returns a pointer to the first byte of the allocated memory block. The returned pointer is a pointer to void. The returned pointer must be cast to the proper type. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 10 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 11 / 29

Deallocation of Memory: C Style The free() Prototype void free(void* ptr); The library function free() deallocates memory. Include the header file <cstdlib>. The pointer must contain an address that was previously returned by malloc(). Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 12 / 29

Deallocation of Memory: C Style The free() Usage int* iptr = (int*)malloc(sizeof(int)); *iptr = 123;. free(iptr); If the programmer does not call free(), then memory allocated by malloc() is automatically freed when the program exits. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 13 / 29

Dynamic Memory for Arrays: C Style Array Allocation with malloc() and free() int size = 20; int* iptr = (int*)malloc(size*sizeof(int)); iptr[5] = 123;. free(iptr); malloc() can be used to allocate memory for an array. Then free() will deallocate the memory. The computer remembers the size of the array. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 14 / 29

Example Example (Example) DynamicCArray.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 15 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 16 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 17 / 29

Allocation of Memory: C++ Style The new Operator Type* ptr = new Type; Type* ptr = new Type[size]; C++ introduced the new operator to replace malloc(). It can allocate memory for a single object. And it can allocate memory for an array of objects. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 18 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 19 / 29

Deallocation of Memory: C++ Style The delete Operator delete ptr; delete [] ptr; // Delete object // Delete array The delete operator will delete memory that was allocated by the new operator. delete can deallocate memory for a single object. And it can deallocate memory for an array of objects. The pointer must contain an address that was previously returned by new. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 20 / 29

Dynamic Memory for Arrays: C++ Style Array Allocation with new and delete int size = 20; int* iptr = new int[size]; iptr[5] = 123;. delete [] iptr; Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 21 / 29

Example Example (Example) DynamicC++Array.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 22 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 23 / 29

Memory Leaks Definition (Memory Leak) A memory leak occurs when all pointers to a block of allocated memory have been lost. Leaked memory cannot be accessed or reallocated; it is useless. Excessive memory leaks may cause the program to run out of usable memory and crash. Memory leaks should always be avoided. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 24 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 25 / 29

Dangling Pointers Definition (Dangling Pointer) A dangling pointer is a non-null pointer that points to unallocated memory. Dereferencing a dangling pointer may cause the program to crash. We do not necessarily avoid dangling pointers, but we must be careful. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 26 / 29

Avoiding Dangling Pointers It impossible to test a non-null pointer to see whether it is dangling. Always set pointers to NULL if they do not point to allocated memory. Then compare them to NULL to see whether they point to allocated memory. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 27 / 29

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 28 / 29

Assignment Assignment Read Sections 10.9-10.10. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 29 / 29