/* Filename: dynamic.cpp Author: Br. David Carlson Date: July 30, 1998 Last Revised: Feb 2, 2015 This program shows how to dynamically allocate space for a plain integer and for an array of integers, freeing up the space after it is no longer needed. Finally, it shows two ways of using pointers to set up strings. Tested with: Microsoft Visual Studio 2013 g++ under Linux */ #include using namespace std; const int MAX = 16; typedef int * IntPtrType; typedef char * CharPtrType; int main(void) { IntPtrType IntPtr; int NumItems, k; CharPtrType Msg1 = "Section 5"; CharPtrType Msg2; // First, let's dynamically allocate space for an int, with exception handling: try { IntPtr = new int; } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(1); } // Then use this int for something: *IntPtr = 44; cout << "The int pointed to by IntPtr is " << *IntPtr << endl << endl; delete IntPtr; // Free up the memory space. // Second, let's dynamically allocate space for an int, without exception handling: IntPtr = new (nothrow) int; // Do not throw a bad_alloc exception if new fails. if (IntPtr == NULL) // Instead, return a NULL pointer. { cerr << "Could not allocate sufficient space" << endl; exit(2); } // Then use this int for something: *IntPtr = 55; cout << "The int pointed to by IntPtr is " << *IntPtr << endl << endl; delete IntPtr; // Deallocate the space used by *IntPtr: // Third, let's dynamically allocate an array of ints, with exception handling: cout << "Enter the number of integers you wish to store:" << endl << "(use a small positive integer): "; cin >> NumItems; cout << endl; try { IntPtr = new int[NumItems]; } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(3); } // Then use this array: for (k = 0; k < NumItems; k++) { cout << "Enter an integer to store: "; cin >> IntPtr[k]; } cout << endl << "The values stored in the array are:" << endl; for (k = 0; k < NumItems; k++) cout << IntPtr[k] << " "; cout << endl << endl; // Use [] when deleting an array: delete [] IntPtr; // Fourth, let's dynamically allocate an array of ints, without exception handling: cout << "Enter the number of integers you wish to store:" << endl << "(use a small positive integer): "; cin >> NumItems; cout << endl; IntPtr = new (nothrow) int[NumItems]; // nothrow means this will not throw an exception if new fails. if (IntPtr == NULL) { cerr << "Could not allocate sufficient space" << endl; exit(4); } // Then use this array: for (k = 0; k < NumItems; k++) { cout << "Enter an integer to store: "; cin >> IntPtr[k]; } cout << endl << "The values stored in the array are:" << endl; for (k = 0; k < NumItems; k++) cout << IntPtr[k] << " "; cout << endl << endl; // Use [] when deleting an array: delete [] IntPtr; // Fifth, let's output the first message: cout << Msg1 << endl << endl; // Sixth, let's allocate space (with exception handling) and fill in the second message: try { Msg2 = new char[16]; } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(6); } strcpy(Msg2, "Section 6"); cout << Msg2 << endl << endl; delete [] Msg2; // free up the space // Seventh, let's allocate space (without exception handling) and fill in the second message: Msg2 = new (nothrow) char[16]; if (Msg2 == NULL) { cerr << "Could not allocate sufficient space" << endl; exit(7); } strcpy(Msg2, "Section 7"); cout << Msg2 << endl << endl; delete [] Msg2; // free up the space // NOTE: In the final 2 sections, comment one of the two off, build and run; then comment the other one off while // removing the comments from the previous attempt, build and run. // Eighth, let's attempt to allocate what should be too much space (with exception handling): try { Msg2 = new char[0x7fffffff]; // In Linux you may have to add 1 or 2 more letters f to get the error message. } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(8); } strcpy(Msg2, "Section 8"); cout << Msg2 << endl << endl; delete [] Msg2; // free up the space // Ninth, let's attempt to allocate what should be too much space (without exception handling): //Msg2 = new (nothrow) char[0x7fffffff]; // In Linux you may have to add 1 or 2 more letters f to get the error message. //if (Msg2 == NULL) // { // cerr << "Could not allocate sufficient space" << endl; // exit(1); // } //strcpy(Msg2, "Section 9"); //cout << Msg2 << endl << endl; //delete [] Msg2; // free up the space return 0; }