Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1

2 1

3 2 2

4 Call The Project Dynamic-Memory

5 4 4

6 # 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

7 # 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 &1000 4 bytes  * short int What Is The Value In &1000?

8 short int No = 12, *PtrNo; Pointer Review - 1 ?? *PtrNo &1002 4 bytes  * short int Sketch The Memory For No  No &1000 2 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

9 short int No = 12, *PtrNo; Pointer Review - 2 ?? *PtrNo &3602268 4 bytes  * short int No &3602280 2 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

10 short int No = 12, *PtrNo; Pointer Review - 3 *PtrNo &3734804 4 bytes  * short int No &3734816 2 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 &3734816

11 short int No = 12, *PtrNo; Pointer Review - 4 *PtrNo &3734804 4 bytes  * short int No &3734816 2 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 &3734816 13 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)

12 11

13 Add Student.hpp & Student.cpp To Project

14 Set The Diagnostic Level To 1 Run The Program

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

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

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

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

19 18

20 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)

21 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!)

22 short int, *PtrNo; Pointer Review - 1 ?? *PtrNo &1000 4 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 &1004 2 bytes  short int ?? &1004

23 Dynamic Memory - 2 ?? *PtrNo &1000 4 bytes  * short int &1004 2 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!

24 Dynamic Memory - 3 ?? *PtrNo &1000 4 bytes  * short int &1004 2 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.

25 Dynamic Memory - 4 ?? *PtrNo &1000 4 bytes  * short int &1004 2 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!

26 Dynamic Memory - 5 ?? *PtrNo &1000 4 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! ?? &1004 10 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!

27 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;

28 27

29 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

30 29

31 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

32 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

33 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 | -------------------------------------- -------------------------------

34 33

35 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 | -------------------------------------- ------------------------------- 34 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

36 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 | -------------------------------------- ------------------------------- 35 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

37 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.

38 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?

39 38

40 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;

41 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

42 // 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!

43 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

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

45 44


Download ppt "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)"

Similar presentations


Ads by Google