Download presentation
Presentation is loading. Please wait.
1
Pointers & Dynamic Memory
Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
2
Write Down Important Link!
c Write Down Important Link! thicks/2320/Schedule.html 2
3
c Objectives For Today 3
4
What You Should Do For Next Class
Review C Dynamic Memory malloc & free Do C++ Dynamic Memory new & delete Begin C++ Classes
5
Assignment For Next Tuesday
c Assignment For Next Tuesday 5
6
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.
7
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
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
9
Make A Copy Of Project Review C Pointers! 9
10
Call The Project Dynamic-Memory
3
11
Review Pointers 11
12
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
13
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.
14
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
15
Acceptable Range Of Values For PtrNo
15 15
16
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.
17
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.
18
Set Ptrs To NULL 18 18
19
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
20
Review Dynamic Memory Pointers
20 20
21
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".
22
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( * 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!
23
Review Malloc & Free (from C) - 3
short int *PtrNo = NULL; 4 bytes * short int PtrNo NULL &1000 PtrNo = (short int *) malloc( * 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.
24
Review Malloc & Free (from C) - 4
2 bytes short int ?? & 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.
25
Review Malloc & Free (from C) - 5
2 bytes short int ?? & 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.
26
In C++ malloc & free are replaced
Bad News In C++ malloc & free are replaced by safer new & delete 26 26
27
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
28
Review New & Delete (from C++) - 1
2 bytes short int ?? & 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; }
29
Review New & Delete (from C++) - 1
# define MAX 10 short int *PtrNo = NULL; 20 bytes 10 short int & ?? 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; } 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];
30
struct 30 30
31
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
32
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?
33
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 ?? & Name 24 bytes No 4 bytes | Memory Map? BB = new Part;
34
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 ?? & Name 24 bytes No 4 bytes | Basketball Memory Map? strcpy_s(BB->Name, "Basketball"); or strcpy_s((*BB).Name, "Basketball");
35
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 ?? & Name 24 bytes No 4 bytes | Basketball 10021 Memory Map? St->No = 10021; or (*St).No = 10021;
36
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 ?? & Name 24 bytes No 4 bytes | Basketball 10021 printf("Name.. = %s\n", BB->Name); printf("No.... = %ld\n\n", (*BB).No); delete BB; }
37
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
38
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; } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.