/* * File: allocation.cpp * * This program simulates different blocks of code in conjunction * with a homework assignment. The point is to explore several * subtleties in the allocation and subsequent assignment of objects * (in this case, STL vector's). * * This can be compiled as: * g++ -o allocation allocation.cpp */ #include #include using namespace std; int main() { for (int problem=1; problem<=5; problem++) try { cout << "---- starting problem " << problem << "-----" << endl; vector a; vector b(100, 2222); vector c(200); c.at(30) = 3333; vector d(b); vector *e = new vector(100, 4444); vector *f = new vector(b); switch (problem) { case 1: // problem (i) cout << b.at(30) << endl; cout << c[30] << endl; cout << d.at(30) << endl; c = b; d = *e; cout << b[30] << endl; cout << c.at(30) << endl; cout << d[30] << endl; cout << e->at(30) << endl; break; case 2: // problem (ii) cout << b[30] << endl; cout << e->at(30) << endl; cout << f->at(30) << endl; f = e; f->at(30) = 7777; cout << b.at(30) << endl; cout << e->at(30) << endl; cout << f->at(30) << endl; break; case 3: // problem (iii) cout << a.at(30) << endl; a = b; cout << a[30] << endl; break; case 4: // problem (iv) cout << b[30] << endl; cout << e->at(30) << endl; cout << f->at(30) << endl; f = &b; f->at(30) = 5555; cout << b.at(30) << endl; cout << e->at(30) << endl; cout << f->at(30) << endl; *f = *e; f->at(30) = 9999; cout << b[30] << endl; cout << e->at(30) << endl; cout << f->at(30) << endl; break; case 5: // problem (v) cout << b.size() << endl; cout << &b[150] << endl; cout << b[150] << endl; cout << c[46] << endl; b[150] = 9999; cout << b[150] << endl; cout << &c[46] << endl; cout << c[46] << endl; break; } } catch (...) { cout << "Unexpected exception occurred.\n"; } }