- Dr. Tom Hicks
Computer Science Department
Pointers & Dynamic Memory
Review C Pointers Introduce C++ Pointers
Data Abstractions
CSCI-2320
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
Computer Science Department
CSCI-2320
Add fflush_stdin() & HitCarriageReturnToContinue
6
7
int main(int argc, char * argv[]) { puts ("-------------- Start Of Main --------------\n");
puts("--------------- End Of Main ---------------\n"); HitCarriageReturnToContinue(); return (0); }
Visual Studio Provides A Number Of Safeguards That Is Not Incorporated Into Other Compilers Memory gets_s, strcpy_s, etc.
??
*PtrNo &1000 4 bytes * short int
11 11
On most C/C++ compilers, if the ?? Garbage memory address is in the Valid Range, the compiler continues:
??
*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
(*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.
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
??
*PtrNo &1000 4 bytes * short int
On most C/C++ compiler(*PtrNo) = 5 Could Easily 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.
14 14
If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0
8,387,743
4,226,888,757
*PtrNo &1000 4 bytes * short int
On most C/C++ compiler (*PtrNo) = 123 Would Destroy Something. Visual C++ Does A Lot To Increase Security & Protect Us C++17 gets gets_s scanf scanf_s etc.
16 16
int main(int argc, char * argv[]) { puts ("-------------- Start Of Main --------------\n");
puts("--------------- End Of Main ---------------\n"); HitCarriageReturnToContinue(); return (0); }
NULL
PtrNo &1000 4 bytes * short int Hopefully You Learned This In Your C Class
18 18
NULL PtrNo &1000 4 bytes * short int
This malloc call requests, of the compiler, a contiguous block of memory that is 2 bytes in size. PtrNo = (short int *) malloc(sizeof(short int)); short int *PtrNo = NULL;
2 bytes short int ?? 924240 &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
free(PtrNo);
PtrNo &1000 4 bytes * short int
This malloc call requests, of the compiler, a contiguous block of memory that is 2,000,000 bytes in size. PtrNo = (short int *) malloc(1000000* sizeof(short int)); short int *PtrNo = NULL; If the compiler is unable to provide this, then PtrNo is filled with NULL ASSUME THAT IS THE CASE! free(PtrNo); Function free will create a problem when it tries to return 2,000,000 bytes of memory beginning at &0!
PtrNo &1000 4 bytes * short int
PtrNo = (short int *) malloc(1000000000 * sizeof(short int)); short int *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.
if (PtrNo != NULL) free(PtrNo);
At Some Point, This Value Will exceed The Capacity! NULL
NULL
PtrNo &1000 4 bytes * short int
short int *PtrNo = NULL;
2 bytes short int ?? 15298032 &1529803
Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.
NULL
PtrNo &1000 4 bytes * short int
PtrNo = (short int *) malloc(sizeof(short int)); short int *PtrNo = NULL;
2 bytes short int ?? 15298032 &1529803
Avoid "Dangling Pointers"! if (PtrNo != NULL) free(PtrNo); printf("PtrNo = %ld\n", PtrNo); printf("PtrNo = %ld\n\n", PtrNo);
NULL PtrNo &1000 4 bytes * short int
short int *PtrNo = NULL;
2 bytes short int ?? 15298032 &1529803
127
25 25
26 26
NULL PtrNo &1000 4 bytes * short int
short int *PtrNo = NULL;
2 bytes short int ?? 10165528 &10165528 127
NULL PtrNo &1000 4 bytes * short int
# define MAX 10 short int *PtrNo = NULL;
10165528 20 bytes 10 short int &10165528 ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
5 6 7 8 9 1 2 3 4 50 60 70 80 90 10 20 30 40
29 29
NULL BB &1000 4 bytes * Part
Allocate A Block Of Dynamic Memory For One Part Memory Map
NULL
BB &1000 4 bytes * Part
24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes ?? |
NULL BB &1000 4 bytes * Part
24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes ?? |
Basketball
NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes ?? |
Basketball
10021
struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part
NULL BB &1000 4 bytes * Part 24 bytes Part ?? 10165528 &10165528 Name 24 bytes No 4 bytes ?? | Basketball 10021
36 36
struct Part { char Name[24]; long No; };