// // new2.cpp -- shows how to use the new operator to create an array // when the program is running. The new operator returns // a pointer to the newly allocated memory chunk. // This array is allocated until you use the "delete" operator // with the array designator. // // This program is just a pedagogical example and is // not very useful or well-organized // #include using namespace std; int main() { int *array; // pointer var: place to hold addr of ints long len, i; // regular ints cout << "How many numbers do you want to store? "; cin >> len; array = new int[len]; // create a new array, just like that! // throws exception if it runs out of // memory for (i = 0; i < len; ++i) // Use the new array array[i] = i; for (i = 0; i < len; ++i) // Use the new array cout << "array[" << i << "] is " << array[i] << endl; delete [] array; // Give the memory back. Remember [] array = NULL; return 0; }