Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
Write Down Important Link! c Write Down Important Link! http://carme.cs.trinity.edu/ thicks/2320/Schedule.html http://carme.cs.trinity.edu/thicks/2320/Schedule.html 2
c Objectives For Today 3
What You Should Do For Next Class Review C Dynamic Memory malloc & free Do C++ Dynamic Memory new & delete Begin C++ Classes
Assignment For Next Tuesday c Assignment For Next Tuesday 5
What You Should Do For Next Class Read Course Outline Complete the two-page questionnaire. Install Visual Studio 2017 Professional On Your Computer (if you have not done so) Complete OOP-1 Homework. (It is the longest lab form that you will do all semester - but only two pages of it have not been in the review recommendations that I sent out to those registered during the break) It Is Important That We Make Sure That The Review Material Does Not Prevent You From Completing The CSCI 2320 Material.
Check Web Site For Quizzes Practice - Review For Upcoming Quiz 1 Using Visual Studio Check The Class Schedule Daily! I Will Either Announce Quizzes In Class Or Post On The Web Site Two Days In Advance
The Search Capability Is Important! Recommendation If You Are Viewing My Slides Sets To Learn The Material View The PPS The Annimation Important! If You Are Answering The Short Answer Questions On The Homework Labs View PDF The Search Capability Is Important! 8
Make A Copy Of Project Review C Pointers! 9
Call The Project Dynamic-Memory 3
Review Pointers 11
printf("PtrNo = %ld\n", PtrNo); Add This Code To Main # include "Utilities.hpp" int main(int argc, char * argv[]) { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo; printf("PtrNo = %ld\n", PtrNo); puts("--------------- End Of Main ---------------\n"); HitCarriageReturnToContinue(); return (0); } Run The Program
Most C/C++ Compilers Will Generate No Errors Visual Studio Provides A Number Of Safeguards That Is Not Incorporated Into Other Compilers Memory gets_s, strcpy_s, etc.
Memory Maps Often Help Us To See What Is Happening What Is The Problem? short int *PtrNo; Unknown Value In &1000? *PtrNo &1000 4 bytes * short int ?? Memory Maps Often Help Us To See What Is Happening
Acceptable Range Of Values For PtrNo 15 15
Valid Memory Address? short int *PtrNo; ?? 1 GB = 1,048,576 bytes &1000 4 bytes * short int 8,387,743 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0 On most C/C++ compilers, if the ?? Garbage memory address is in the Valid Range, the compiler continues: (*PtrNo) = 5 Would Change Something It may mess up your word processor it may mess up your ability to access the Internet it may mess up your ability to print it may mess up another part of the program, etc.
InValid Memory Address? short int *PtrNo; ?? *PtrNo &1000 4 bytes * short int 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0 8,387,743 On most C/C++ compiler(*PtrNo) = 5 Would Crash The System. This means that a program might compile one moment (because ?? is in the valid memory range) and not compile the next moment.
Set Ptrs To NULL 18 18
Better Practice Avoid Dangling Pointers # include "Utilities.hpp" int main(int argc, char * argv[]) { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo = NULL; printf("PtrNo = %ld\n", PtrNo); puts("--------------- End Of Main ---------------\n"); HitCarriageReturnToContinue(); return (0); } PtrNo &1000 4 bytes * short int NULL Hopefully You Learned This In Your C Class
Review Dynamic Memory Pointers 20 20
Review Malloc & Free (from C) - 1 2 bytes short int ?? 924240 &924240 short int *PtrNo = NULL; 4 bytes * short int PtrNo NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); free(PtrNo); This malloc call requests, of the compiler, a contiguous block of memory that is 2 bytes in size. If the compiler is unable to provide this, then PtrNo is filled with NULL. Function free must return the allocated memory back to the operating system; failure to do that results in a "memory leak".
Review Malloc & Free (from C) - 2 short int *PtrNo = NULL; 4 bytes * short int NULL If the compiler is unable to provide this, then PtrNo is filled with NULL ASSUME THAT IS THE CASE! PtrNo &1000 PtrNo = (short int *) malloc(1000000* sizeof(short int)); free(PtrNo); This malloc call requests, of the compiler, a contiguous block of memory that is 2,000,000 bytes in size. Function free will create a problem when it tries to return 2,000,000 bytes of memory beginning at &0!
Review Malloc & Free (from C) - 3 short int *PtrNo = NULL; 4 bytes * short int PtrNo NULL &1000 PtrNo = (short int *) malloc(1000000* sizeof(short int)); if (PtrNo != NULL) free(PtrNo); I hope you have been taught that you should chase each and every request for dynamic memory with a test to verify that malloc was successful.
Review Malloc & Free (from C) - 4 2 bytes short int ?? 15298032 &1529803 short int *PtrNo = NULL; 4 bytes * short int PtrNo NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); printf("PtrNo = %ld\n\n", PtrNo); if (PtrNo != NULL) free(PtrNo); PtrNo = NULL; Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.
Review Malloc & Free (from C) - 5 2 bytes short int ?? 15298032 &1529803 short int *PtrNo = NULL; 127 4 bytes * short int PtrNo NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); if (PtrNo != NULL) { (*PtrNo) = 127; free(PtrNo); PtrNo = NULL; } Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.
In C++ malloc & free are replaced Bad News In C++ malloc & free are replaced by safer new & delete 26 26
new works just like malloc & delete works just like free safer Good News new works just like malloc & delete works just like free safer You Are To Use delete & new in 2320 27 27
Review New & Delete (from C++) - 1 2 bytes short int ?? 10165528 &10165528 short int *PtrNo = NULL; 127 4 bytes * short int PtrNo NULL &1000 PtrNo = new short int; if (PtrNo != NULL) { (*PtrNo) = 127; printf("PtrNo = %ld\n\n", PtrNo); printf("*PtrNo = %hi\n\n", *PtrNo); delete PtrNo; PtrNo = NULL; }
Review New & Delete (from C++) - 1 # define MAX 10 short int *PtrNo = NULL; 20 bytes 10 short int &10165528 ?? 4 bytes * short int 50 60 70 80 90 10 20 30 40 5 6 7 8 9 1 2 3 4 PtrNo = new short [MAX]; if (PtrNo != NULL) { delete [] PtrNo; PtrNo = NULL; } 10165528 PtrNo NULL &1000 for (int Pos = 0; Pos < MAX; Pos++) PtrNo[Pos] = 10 * Pos; for (int Pos = 0; Pos < MAX; Pos++) cout << setw(5) << PtrNo[Pos];
struct 30 30
NaDa We Have Not Allocated Any Memory Yet Create A Struct Called Part 24 character Name long No struct Part { char Name[24]; long No; }; Create Memory Map? NaDa We Have Not Allocated Any Memory Yet
Create A Dynamic Memory Pointer, Called BB, That Is Of Part Type struct Part { char Name[24]; long No; }; NULL BB &1000 4 bytes * Part main (int argc, char argv[]) { Part *BB = NULL; Memory Map?
Allocate A Block Of Dynamic Memory For One Part struct Part { char Name[24]; long No; }; main (int argc, char argv[]) Part *BB = NULL; NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes | Memory Map? BB = new Part;
Place "Basketball" In Part Name struct Part { char Name[24]; long No; }; main (int argc, char argv[]) Part *BB = NULL; BB = new Part; NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes | Basketball Memory Map? strcpy_s(BB->Name, "Basketball"); or strcpy_s((*BB).Name, "Basketball");
Memory Map? Place 10021 In Part Name struct Part { char Name[24]; long No; }; main (int argc, char argv[]) Part *BB = NULL; BB = new Part; strcpy_s(BB->Name, "Basketball"); NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes | Basketball 10021 Memory Map? St->No = 10021; or (*St).No = 10021;
Display printf("Name.. = %s\n", BB->Name); struct Part { char Name[24]; long No; }; main (int argc, char argv[]) Part *BB = NULL; BB = new Part; strcpy_s(BB->Name, "Basketball"); (*St).No = 10021; NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes | Basketball 10021 printf("Name.. = %s\n", BB->Name); printf("No.... = %ld\n\n", (*BB).No); delete BB; }
If You Had This Solution It Would Work But … I Will Always Take Off A Little If You Don't Test Your Dynamic Memory Processing! 37 37
Always Check Dynamic Memory Allocation! struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; if (BB != NULL) { strcpy_s(BB->Name, "Basketball"); (*St).No = 10021; printf("Name.. = %s\n", BB->Name); printf("No.... = %ld\n\n", (*BB).No); delete BB; } }