Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
2 Printing! Use Computer Science Printers Only For Your “Computer Science Homework!!!!” Print Only What Is Necessary!
Start Visual Studio 2019
Add fflush_stdin() & HitCarriageReturnToContinue
main Refinement
6
7
Add This Code To Main 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); } 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
11 11
Valid Memory Address? short int 4 bytes * short int *PtrNo; *PtrNo ?? &1000 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, 8,387,743 Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0 On most C/C++ compilers, if the ?? Garbage memory address is in the Valid 0 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 4 bytes * short int *PtrNo; *PtrNo ?? &1000 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, 8,387,743 Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0 On most C/C++ compiler(*PtrNo) = 5 Could Easily 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.
14 14
Attempt To Use Un-Initialized Pointer short int 4 bytes * short int *PtrNo; 4,226,888,757 *PtrNo (*PtrNo) = 123; &1000 If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0 On most C/C++ compiler (*PtrNo) = 123 8,387,743 Would Destroy Something. 0 Visual C++ Does A Lot To Increase Security & Protect Us C++17 etc. gets gets_s scanf scanf_s
16 16
Better Practice Avoid Dangling Pointers 4 bytes * short int PtrNo NULL int main(int argc, char * argv[]) &1000 { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo = NULL; printf("PtrNo = %ld\n", PtrNo); puts("--------------- End Of Main ---------------\n"); HitCarriageReturnToContinue(); return (0); } Hopefully You Learned This In Your C Class
18 18
Review Malloc & Free (from C) - 1 2 bytes short int short int ?? *PtrNo = NULL; 4 bytes * short int &924240 PtrNo 924240 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 Function free will create a *PtrNo = NULL; problem when it tries to return 4 bytes * short int 2,000,000 bytes of memory beginning at &0! 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. If the compiler is unable to provide this, then PtrNo is filled with NULL ASSUME THAT IS THE CASE!
Review Malloc & Free (from C) - 3 short int *PtrNo; At Some Point, This Value 4 bytes * short int Will exceed The Capacity! PtrNo NULL ??? &1000 PtrNo = (short int *) malloc(1000000000 * 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 short int ?? *PtrNo = NULL; 4 bytes * short int &1529803 PtrNo NULL 15298032 &1000 PtrNo = (short int *) malloc(sizeof(short int)); printf("PtrNo = %ld\n", PtrNo); if (PtrNo != NULL) free(PtrNo); 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.
Review Malloc & Free (from C) - 5 2 bytes short int short int ?? *PtrNo = NULL; 4 bytes * short int &1529803 PtrNo NULL 15298032 &1000 PtrNo = (short int *) malloc(sizeof(short int)); printf("PtrNo = %ld\n", PtrNo); if (PtrNo != NULL) free(PtrNo); printf("PtrNo = %ld\n\n", PtrNo); PtrNo = NULL; printf("PtrNo = %ld\n\n", PtrNo); Avoid "Dangling Pointers"!
Review Malloc & Free (from C) - 6 2 bytes short int short int ?? 127 *PtrNo = NULL; 4 bytes * short int &1529803 PtrNo 15298032 NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); if (PtrNo != NULL) { (*PtrNo) = 127; free(PtrNo); PtrNo = NULL; }
25 25
26 26
Review New & Delete (from C++) - 1 2 bytes short int short int ?? 127 *PtrNo = NULL; 4 bytes * short int &10165528 PtrNo 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; }
Review New & Delete (from C++) - 1 20 bytes 10 short int # define MAX 10 4 bytes * short int short int ?? 90 9 *PtrNo = NULL; PtrNo 10165528 NULL ?? 80 8 &1000 ?? 70 PtrNo = new short [MAX]; 7 ?? 60 if (PtrNo != NULL) 6 ?? 50 { 5 ?? 40 4 for (int Pos = 0; Pos < MAX; Pos++) ?? 30 3 PtrNo[Pos] = 10 * Pos; ?? 20 2 ?? 10 1 for (int Pos = 0; Pos < MAX; Pos++) ?? 0 0 cout << setw(5) << PtrNo[Pos]; &10165528 delete [] PtrNo; PtrNo = NULL; }
29 29
Create A Struct Called Part 24 character Name long No Memory Map struct Part { char Name[24]; long No; };
Create A Dynamic Memory Pointer, Called BB, That Is Of Part Type Memory Map struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part 4 bytes * Part *BB = NULL; BB NULL &1000
Allocate A Block Of Dynamic Memory For One Part Memory Map struct Part 24 bytes Part { Name 24 bytes No 4 bytes char ?? | ?? Name[24]; &10165528 long No; }; main (int argc, char argv[]) { Part 4 bytes * Part *BB = NULL; BB 10165528 NULL BB = new Part; &1000
Place "Basketball" In Part Name struct Part { char Name[24]; long 24 bytes Part Name 24 bytes No 4 bytes No; }; ?? | ?? Basketball &10165528 main (int argc, char argv[]) { Part 4 bytes * Part *BB = NULL; BB 10165528 NULL BB = new Part; &1000 strcpy_s(BB->Name, "Basketball"); or strcpy_s((*BB).Name, "Basketball");
Place 10021 In Part Name struct Part { 4 bytes * Part char BB 10165528 NULL Name[24]; &1000 long 24 bytes Part Name 24 bytes No 4 bytes No; }; ?? | ?? 10021 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;
Display struct Part { 4 bytes * Part char BB 10165528 NULL Name[24]; &1000 long No; 24 bytes Part Name 24 bytes No 4 bytes }; main (int argc, char argv[]) ?? | ?? 10021 Basketball { &10165528 Part *BB = NULL; BB = new Part; strcpy_s(BB->Name, "Basketball"); (*St).No = 10021; printf("Name.. = %s\n", BB->Name); printf("No.... = %ld\n\n", (*BB).No); delete BB; }
36 36
Always Check Dynamic Memory Allocation! struct Part { char Name[24]; long main (int argc, char argv[]) No; { }; 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; } }
Recommend
More recommend