Dynamic Allocation of Memory

Similar documents
Dynamic Allocation of Memory

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. Fri, Jan 18, 2013

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

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

Scope and Parameter Passing

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

Implementing Linked Lists

Scope and Parameter Passing

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

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

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

Building the Abstract Syntax Trees

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

The Critical-Path Algorithm

Minimal Spanning Trees

List Iterator Implementation

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

Recursive Descent Parsers

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

Binary Tree Applications

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

CS 11 C track: lecture 5

Stacks and their Applications

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

CS24 Week 2 Lecture 1

Memory (Stack and Heap)

Fundamental Data Types

Friends and Unary Operators

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

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

The Class Construct Part 1

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

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

CS201- Introduction to Programming Current Quizzes

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

Dynamic memory allocation (malloc)

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

The Decreasing-Time Algorithm

CSCI 171 Chapter Outlines

C Structures & Dynamic Memory Management

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Density Curves Sections

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

Recursive Linked Lists

Sampling Distribution Examples Sections 15.4, 15.5

Introduction to Compiler Design

Homework #3 CS2255 Fall 2012

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Memory Management. CSC215 Lecture

Rotations and Translations

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

The Class Construct Part 2

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

Function Definition Syntax Tree

Dynamic memory allocation

Pointers, Dynamic Data, and Reference Types

LR Parsing - Conflicts

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

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

Inheritance: The Fundamental Functions

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

Lecture 8 Dynamic Memory Allocation

CSC 1600 Memory Layout for Unix Processes"

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Object-Oriented Programming

Memory Management I. two kinds of memory: stack and heap

Programming Languages

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

Programming Languages

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

The Graphics Pipeline

CS 161 Exam II Winter 2018 FORM 1

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

Scheduling and Digraphs

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Nondeterministic Programming in C++

The Pairwise-Comparison Method

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

Memory Allocation. General Questions

Pointers and References

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Recognition of Tokens

The Traveling Salesman Problem Brute Force Method

The Standard Template Library Classes

Quick review pointer basics (KR ch )

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Class Information ANNOUCEMENTS

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.

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

Transcription:

Dynamic Allocation of Memory Lecture 5 Section 9.8 Robb T. Koether Hampden-Sydney College Wed, Jan 24, 2018 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 1 / 34

1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 2 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 3 / 34

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 Wed, Jan 24, 2018 4 / 34

Arrays vs. Vectrs Arrays const int MAX_SIZE = 20; int size = 10; int a[max_size]; // Legal int b[size]; // Illegal a[5] = 123; // Legal a = b; // Illegal cout << a[5] << endl; // Legal cout << a << endl; // Legal, but not advised. Why? Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 5 / 34

Arrays vs. Vectrs Vectrs const int MAX_SIZE = 20; int size = 10; Vectr v(max_size); // Legal Vectr w(size); // 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 Wed, Jan 24, 2018 6 / 34

Vectr Objects Non-empty Vectr size element 5 Empty Vectr size element 0 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 7 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 8 / 34

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 Wed, Jan 24, 2018 9 / 34

Allocation of Memory: C Style malloc() Usage int* pi = (int*)malloc(sizeof(int)); Point* ppt = (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 Wed, Jan 24, 2018 10 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 11 / 34

Deallocation of Memory: C Style The free() Prototype void free(void* p); 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 Wed, Jan 24, 2018 12 / 34

Deallocation of Memory: C Style The free() Usage int* p = (int*)malloc(sizeof(int)); *p = 123; // Do stuff with p. free(p); 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 Wed, Jan 24, 2018 13 / 34

Dynamic Memory for Arrays: C Style Array Allocation with malloc() and free() int size = 20; int* p = (int*)malloc(size*sizeof(int)); p[5] = 123; // Do stuff with p. free(p); 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 Wed, Jan 24, 2018 14 / 34

Example Example (Example) DynamicCArray.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 15 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 16 / 34

The calloc() Function The calloc() Prototype void* calloc(int num-of-objects, int size-of-object); The library function calloc() allocates memory for a specified number of objects each of a specified size and returns a pointer to it. Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 17 / 34

The calloc() Function calloc() Usage int* pi = (int*)calloc(1, sizeof(int)); Point* ppt_arr = (Point*)calloc(50, sizeof(point)); calloc() 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 Wed, Jan 24, 2018 18 / 34

The realloc() Function The realloc() Prototype void* realloc(void* p, int num-of-bytes); The library function realloc() will allocate a new block of memory containing the specified number of bytes. The contents of the old memory will be copied to the new memory (as much as fits). Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 19 / 34

The realloc() Function realloc() Usage int* p = (int*)malloc(100*sizeof(int)); for (int i = 0; i < 100; i++) p[i] = 10*i; p = (int*)realloc(p, 200*sizeof(int)); The contents 0, 10, 20,..., 990 will be copied to the new memory. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 20 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 21 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 22 / 34

Allocation of Memory: C++ Style The new Operator Type* p = new Type; // For single object Type* p = new Type[size]; // For an array 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 Wed, Jan 24, 2018 23 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 24 / 34

Deallocation of Memory: C++ Style The delete Operator delete p; delete [] p; // Delete single object // Delete an 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 Wed, Jan 24, 2018 25 / 34

Dynamic Memory for Arrays: C++ Style Array Allocation with new and delete int size = 20; int* p = new int[size]; p[5] = 123; // Do stuff with p. delete [] p; Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 26 / 34

Example Example (Example) DynamicC++Array.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 27 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 28 / 34

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 Wed, Jan 24, 2018 29 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 30 / 34

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 Wed, Jan 24, 2018 31 / 34

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 Wed, Jan 24, 2018 32 / 34

Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 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 Wed, Jan 24, 2018 33 / 34

Assignment Assignment Read Section 9.8. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 34 / 34