Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
c http://carme.cs.trinity.edu/ thicks/2320/schedule.html http://carme.cs.trinity.edu/thicks/2320/schedule.html 2
c 3
What You Should Do For Next Class Review C Dynamic Memory malloc & free Do C++ Dynamic Memory new & delete Begin C++ Classes
c 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
8
9
Call The Project Dynamic-Memory 3
11
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.
What Is The Problem? short int *PtrNo; Unknown Value In &1000? 4 bytes * short int?? *PtrNo &1000
15 15
Valid Memory Address? short int *PtrNo; 4 bytes * short int?? *PtrNo &1000 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++ compilers, if the?? Garbage memory address is in the Valid Range, the compiler continues: 0 (*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; 4 bytes * short int?? *PtrNo &1000 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. 0 This means that a program might compile one moment (because?? is in the valid memory range) and not compile the next moment.
18 18
Better Practice Avoid Dangling Pointers # include "Utilities.hpp" NULL int main(int argc, char * argv[]) &1000 { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo = NULL; printf("ptrno = %ld\n", PtrNo); 4 bytes * short int PtrNo puts("--------------- End Of Main ---------------\n"); } HitCarriageReturnToContinue(); return (0); Hopefully You Learned This In Your C Class
20 20
short int *PtrNo = NULL; Review Malloc & Free (from C) - 1 PtrNo 4 bytes * short int 924240 NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); free(ptrno); 2 bytes short int This malloc call requests, of the compiler, a contiguous block of memory that is 2 bytes in size.?? &924240 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; PtrNo 4 bytes * short int &1000 PtrNo = (short int *) malloc(1000000* sizeof(short int)); free(ptrno); NULL This malloc call requests, of the compiler, a contiguous block of memory that is 2,000,000 bytes in size. If the compiler is unable to provide this, then PtrNo is filled with NULL ASSUME THAT IS THE CASE! 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; PtrNo 4 bytes * short int 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.
short int *PtrNo = NULL; Review Malloc & Free (from C) - 4 PtrNo 4 bytes * short int 15298032 NULL &1000 2 bytes short int PtrNo = (short int *) malloc(sizeof(short int)); printf("ptrno = %ld\n\n", PtrNo); if (PtrNo!= NULL) free(ptrno);?? &1529803 printf("ptrno = %ld\n\n", PtrNo); PtrNo = NULL; printf("ptrno = %ld\n\n", PtrNo); Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.
short int *PtrNo = NULL; Review Malloc & Free (from C) - 5 PtrNo 4 bytes * short int 15298032 NULL &1000 2 bytes short int 127?? &1529803 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.
26 26
27 27
Review New & Delete (from C++) - 1 short int *PtrNo = NULL; PtrNo 4 bytes * short int 10165528 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; } 2 bytes short int 127?? &10165528
Review New & Delete (from C++) - 1 # define MAX 10 short int *PtrNo = NULL; PtrNo PtrNo = new short [MAX]; if (PtrNo!= NULL) { 4 bytes * short int 10165528 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]; 20 bytes 10 short int 9 8 7 6 5 4 3 2 1 0???????????????????? 90 80 70 60 50 40 30 20 10 0 &10165528 } delete [] PtrNo; PtrNo = NULL;
30 30
struct Part { char Name[24]; long No; }; Create A Struct Called Part 24 character Name long No
Create A Dynamic Memory Pointer, Called BB, That Is Of Part Type struct Part { char Name[24]; long No; }; BB 4 bytes * Part NULL &1000 main (int argc, char argv[]) { Part *BB = NULL;
Allocate A Block Of Dynamic Memory For One Part struct Part { char Name[24]; long No; }; 4 bytes * Part BB 10165528 NULL &1000 24 bytes Part Name 24 bytes?? &10165528 No 4 bytes?? main (int argc, char argv[]) { Part *BB = NULL; BB = new Part;
struct Part { char Name[24]; long No; }; Place "Basketball" In Part Name BB main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; 4 bytes * Part 10165528 NULL &1000 24 bytes Part Name 24 bytes Basketball?? &10165528 strcpy_s(bb->name, "Basketball"); or strcpy_s((*bb).name, "Basketball"); No 4 bytes??
Place 10021 In Part Name struct Part { 4 bytes * Part char Name[24]; long No; BB 10165528 NULL &1000 Name 24 bytes }; Basketball main (int argc, char argv[]) &10165528 { Part *BB = NULL; BB = new Part; strcpy_s(bb->name, "Basketball"); St->No = 10021; or (*St).No = 10021; 24 bytes Part?? No 4 bytes 10021??
Display struct Part { 4 bytes * Part char BB 10165528 NULL Name[24]; long &1000 No; }; Name 24 bytes main (int argc, char argv[]) { &10165528 Part *BB = NULL; BB = new Part; strcpy_s(bb->name, "Basketball"); (*St).No = 10021; } 24 bytes Part No 4 bytes Basketball?? 10021?? printf("name.. = %s\n", BB->Name); printf("no... = %ld\n\n", (*BB).No); delete BB;
37 37
main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; if (BB!= NULL) { strcpy_s(bb->name, "Basketball"); (*St).No = 10021; } Always Check Dynamic Memory Allocation! } printf("name.. = %s\n", BB->Name); printf("no... = %ld\n\n", (*BB).No); delete BB; struct Part { char Name[24]; long No; };