a dynamically allocated array

Solutions on MaxInterview for a dynamically allocated array by the best coders in the world

showing results for - "a dynamically allocated array"
Salomé
13 Aug 2018
1#include <iostream>
2#include <cstddef> // std::size_t
3 
4int main()
5{
6    std::cout << "Enter a positive integer: ";
7    std::size_t length{};
8    std::cin >> length;
9 
10    int *array{ new int[length]{} }; // use array new.  Note that length does not need to be constant!
11 
12    std::cout << "I just allocated an array of integers of length " << length << '\n';
13 
14    array[0] = 5; // set element 0 to value 5
15 
16    delete[] array; // use array delete to deallocate array
17 
18    // we don't need to set array to nullptr/0 here because it's going to go out of scope immediately after this anyway
19 
20    return 0;
21}