// @topic T51002 Miscellaneous small samples developed in class
// @brief 10/02/2012 -- allocating/deallocating arrays, using a vector
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int* allocate_memory( int how_much ) {
return ( new int[ how_much ] );
}
int main()
{
cout << "how many ints do you want?";
int array_size;
cin >> array_size; // 4
//int myarray[ 4 ]; myarray[ 0 ] = 123;
//int* myarray = new int[ array_size ]; // addr -> int int int int
// create an array of integers:
int* myarray = allocate_memory( array_size );
myarray[ 0 ] = 123;
// deallocate memory
if ( myarray != NULL ) {
delete[] myarray;
myarray = NULL; // good practice:
delete[] myarray; // it's safe to "delete" a NULL ptr
}
// create another array of integers:
myarray = allocate_memory( 2 * array_size );
// whenever possible, use STL containers to manage memory:
vector< int > vect;
vect.push_back( 1 );
vect.push_back( 3 );
vect.push_back( 2 );
// make a copy -- it's that easy!
vector< int > vect2 = vect;
return 0;
}