CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
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 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.
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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
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.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Yan Shi CS/SE2630 Lecture Notes
Object Lifetime and Pointers
Dynamic Storage Allocation
Overview 4 major memory segments Key differences from Java stack
Chapter 9: Pointers.
Motivation and Overview
ENEE150 Discussion 07 Section 0101 Adam Wang.
Checking Memory Management
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
8 Pointers.
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC 341 Prof. Michael Neary
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Given the code to the left:
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review) Based on slides by Shawn Lupoli and Katherine Ginson at UMBC

UMBC CMSC 341 Dynamic Memory and Pointers Today’s Topics Stack vs Heap Allocating and freeing memory new and delete Memory Leaks Valgrind Pointers Dynamic Memory and Classes Random Numbers UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Stack vs Heap Specific regions of memory Both are used to store variables Each behaves differently Each requires different variable declarations UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Stack Memory automatically allocated by program Local variables Function calls Restricted in size Relatively small Fixed at compile time Fast access! UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Heap Memory allocated at run-time NOT managed by the program Managed by the programmer (you) Must use pointers to access heap memory Variables on the heap can be resized Heap is much larger than the stack Access slower than the stack UMBC CMSC 341 Dynamic Memory and Pointers

Declaring Variables (Stack vs Heap) Stack variable declaration What you’ve been doing all along type arrayname [size]; Heap variable declaration Must use a pointer type *arrayname = new type[size]; UMBC CMSC 341 Dynamic Memory and Pointers

Allocating and Freeing Memory

UMBC CMSC 341 Dynamic Memory and Pointers new and delete Used to dynamically allocate and free memory on the heap new must be assigned to a pointer variable int *calendar; calendar = new int[12]; delete can only be used on pointer variables delete[] calendar; UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Garbage Data Allocated memory is not cleared for you Memory was used previously Full of “garbage” data After new is used to allocated memory it must be initialized With “default” values (0, 0.0, ' ') With the correct values UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Garbage Data Example What does the following code print out? double *sodaPrice = new double[3]; sodaPrice[0] = 16.01; cout << sodaPrice[0] << endl; cout << sodaPrice[1] << endl; cout << sodaPrice[2] << endl; 16.01 1.1847636e-309 2.746349e-12 where are these numbers coming from? UMBC CMSC 341 Dynamic Memory and Pointers

(No) Garbage Collection C++ does not have garbage collection Allocated memory that is no longer needed must be de-allocated, or freed Use delete to do this Must free all allocated memory before end of program (return 0) Or earlier, if it’s no longer needed UMBC CMSC 341 Dynamic Memory and Pointers

Good Programming Practices Before memory is freed, clear it out Replace with meaningless or default values Must be done before delete is called After memory has been freed, set the pointer equal to NULL Must be done after delete is called Why do this? UMBC CMSC 341 Dynamic Memory and Pointers

Memory Leaks How to Find and Fix Them

UMBC CMSC 341 Dynamic Memory and Pointers Memory Leaks Occur when data is allocated, but not freed Calling new over and over, but never delete Not freeing new memory before exiting a function Access to the previous memory is lost The location of that memory was overwritten Eventually the program runs out of memory, and the program will crash UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Memory Leak Example int *arr, var = 1000; for (int i = 0; i < var; i++) { arr = new int[100000000]); } Heap ? arr UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Memory Leak Example int *arr, var = 1000; for (int i = 0; i < var; i++) { arr = new int[100000000]); } arr = new int[100000000] Heap arr = new int[100000000] arr = new int[100000000] arr = new int[100000000] arr = new int[100000000] arr arr = new int[100000000] UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Memory Leak Example int *arr, var = 1000; for (int i = 0; i < var; i++) { arr = new int[100000000]); } arr = new int[100000000] Heap arr = new int[100000000] arr = new int[100000000] arr = new int[100000000] arr = new int[100000000] arr arr = new int[100000000] UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Valgrind Assists with dynamic memory management Memory allocated using new And therefore on the heap Must compile with the -g flag (for debugging) Detects memory leaks and write errors Running valgrind significantly slows program down UMBC CMSC 341 Dynamic Memory and Pointers

Pointers - Review

UMBC CMSC 341 Dynamic Memory and Pointers Used to “point” to locations in memory int x; int *xPtr; x = 5; xPtr = &x; /* xPtr points to x */ *xPtr = 6; /* x’s value is 6 now */ Pointer type must match the type of the variable whose location in memory it points to UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Pointers – Ampersand Ampersand returns the address of a variable int x = 5; int *varPtr = &x; int y = 7; varPtr = &y; UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Pointers – Asterisk Asterisk dereferences a pointer to get to its value int x = 5; int *varPtr = &x; int y = *varPtr; UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Pointers – Asterisk Asterisk dereferences a pointer to get to its value int x = 5; int *varPtr = &x; int y = *varPtr; Also used when initially declaring a pointer (and in function prototypes), but after declaration the asterisk is not used: varPtr = &y; UMBC CMSC 341 Dynamic Memory and Pointers

Examples – Ampersand and Asterisk int x = 5; int *xPtr; [* used to declare ptr] xPtr = &x; [& used to get address] *xPtr = 10; [* used to set value] cout << &xPtr; [& used to get address] UMBC CMSC 341 Dynamic Memory and Pointers

Examples – Ampersand and Asterisk int x = 5; int *xPtr; [* used to declare ptr] xPtr = &x; [& used to get address] *xPtr = 10; [* used to set value] cout << &xPtr; [& used to get address] UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Pointer Assignments Pointers can be assigned to one another using = int x = 5; int *xPtr1 = &x; /* xPtr1 points to address of x */ int *xPtr2; /* uninitialized */ xPtr2 = xPtr1; /* xPtr2 also points to address of x */ (*xPtr2)++; /* x is 6 now */ (*xPtr1)--; /* x is 5 again */ UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers NULL Pointers NULL is a special value that does not point to any address in memory It is a “non” address Uninitialized pointers are like any new memory – they’re full of … what? Setting a pointer to NULL will prevent accidentally accessing a garbage address GARBAGE! UMBC CMSC 341 Dynamic Memory and Pointers

Pointer Visualization Exercise int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name x xPtr y memory address 0x7f96c 0x7f960 0x7f95c value 5 ? UMBC CMSC 341 Dynamic Memory and Pointers

Pointer Visualization Exercise int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name x xPtr y memory address 0x7f96c 0x7f960 0x7f95c value 5 ? UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Pointers and Arrays Arrays are built by pointers Array name contains address of first element char terry[6] = “hello”; UMBC CMSC 341 Dynamic Memory and Pointers

Dynamic Memory and Classes

Dynamically Allocating Instances Stack: Date today; Heap: Date *todayPtr = new Date(); UMBC CMSC 341 Dynamic Memory and Pointers

Dynamically Allocating Instances Stack: Date today; nothing – handled for you Heap: Date *todayPtr = new Date(); call delete and set pointer to NULL delete todayPtr; todayPtr = NULL; What to do when freeing memory? UMBC CMSC 341 Dynamic Memory and Pointers

Accessing Member Variables Stack (non-dynamic) Use the “dot” notation today.m_day = 2; Heap (dynamic) Use the “arrow” notation todayPtr->m_year = 2015; Can also dereference and use “dot” (*todayPtr).m_year = 2015; UMBC CMSC 341 Dynamic Memory and Pointers

Passing Class Instances Stack Normal variable; works as expected cout << x; Heap Need to dereference variable first cout << xPtr; // prints address cout << (*xPtr); // prints value UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Destructor All classes have a built-in destructors Created for you by C++ automatically Called when instance of class ceases to exist Explicit delete, or end of program (return 0) Classes can have member variables that are dynamically allocated Built-in destructors do not free dynamic memory! Must code one for the class yourself UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Coding Destructors Named after class, and has no parameters In source (.cpp file) Student::~Student() { // free array of class name strings delete classList; } In header (.h file) ~Date(); // denotes destructor UMBC CMSC 341 Dynamic Memory and Pointers

UMBC CMSC 341 Dynamic Memory and Pointers Calling Destructors Stack Student GV37486; Destructor automatically called at return 0; Heap Student *FY18223 = new Student(); Destructor is called when memory is freed delete FY18223; FY18223 = NULL; UMBC CMSC 341 Dynamic Memory and Pointers

Random Numbers

UMBC CMSC 341 Dynamic Memory and Pointers Pseudo Randomness Computers cannot be truly random Results are statistically random But deterministically produced To create “random” numbers, the random number generated must be seeded Given an initial starting value Current time is traditional seeding value Seed changes regularly UMBC CMSC 341 Dynamic Memory and Pointers

Creating Random Numbers Need to include two libraries cstdlib – contains functions for random time.h – to get current time (traditional seed) Must seed random number generator first srand(SEED_VALUE); srand(time(0)); // current time Now we can create random numbers UMBC CMSC 341 Dynamic Memory and Pointers

Generating Random Numbers For each random number you need, make a call to the rand function (no arguments) rand(); Returns a value between 0 and 32765 To get numbers within a specific range, use % For example: numbers in the range 101 to 200 num = (rand() % 100) + 101 between 0 and 32765 between 0 and 99 only between 101 and 200 UMBC CMSC 341 Dynamic Memory and Pointers