pointers dynamic memory
play

Pointers & Dynamic Memory Review C Pointers Introduce C++ - PowerPoint PPT Presentation

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


  1. Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department

  2. 2 Printing! Use Computer Science Printers Only For Your “Computer Science Homework!!!!” Print Only What Is Necessary!

  3. Start Visual Studio 2019

  4. Add fflush_stdin() & HitCarriageReturnToContinue

  5. main  Refinement

  6. 6

  7. 7

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

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

  10. What Is The Problem? short int *PtrNo; Unknown Value In &1000? 4 bytes  * short int *PtrNo ?? &1000

  11. 11 11

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

  13. 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 14

  15. 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 16

  17. 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 18

  19. 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".

  20. 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!

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

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

  23. 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"!

  24. 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 25

  26. 26 26

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

  28. 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 29

  30. Create A Struct Called Part 24 character Name  long No Memory Map struct Part { char Name[24]; long No; };

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

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

  33. 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");

  34. 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;

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

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