Lecture 11 Memory Richard Gesick.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Merge Sort Algorithm rMerge sort has two phases. l First it divides the data into smaller and smaller.
Advertisements

Chapter 18 Vectors and Arrays
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 11 Memory Management C makes it easy to shoot.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Introduction to Programming Lecture 39. Copy Constructor.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
DSP Implementation Lecture 3. Anatomy of a DSP Project In VDSP Linker Description File (.LDF) Source Files (.asm,.c,.h,.cpp,.dat) Object Files (.doj)
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Run-Time Storage Organization
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
C and Data Structures Baojian Hua
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Memory Layout C and Data Structures Baojian Hua
Runtime Environments Compiler Construction Chapter 7.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
More C++ Features True object initialisation
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Memory in Computer Yung-Hsiang Lu Purdue University 1.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
1 Programming Languages (CS 550) List Processing, Dynamic Memory Allocation and Garbage Collection Jeremy R. Johnson.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CSC 533: Programming Languages Spring 2016
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Creating and Using Objects, Exceptions, Strings
CSC 533: Programming Languages Spring 2015
Unit-2 Objects and Classes
Understand Computer Storage and Data Types
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Advanced Programming Behnam Hatami Fall 2017.
Dynamically Allocated Memory
More Object Oriented Programming
CSC 253 Lecture 8.
CMSC 341 Prof. Michael Neary
Object References James Brucker.
CSC 253 Lecture 8.
Lecture 10 List Richard Gesick.
Dynamic Memory A whole heap of fun….
Memory Allocation CS 217.
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
CS2011 Introduction to Programming I Arrays (II)
Dynamic Memory Management
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
List Allocation and Garbage Collection
C H A P T E R F I V E Memory Management.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
How Memory Leaks Work with Memory Diagram
Dynamic Memory Management
Lecture 2 Memory management.
CSC 533: Programming Languages Spring 2019
Classes and Objects Object Creation
Run-time environments
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Lecture 11 Memory Richard Gesick

Memory There is a significant difference between value and reference types.  Value types live within the stack space and are accessible directly.  Reference types live in the heap and are allocated dynamically using the "new" command.  Primitive types like int, float, bool, etc. are value types.

Memory Consider the following code: int x, y; Random r = new Random(); Memory would look like this:

Memory Just as assignment works with value types, assignment makes a copy of the reference stored in one reference type and places that copy into the other reference type.  Imagine that there are two references to the same space in the heap as a result.

Memory The following code: Dog d1 = new Dog(); Dog d2; d2 = d1; Generates the following memory structure:

Garbage Collection When nothing is pointing to a space in the heap, then the Garbage Collector can come along and free up that memory for some other use.

Memory Model References can get as complicated as we'd like.  Consider the following: Player p = new Player(); p.Position = new Vector2(100,100); p.Weapons.Add(new Weapon("sword", new Vector(-1,-1))); p.Weapons.Add(new Weapon("wand", new Vector(-1,-1))); p.CurrentWeapon = p.Weapons[0]; // current weapon sword