Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamically allocating arrays

Similar presentations


Presentation on theme: "Dynamically allocating arrays"— Presentation transcript:

1 Dynamically allocating arrays

2 Outline In this lesson, we will: Revisit the three looping statements
Describe the break statement Look at the implementation Consider some consequences

3 Allocating instances Up to this point, we have looked at dynamically allocating single instances of primitive data types We will now looking at arrays Recall that a local variable declared to be an array is a local variable that is simply assigned an address: double array[6]{3.2, 4.5, 4.6, 4.8, 5.1, 5.6}; std::cout << array << std::endl; array[3] = 4.9; // slightly modify the 3rd entry

4 Allocating arrays If an array is just an address, like a pointer, can we not do the same thing for arrays? // Dynamically allocate memory for 6 doubles double *data_array{ new double[6] }; This allocates 6 x 8 = 48 bytes of memory This memory is found by the operating system and the address of the first byte is returned

5 Using allocated arrays
These arrays can now be used just like arrays declared to be local variables: #include <iostream> int main(); int main() { std::size_t n{6}; double *data_array{ new double[n] }; for ( std::size_t k{0}; k < n; ++k ) { std::cout << "Enter a number: "; std::cin >> data_array[k]; } // Do something with this data array... return 0;

6 Initializing dynamically allocated arrays
Dynamically allocated arrays can be uninitialized or initialized: // Do not initialize any entries--this is the fastest double *data_array{ new double[n] }; // Set all entries to 0 double *data_array{ new double[n]{} }; // Set the first 4 entries // - there must be at least 4 entries double *data_array{ new double[n]{1.0, 1.1, 1.2, 1.3} };

7 Deleting arrays Sometimes arrays are no longer necessary
It is important to delete memory when it is no longer needed Suppose we have an image—which is represented by a very large array When that image is no longer being displayed, or it is saved to the hard drive, there may be no longer a need to store it To delete an array, we must use the delete[] operator: delete[] data_array;

8 Passing arrays Consider the following function:
double *create_array( std::size_t n ) { double *data_array{ new double[n] }; for ( std::size_t k{0}; k < n; ++k ) { std::cout << "Enter a number: "; std::cin >> data_array[k]; } return data_array;

9 Passing arrays The following function clears an array and then deletes it void destroy_array( double *data_array, std::size_t n ) { for ( std::size_t k{0}; k < n; ++k ) { data_array[k] = 0.0; } delete[] data_array;

10 Passing arrays This function calculates the average:
double average ( double *data_array, std::size_t n ) { if ( n == 0 ) { throw 0.0; } else { double result{0.0}; for ( std::size_t k{0}; k < n; ++k ) { result += data_array[k]; } return result/n;

11 Passing arrays Inside another function, we can now call these functions to create and destroy arrays: int main() { std::size_t count; // Unitialized std::cout << "Enter an array size: "; std::cin >> count; double *data{ create_array( count ) }; std::cout << "The average of the entries is " << average( data, count ) << std::endl; destroy_array( data, count ); data = nullptr; return 0; }

12 Passing arrays by reference
An alternative approach is to assign the array within the function: void create_array( double *&data_array, std::size_t n ) { data_array = new double[n]; for ( std::size_t k{0}; k < n; ++k ) { std::cout << "Enter a number: "; std::cin >> data_array[k]; } void destroy_array( double *&data_array, std::size_t n ) { data_array[k] = 0.0; delete[] data_array; data_array = nullptr;

13 Passing arrays by reference
Using these functions that use pass by reference int main() { std::size_t count; // Unitialized std::cout << "Enter an array size: "; std::cin >> count; double *data{ nullptr }; create_array( data, count ); std::cout << "The average of the entries is " << average( data, count ) << std::endl; destroy_array( data, count ); return 0; }

14 Summary Following this lesson, you now
Understand how to dynamically allocate and deallocate arrays Know how to use arrays

15 References [1] No references?

16 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

17 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Dynamically allocating arrays"

Similar presentations


Ads by Google