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

pointers dynamic memory
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1
  • Dr. Tom Hicks

Computer Science Department

Pointers & Dynamic Memory

Review C Pointers Introduce C++ Pointers

Data Abstractions

CSCI-2320

slide-2
SLIDE 2

Printing!

Use Computer Science Printers Only For Your “Computer Science Homework!!!!”

Print Only What Is Necessary!

2

slide-3
SLIDE 3

Start Visual Studio 2019

slide-4
SLIDE 4

Add fflush_stdin() & HitCarriageReturnToContinue

slide-5
SLIDE 5

main  Refinement

slide-6
SLIDE 6

6

slide-7
SLIDE 7

7

slide-8
SLIDE 8

Run The Program

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); }

Add This Code To Main

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

slide-10
SLIDE 10

short int *PtrNo;

What Is The Problem?

??

*PtrNo &1000 4 bytes  * short int

Unknown Value In &1000?

slide-11
SLIDE 11

11 11

slide-12
SLIDE 12

On most C/C++ compilers, if the ?? Garbage memory address is in the Valid Range, the compiler continues:

short int *PtrNo;

Valid Memory Address?

??

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

slide-13
SLIDE 13

InValid Memory Address?

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

short int *PtrNo;

??

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

slide-14
SLIDE 14

14 14

slide-15
SLIDE 15

Attempt To Use Un-Initialized Pointer

If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <= ?? <= 0

8,387,743

short int *PtrNo; (*PtrNo) = 123;

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.

slide-16
SLIDE 16

16 16

slide-17
SLIDE 17

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); }

Better Practice  Avoid Dangling Pointers

NULL

PtrNo &1000 4 bytes  * short int Hopefully You Learned This In Your C Class

slide-18
SLIDE 18

18 18

slide-19
SLIDE 19

Review Malloc & Free (from C) - 1

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

  • perating system; failure to do that results in a "memory leak".

free(PtrNo);

slide-20
SLIDE 20

Review Malloc & Free (from C) - 2

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!

slide-21
SLIDE 21

Review Malloc & Free (from C) - 3

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

slide-22
SLIDE 22

Review Malloc & Free (from C) - 4

NULL

PtrNo &1000 4 bytes  * short int

PtrNo = (short int *) malloc(sizeof(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.

if (PtrNo != NULL) free(PtrNo); printf("PtrNo = %ld\n", PtrNo); printf("PtrNo = %ld\n\n", PtrNo);

slide-23
SLIDE 23

Review Malloc & Free (from C) - 5

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);

PtrNo = NULL; printf("PtrNo = %ld\n\n", PtrNo);

slide-24
SLIDE 24

Review Malloc & Free (from C) - 6

NULL PtrNo &1000 4 bytes  * short int

short int *PtrNo = NULL;

2 bytes  short int ?? 15298032 &1529803

if (PtrNo != NULL) {

(*PtrNo) = 127;

free(PtrNo); PtrNo = NULL; }

127

PtrNo = (short int *) malloc(sizeof(short int));

slide-25
SLIDE 25

25 25

slide-26
SLIDE 26

26 26

slide-27
SLIDE 27

Review New & Delete (from C++) - 1

NULL PtrNo &1000 4 bytes  * short int

PtrNo = new short int;

short int *PtrNo = NULL;

2 bytes  short int ?? 10165528 &10165528 127

if (PtrNo != NULL) { (*PtrNo) = 127; printf("PtrNo = %ld\n\n", PtrNo); printf("*PtrNo = %hi\n\n", *PtrNo); delete PtrNo; PtrNo = NULL; }

slide-28
SLIDE 28

Review New & Delete (from C++) - 1

NULL PtrNo &1000 4 bytes  * short int

# define MAX 10 short int *PtrNo = NULL;

PtrNo = new short [MAX]; if (PtrNo != NULL) { delete [] PtrNo; PtrNo = NULL; }

10165528 20 bytes  10 short int &10165528 ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

for (int Pos = 0; Pos < MAX; Pos++) PtrNo[Pos] = 10 * Pos;

5 6 7 8 9 1 2 3 4 50 60 70 80 90 10 20 30 40

for (int Pos = 0; Pos < MAX; Pos++) cout << setw(5) << PtrNo[Pos];

slide-29
SLIDE 29

29 29

slide-30
SLIDE 30

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

slide-31
SLIDE 31

main (int argc, char argv[]) { Part *BB = NULL; struct Part { char Name[24]; long No; }; Create A Dynamic Memory Pointer, Called BB, That Is Of Part Type Memory Map

NULL BB &1000 4 bytes  * Part

slide-32
SLIDE 32

struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part *BB = NULL;

Allocate A Block Of Dynamic Memory For One Part Memory Map

NULL

BB &1000 4 bytes  * Part

BB = new Part;

24 bytes  Part ?? 10165528 &10165528 Name  24 bytes No  4 bytes ?? |

slide-33
SLIDE 33

struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part *BB = NULL; BB = new Part;

Place "Basketball" In Part Name

NULL BB &1000 4 bytes  * Part

strcpy_s(BB->Name, "Basketball");

24 bytes  Part ?? 10165528 &10165528 Name  24 bytes No  4 bytes ?? |

  • r

strcpy_s((*BB).Name, "Basketball");

Basketball

slide-34
SLIDE 34

struct Part { char Name[24]; long No; }; main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; strcpy_s(BB->Name, "Basketball");

Place 10021 In Part Name

NULL BB &1000 4 bytes  * Part 24 bytes  Part ?? 10165528 &10165528 Name  24 bytes No  4 bytes ?? |

St->No = 10021;

Basketball

  • r

(*St).No = 10021;

10021

slide-35
SLIDE 35

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;

Display

NULL BB &1000 4 bytes  * Part 24 bytes  Part ?? 10165528 &10165528 Name  24 bytes No  4 bytes ?? | Basketball 10021

printf("Name.. = %s\n", BB->Name); printf("No.... = %ld\n\n", (*BB).No); delete BB; }

slide-36
SLIDE 36

36 36

slide-37
SLIDE 37

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

Always Check Dynamic Memory Allocation!

struct Part { char Name[24]; long No; };