Top Banner
Dynamic Memory & File Input/Output
18

Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory&

File Input/Output

Page 2: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory: Introduction

• Generally the memory requirements of a program are determined before program execution by the declaration of variables.

• In this way the program will always use the same amount of memory and this cannot be changed while the program is running.

• It is generally useful however to adjust the memory requirements at runtime especially when your program needs a user input to determine the number of variables.

Page 3: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory: Introduction

• The allocation of memory at runtime is referred to as dynamic memory allocation.

• Dynamic memory allocation is managed in C++ using the new and delete keywords.

• The new keyword is used to dynamically allocate memory for variables and the delete keyword is used to release that memory when it is no longer needed.

Page 4: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory: Basic Syntax• When dynamic memory is created a pointer to

that memory location is what is returned to the program.

• To use the new keyword to dynamically allocate memory the following syntax is used:

pointer = new type;pointer = new type [number_of_elements];

• The first statement creates one variable and the second creates an array of variables.

Page 5: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory: Example• The dynamic array is the more useful an

example is: int *foo; int lent=0; cout<<"please enter a length: "; cin>>lent; foo=new int[lent]; //dynamic array created cout<<"please enter numbers: "; for (int cnt=0;cnt<lent;cnt++) { cin>>foo[cnt]; //or cin>>*(foo+cnt); }

Page 6: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Dynamic Memory: Class Exercise

Page 7: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Introduction

Page 8: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Introduction

–––

Page 9: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

•☺

Page 10: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

Page 11: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax•

Page 12: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

Page 13: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

• We will work mostly with text files so the output to them will be formatted as text. This is much the same way that the standard output stream works.

• Once opened a file object is written to like the standard input/output.

Page 14: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Example

Page 15: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

Page 16: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

File Input/Output: Basic Syntax

Page 17: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

Class Exercise

• Create a program to find the average of a group of numbers and print the numbers and their average.

• The numbers and their average should also be saved to a file.

• The amount of numbers must be specified by the user at runtime.

Page 18: Dynamic Memory File Input/Output€¦ · Dynamic Memory: Basic Syntax •When dynamic memory is created a pointer to that memory location is what is returned to the program. •To

END