CMSC 341 Prof. Michael Neary

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Winter 2015 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Introduction and Overview.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
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.
Pointers OVERVIEW.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Memory Management in Java Mr. Gerb Computer Science 4.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Memory Management.
Object Lifetime and Pointers
Dynamic Storage Allocation
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Processes and threads.
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Introduction to Classes
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Advanced Programming Behnam Hatami Fall 2017.
Dynamic Memory Allocation Reference Variables
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
CSC212 Data Structure - Section FG
15-110: Principles of Computing
Introduction to Computer Systems
Introduction to Classes and Objects
Dynamic Memory.
COP 3330 Object-oriented Programming in C++
Lecture 4: Instruction Set Design/Pipelining
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CMSC 341 Prof. Michael Neary Machines & Memory CMSC 341 Prof. Michael Neary

Quick note about email Use a meaningful subject! CMSC 341 section #XX [ISSUE] I get a lot of email, so I’ll see your message faster if you take advantage of my filters.

Overview What does it mean to compute? Applied vs Theoretical Motivate later classes on asymptotic analysis Fetch-Execute Cycle Memory Hierarchy RAM Model of Computation C++ Dynamic Memory and Pointers

Theoretical Computation Automata What abstract models can be derived to solve problems? Computability Can a problem actually be solved with a computer? Computational Complexity How efficiently can a problem be solved?

Complexity We focus on efficiency in 341 Asymptotic Analysis First class where you need to start caring! Asymptotic Analysis Describe efficiency independent of machines Why?

“Applied” Computation When you think about a computer…? CPU, RAM, Hard Disk Interactivity (Mouse, Keyboard, Display, etc.) Not really concerned with anything else Except price, aesthetic IBM PC – 1975 -- $19,975 (91,008.77 in 2017 money)

Memory Hierarchy

Machine Computation Fetch-execute cycle Fetch instruction from memory Decode instruction to machine code Execute instruction Result stored

Decoding Instructions Compile C++ to Assembly Assembly code is incredibly basic, but standardized for most systems Translate Assembly to Machine code Machine code is executed Dependent on CPU Get instruction set from manufacturer Intel

 

Execution Time How do I measure how fast my algorithm runs? Real-world time Incredibly Difficult Different hardware components Clock speed, latency Operating System , memory hierarchy Instruction sets are not standardized Count operations Keep track of clock-cycles needed for each operation Certainly feasible, still too much work

RAM Model Random Access Machine Model of computation Simple operations take one time step =, +, -, *, / bools, function call Loops are not simple Memory access takes one time step

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

Stack Memory automatically allocated by program Restricted in size Local variables Function calls Restricted in size Relatively small Fixed at compile time Fast access!

Heap Memory allocated at run-time NOT managed by the program Managed by the programmer (you) Useful when you cannot determine the max memory needed 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

Declaring Variables (Stack vs Heap) Stack variable declaration What you’ve been doing all along type arrayname [size]; int x = 6; Heap variable declaration Must use a pointer type *arrayname = new type[size];

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;

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

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?

(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

Dynamic Memory and Classes

Dynamically Allocating Instances Stack: Date today; Heap: Date *todayPtr = new Date();

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?

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;

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

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

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

Calling Destructors Stack Heap Student GV37486; Destructor automatically called at return 0; Heap Student *FY18223 = new Student(); Destructor is called when memory is freed delete FY18223; FY18223 = NULL;

Takeaways So many abstraction layers! Make efficient choices in your data structures Experiment with pointers so you understand

Reminders Project 0 and HW1 are out Piazza is available Course schedule should be up tomorrow Lecture Slides will go up soon