Programming Abstractions

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamically Allocated Arrays May 2, Quiz 5 Today.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Pointers OVERVIEW.
C++ Memory Overview 4 major memory segments Key differences from Java
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Memory Management.
Pointer to an Object Can define a pointer to an object:
Dynamic Storage Allocation
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
CS 106X Arrays; Implementing a Collection (ArrayList)
Programming Abstractions
Overview 4 major memory segments Key differences from Java stack
CSE 374 Programming Concepts & Tools
Programming Abstractions
Programming Abstractions
Programming Abstractions
Programming Abstractions
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
CSCE 210 Data Structures and Algorithms
Pointers and Dynamic Variables
group work #hifiTeam
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation
CSC 253 Lecture 8.
CMSC 341 Prof. Michael Neary
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
CSC212 Data Structure - Section FG
Programming Abstractions
Indirection.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Chapter 16 Linked Structures
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.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Chapter 9: Pointers and String
Pointers and dynamic objects
COP 3330 Object-oriented Programming in C++
Data Structures and Algorithms Memory allocation and Dynamic Array
Pointers and References
Dynamic Memory CSCE 121.
Rule of Three Part 1 & 2.
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: Last week: Making your own class Arrays in C++ new/delete This week: Memory and Pointers First revisit some topics from last week in more detail: Deeper look at new/delete dynamic memory allocation Deeper look at what a pointer is Then new topics: Linked nodes Linked List data structure (if we have time) Binary tree data structure Hat tip to Victoria Kirst of Google for some of today’s slides!

new and delete (revisit from last week)

Arrays type* name = new type[length]; A dynamically allocated array. The variable that refers to the array is a pointer. The memory allocated for the array must be manually released, or else the program will have a memory leak. (>_<) delete[] name; Manually releases the memory back to the computer. DRAW DIAGRAM ON WHITEBOARD to contrast two approaches.

Memory on the stack and heap main() myFunction() x: y: void myFunction() { int x = 5; int y = 3; int *heapArr = new int[2]; heapArr[0] = randomInteger(0,3); // bad -- memory leak coming! } heapArr:

Memory on the stack and heap main() myFunction() x: y: void myFunction() { int x = 5; int y = 3; int *heapArr = new int[2]; heapArr[0] = randomInteger(0,3); // bad -- memory leak coming! } void randomInteger(int low, int high) { int var1 = 5; double var2 = 3.14159; … What happens when myFunction() and randomInteger() return? Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)? heapArr: randomInteger() v1: v2:

Memory on the stack and heap main() myFunction() x: y: void myFunction() { int x = 5; int y = 3; int *heapArr = new int[2]; heapArr[0] = randomInteger(0,3); // bad -- memory leak coming! } void randomInteger(int low, int high) { int var1 = 5; double var2 = 3.14159; … What happens when myFunction() and randomInteger() return? Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)? heapArr: randomInteger’s stack frame automatically released

Memory on the stack and heap main() void myFunction() { int x = 5; int y = 3; int *heapArr = new int[2]; heapArr[0] = randomInteger(0,3); // bad -- memory leak coming! } void randomInteger(int low, int high) { int var1 = 5; double var2 = 3.14159; … What happens when myFunction() and randomInteger() return? Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)? myFunction’s stack frame automatically released

Always a pair: new and delete Sample codes from Friday: // a simple main int main() { int* a = new int[3]; a[0] = 42; a[1] = -5; a[2] = 17; delete[] a; return 0; } // constructor and destructor // in ArrayList.cpp ArrayList::ArrayList() { myElements = new int[10](); mySize = 0; myCapacity = 10; } void ArrayList::~ArrayList() { delete[] myElements;

What is a pointer?

Anything wrong with this struct? struct Album { string title; int year; string artist_name; int artist_age; string artist_favorite_food; int artist_height; // in cm };

Anything wrong with this struct? struct Album { string title; int year; string artist_name; int artist_age; string artist_favorite_food; int artist_height; // in cm }; Style-wise seems awkward - "artist_" prefix on fields Anything else? How many times do you construct the artist info?

Album lemonade = { "Lemonade", 2016, "Beyonce", 34, "Red Lobster", 169 }; Album bday = { "B'Day", 2006, Redudant code to declare an initialize these two album variables, lemonade and bday

It's redundantly stored, too "Lemonade", 2016, "Beyonce", 34, "Red Lobster", 169 "B'Day", 2006, "Beyonce", 34, "Red Lobster", 169 lemonade bday Storage in memory is also redundant

How do we fix this? Should probably be another struct? struct Album { string title; int year; string artist_name; int artist_age; string artist_favorite_food; int artist_height; // in cm }; Should probably be another struct?

Does this fix the redundancy? struct Artist { string name; int age; string favorite_food; int height; // in cm }; struct Album { string title; int year; Artist artist; }; Artist britney = { "Britney Spears", 34, "Snickers", 163}; Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; Album femme_fatale = { "Femme Fatale", 2011, britney }; What does this look like in memory?

What does it mean when you have a struct field? struct Album { string title; int year; Artist artist; }; This embeds all the fields of the Artist struct into the Album struct. struct Artist { string name; int age; string favorite_food; int height; // in cm };

Still stored redundantly "Britney Spears", 34, "Snickers", 163 { "Blackout", 2007, "Britney Spears", 34, "Snickers", 163 } { "Circus", 2008, "Britney Spears", 34, "Snickers", 163 } britney blackout circus Artist britney = { "Britney Spears", 34, "Snickers", 163}; Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney };

Still stored redundantly "Britney Spears", 34, "Snickers", 163 { "Blackout", 2007, "Britney Spears", 34, "Snickers", 163 } { "Circus", 2008, "Britney Spears", 34, "Snickers", 163 } britney blackout circus Artist britney = { "Britney Spears", 34, "Snickers", 163}; Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; All the fields of britney are copied in this step!

Still stored redundantly "Britney Spears", 34, "Snickers", 163 { "Blackout", 2007, "Britney Spears", 34, "Snickers", 163 } { "Circus", 2008, "Britney Spears", 34, "Snickers", 163 } britney blackout circus Artist britney = { "Britney Spears", 34, "Snickers", 163}; Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; britney.favorite_food = "Twix"; What happens to the data? (a) All 3 Snickers change to Twix (b) only britney Snickers changes to Twix (c) only blackout/circus Snickers changes to Twix

What do we really want? How do we do this in C++? …pointers! "Britney Spears", age: 34, food: "Snickers", height: 163 title: "Blackout", year: 2007, artist: Please see the "britney" object title: "Circus", year: 2008, artist: Please see the "britney" object britney blackout circus The album's artist field should point to the "britney" data structure instead of storing it. How do we do this in C++? …pointers!

new with objects stack heap Example: Album* album = new Album; album->title = "Blackout"; album->year = 2007; album 0x64 0x24 stack heap 234123523 "Blackout" 0x64 340293023 2007

Pointers Taking a deeper look at the syntax of that array on the heap

Memory is a giant array 40 36 bool kitkat = true; int candies = 10; 32 50123 40 bool kitkat = true; int candies = 10; Whenever you declare a variable, you allocate a bucket (or more) of memory for the value of that variable Each bucket of memory has a unique address 38252 36 93402 32 4402 28 5552 24 1952 20 42552 16 683 12 82391 8 23532 4 93042

Memory addresses 50123 40 Whenever you declare a variable, you allocate a bucket (or more) of memory for the value of that variable Each bucket of memory has a unique address You can get the value of a variable's address using the & operator. 38252 36 93402 32 4402 28 5552 24 1952 20 42552 16 cout << &candies << endl; // 20 cout << &kitkat << endl; // 0 683 12 82391 8 23532 4 93042

Memory addresses 50123 40 You can store memory addresses in a special type of variable called a pointer. i.e. A pointer is a variable that holds a memory address. You can declare a pointer by writing (The type of data it points at)* e.g. int*, string* 38252 36 93402 32 4402 28 5552 24 1952 20 42552 16 cout << &candies << endl; // 20 cout << &kitkat << endl; // 0 int* ptrC = &candies; // 20 bool* ptrB = &kitkat; // 0 683 12 82391 8 23532 4 93042