1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
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.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
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.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Memory Management Issues, Solutions, and Examples.
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.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
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.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
C:\Temp\Templates 4 5 Use This Main Program 6.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Memory Management.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers & Dynamic Memory
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS111 Computer Programming
Presentation transcript:

1

2 2

Call The Project Dynamic-Memory

4 4

# include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo) = %d\n", (*PtrNo)); getchar(); return(0); } Copy-Paste Main Run The Program

# include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; //(*PtrNo) = 5; //printf ("(*PtrNo) = %d\n", (*PtrNo)); getchar(); return(0); } Why Does It Not Work? ?? *PtrNo & bytes  * short int What Is The Value In &1000?

short int No = 12, *PtrNo; Pointer Review - 1 ?? *PtrNo & bytes  * short int Sketch The Memory For No  No & bytes  short int 12 Sketch The Memory For *PtrNo  Write The Line Of C/C++ Code To Display The Contents Of No Write The Line Of C/C++ Code To Display The Address Of No (10) Write The Line Of C/C++ Code To Display The Address Of No (16) Our & Addresses Will Not Be The Same

short int No = 12, *PtrNo; Pointer Review - 2 ?? *PtrNo & bytes  * short int No & bytes  short int 12 Write The Line Of C/C++ Code To Display The Contents Of PtrNo Write The Line Of C/C++ Code To Display The Address Of PtrNo (10) Write The Line Of C/C++ Code To Display The Address Of PtrNo (16) Our & Addresses Will Not Be The Same

short int No = 12, *PtrNo; Pointer Review - 3 *PtrNo & bytes  * short int No & bytes  short int 12 Write The Line Of C/C++ Code To Make PtrNo Point To No Write The Line Of C/C++ Code To Display The Address In PtrNo (10) Write The Line Of C/C++ Code To Display The Value Pointed To By PtrNo (16) Our & Addresses Will Not Be The Same &

short int No = 12, *PtrNo; Pointer Review - 4 *PtrNo & bytes  * short int No & bytes  short int 12 Write The Line Of C/C++ Code To Increment Value Pointed To By PtrNo Our & Addresses Will Not Be The Same & Write The Line Of C/C++ Code To Display The Contents Of No Write The Line Of C/C++ Code To Display The Value Pointed To By PtrNo (16)

11

Add Student.hpp & Student.cpp To Project

Set The Diagnostic Level To 1 Run The Program

Allocate Memory Write The C/C++ Code To Allocate Memory For SPtr.

Display The Student - 1 Write The C/C++ Code To Display Our Student.

Display The Student - 2 Two Ways To Display Our Student.

Change The Student Info Write The C/C++ Code To Change The Student Info To ^

18

malloc & new 19 You may remember using malloc to allocate dynamic memory in C. We are going to use the C++ equivalent, called new. (Type Safe)

new ? NULL 20 When Using New, The Compiler Is Asking The Operating System "Where Do You Have Space (A Contiguous Block Of Memory) For Me To Provide This Variable?" PtrNo = new long int [5000]; The Operating System Return NULL If There Is Not A Block Of Contiguous Space Avalilable! PtrNo = new long int [5000]; if (PtrNo == NULL)... (or) if (PtrNo != NULL)... You Must Always Check This Return Before Using The Variable! (NULL is 0 - right where the operating system is loaded!)

short int, *PtrNo; Pointer Review - 1 ?? *PtrNo & bytes  * short int Sketch Memory Type This Line Of C/C++ Code To Dynamically Allocate Memory For PtrNo Our Only Reference To This Block Of Memory Is Through The Pointer PtrNo! Our & Addresses Will Not Be The Same & bytes  short int ?? &1004

Dynamic Memory - 2 ?? *PtrNo & bytes  * short int & bytes  short int 12 &1004 When The Programmer Fails To Return Dynamic Memory To The Memory Manager, That Memory Is Lost Until The Computer, or Virtual Computer, Is Restarted! This Is Referred To As A Memory Leak!

Dynamic Memory - 3 ?? *PtrNo & bytes  * short int & bytes  short int 12 &1004 Function delete Is Used To Return The Dynamic Memory To The Memory Manager. Just as free is used with malloc, delete is used with new.

Dynamic Memory - 4 ?? *PtrNo & bytes  * short int & bytes  short int 12 &1004 If The Return Of The Dynamic Memory To The Memory Manager Is Not At The End Of The Program/Function, Good Programmers Will Always Set The Pointer To NULL To Avoid Dangling Pointers!

Dynamic Memory - 5 ?? *PtrNo & bytes  * short int Write The Line Of C/C++ Code To Allocate An Array Of 5 Short Integers Pointed to By PtrNos  Don't Execute Yet! ?? & bytes  short int ?? &1004 Write The Block Of C/C++ Code To Fill The Array With 5, 10, 15, … Did This Work? How Do You Know!

Dynamic Memory - 6 Write The Block Of C/C++ Code To Display The Contents Of The Array Pointed To By PtrNos Discuss The Line Of C/C++ Code To Return The Dynamic Memory delete PtrNos[ ]; vs. delete PtrNos;

27

class Name { public: Name(void); ~Name(void); void Set (char NewFirst[] = "", char NewLast[] = ""); private: int NoCharsInFirstName, NoCharsInLastName; char *First, *Last; }; Name::Name(void) { } 28 Construct A Memory Map [Sketch Of Memory] For Name N1; Initial N1 Map Address Symbolic Name Contents | &1000 | *FirstName | ? | | &1004 | *LastName | ? | | &1008 | NoCharsInFirst | ? | | &1012 | NoCharsInLast | ? | Not A Good Constructor! Nothing Initialized! Dangling Pointers FirstName & LastName - Pointers Reference Memory Incorrect Memory! 28

29

class Person { public: Person (char NewFirst[] = "", char NewLast[] = ""); ~Person (void); void Set (char NewFirst[] = "", char NewLast[] = ""); private: int NoCharsInFirstName, NoCharsInLastName; char *First, *Last; }; 30 Create A Good Constructor For Person P1; Person::Person (char NewFirst[] = "", char NewLast[] = "") { }; You Do! NoCharsInFirstName = strlen(NewFirst)+1; First = new char[NoCharsInFirstName]; strcpy(First, NewFirst); NoCharsInLastName = strlen(NewLast)+1; Last = new char[NoCharsInLastName]; strcpy(First, NewFirst); In order to avoid dangling pointers, the constructor should either allocate the needed dynamic memory or set the pointer to NULL. 30

class Person { public: Person (char NewFirst[] = "", char NewLast[] = ""); ~Person (void); void Set (char NewFirst[] = "", char NewLast[] = ""); private: int NoCharsInFirstName, NoCharsInLastName; char *First, *Last; }; 31 Create A Good Destructor For Person P1; Person::~Person (void) { }; You Do! delete [] First; delete[] Last; 31

class Person { public: Person (char NewFirst[] = "", char NewLast[] = ""); ~Person (void); void Set (char NewFirst[] = "", char NewLast[] = ""); private: int NoCharsInFirstName, NoCharsInLastName; char *First, *Last; }; 32 Create A Good Memory Map For Person P1 (“Mickey”, “Mouse”); You Do! Initial P1 Map Address Symbolic Name Contents | &1000 | *FirstName | &2000 | | &2000-&2006 | Mickey | | &1004 | *LastName | &3174 | | &1008 | NoCharsInFirst | 7 | | &1012 | NoCharsInLast | 6 | | &3174-&3180 | Mouse |

33

Initial Donald Map Address Symbolic Name Contents | &1016 | *FirstName | &2400 | | &2400-&2406 | Donald | | &1020 | *LastName | &2822 | | &1024 | NoCharsInFirst | 7 | | &1028 | NoCharsInLast | 5 | | &2822-&2826 | Duck | Person Mickey (“Mickey”, “Mouse”), Donald (“Donald”, “Duck”); Initial Mickey Map Address Symbolic Name Contents | &1000 | *FirstName | &2000 | | &2000-&2006 | Mickey | | &1004 | *LastName | &3174 | | &1008 | NoCharsInFirst | 7 | | &1012 | NoCharsInLast | 6 | | &3174-&3180 | Mouse | Donald = Mickey; Shallow Copy! No Operator Overload For = What Happens? Alter The Memory Maps Accordingly! 34

Initial Donald Map Address Symbolic Name Contents | &1016 | *FirstName | &2400 | | &2400-&2406 | Donald | | &1020 | *LastName | &2822 | | &1024 | NoCharsInFirst | 7 | | &1028 | NoCharsInLast | 5 | | &2822-&2826 | Duck | Initial Mickey Map Address Symbolic Name Contents | &1000 | *FirstName | &2000 | | &2000-&2006 | Mickey | | &1004 | *LastName | &3174 | | &1008 | NoCharsInFirst | 7 | | &1012 | NoCharsInLast | 6 | | &3174-&3180 | Mouse | Donald = Mickey;Shallow Copy! Memory Leak Shallow copy of Dynamic Variables Creates Memory Leak – When Either Mickey Or Donald Go Out Of Scope, The Destructor Would Cause Other To Have Dangling Pointers. 35

36 Problems With A Shallow Copy 1. Shallow Copy only applies to objects which contain pointers 2. Shallow copy transfers pointer addresses, but fails to properly manage dynamic memory 3. Immediately creates a Memory Leak  If processing occurs in actual memory [like in Windows, MacOS, etc.] re-booting the computer is generally the easiest way to return the lost memory to the Operating System  If processing occurs in a shell [like in UNIX, Linux] logging out and then re-logging in is generally the easiest way to return the lost memory to the Environment. 4.When one of the assignment variables goes out of scope, the destructor for that variable will cause the other assigned variable to have one or more Dangling Pointers.

37 I Hope You Are Asking Yourself? How Do I Avoid The Problems Generated By Shallow Copies? Potential Problem In Many Languages Not Unique To C & C++ Must Be Some Solution?

38

class Person { public: Name (char NewFirst[] = "", char NewLast[] = ""); ~Name(void); void Set (char NewFirst[] = "", char NewLast[] = ""); void operator = (Person P); private: int NoCharsInFirstName, NoCharsInLastName; char *First, *Last; }; 39 Create A Good Operator Overload For Person Mickey (“Mickey”, “Mouse”), Donald (“Donald”, “Duck”); Mickey = Donald;

void Person::operator = (Person P) { // Fill the Indigenous data NoCharsInFirstName = P.NoCharsInFirstName; NoCharsInLastName = P.NoCharsInLastName; // Free the memory associated with First and Last if (First != NULL) delete [] First; if (Last != NULL) delete [] Last; Person Mickey (“Mickey”, “Mouse”), Donald (“Donald”, “Duck”); Mickey = Donald; 40 Part 1/2

// Allocate memory for First and fill it with P.First if appropriate if (N.First == NULL) First = NULL; else { First = new char [NoCharsInFirstName]; strcpy (First, P.First); } // Allocate memory for Last and fill it with P.Last if appropriate if (N.Last == NULL) Last = NULL; else { Last = new char [NoCharsInLastName]; strcpy (Last, P.Last); } Deep Copy - makes an actual copy of the data to which the pointers are pointing. 41 Part 2/2 There Are Other Solutions!

42 1. Object has one or more pointers 2. Shallow copy transfers pointer addresses and properly manage dynamic memory 3. Creates no Memory Leak 4.Creates No Dangling Pointers. Deep Copy Solves Problems With A Shallow Copy

So Why Do We Really Care About This Shallow Copy & Deep Copy Stuff? Are We Ever Going To Use It? 43

44