excessive memory consumption
Joe Buck
jbuck@synopsys.com
Mon Sep 29 17:33:00 GMT 1997
The attached program, when built with
c++ -O tdeque.cxx -o tdeque
builds correctly, but needs 492 megabytes of virtual memory to build on a
Sparc. Fortunately, I was using a server with gobs of memory. That's,
um, impressive. I haven't whipped out purify, but there's got to be some
kind of leak here (or tons of unneeded stuff is kept around?).
The program attempts to do thorough tests of three different expansions
of deque<T>: deque<double>, deque<string>, and deque<pair<int,int> >,
using one test template.
---------------- cut here ---------------------
#include <string>
#include <deque>
#include <utility>
#include <iostream.h>
#include <algorithm>
inline const char* yn(bool ans) { return ans ? "yes" : "no";}
template<class A,class B>
ostream& operator<<(ostream& o, const pair<A,B>& p)
{
o << '(' << p.first << ',' << p.second << ')';
return o;
}
template<class T>
void show_d(const char* label, const deque<T>& data)
{
cout << label;
copy(data.begin(), data.end(), ostream_iterator<T>(cout, " "));
cout << endl;
}
// val1, val2, val3 should be three distinct values, all different from the
// default constructor value T().
template<class T>
void deque_test(const T& val1, const T& val2, const T& val3)
{
cout << "val1 = " << val1 << ", val2 = " << val2 << ", val3 = "
<< val3 << endl;
// test basic contructors
deque<T> dq1;
show_d("empty deque: ", dq1);
cout << "empty? " << dq1.empty() << ", size? " << dq1.size() << endl;
deque<T> dq2(5);
show_d("default deque of 5 elements: ", dq2);
cout << "empty? " << dq2.empty() << ", size? " << dq2.size() << endl;
deque<T> dq3(4, val1);
show_d("four elements of val1: ", dq3);
deque<T> dq4(dq3);
show_d("copy ctor: ", dq4);
// test push_back and push_front
dq1.push_back(val1);
dq1.push_back(val2);
dq1.push_front(val3);
cout << "push back: " << val1 << endl;
cout << "push back: " << val2 << endl;
cout << "push front: " << val3 << endl;
show_d("dq1 with the three values: ", dq1);
// test compares:
cout << "dq1 == dq3? " << yn(dq1 == dq3) << endl;
cout << "dq1 < dq3? " << yn(dq1 < dq3) << endl;
cout << "dq3 < dq1? " << yn(dq3 < dq1) << endl;
cout << "dq1 == dq1? " << yn(dq1 == dq1) << endl;
cout << "dq1 < dq1? " << yn(dq1 < dq1) << endl;
// test member template ctor and reverse iterators
deque<T> dq5(dq1.rbegin(), dq1.rend());
show_d("dq5: should reverse dq1: ", dq5);
// save value
deque<T> dq6(dq5);
// use indexing.
for (unsigned i = 0; i < dq1.size(); i++)
dq5[i] = dq1[i];
show_d("dq5 after index copy: ", dq5);
// restore value. (test assignment op)
dq5 = dq6;
show_d("dq5 restored: ", dq5);
// swap
dq5.swap(dq3);
show_d("dq5 after swap with dq3: ", dq5);
show_d("dq3 after swap with dq5: ", dq3);
cout << "dq3 front: " << dq3.front() << ", dq3 back: " << dq3.back() << endl;
// insertion tests.
deque<T> dq7;
dq7.insert(dq7.begin(), val1);
dq7.insert(dq7.begin(), 3, val2);
dq7.insert(dq7.end(), val3);
show_d("dq7: ", dq7);
typename deque<T>::iterator iter1 = find(dq7.begin(), dq7.end(), val1);
dq7.erase(iter1);
show_d("dq7 after erasing first val1 value: ", dq7);
dq7.pop_back();
show_d("dq7 after pop_back: ", dq7);
dq7.pop_front();
show_d("dq7 after pop_front: ", dq7);
copy(dq5.begin(), dq5.end(), back_inserter(dq7));
show_d("dq7 with dq5 appended: ", dq7);
copy(dq5.begin(), dq5.end(), front_inserter(dq6));
show_d("dq6 with dq5 prepended: ", dq6);
sort(dq7.begin(), dq7.end());
show_d("sorted dq7: ", dq7);
dq7.resize(dq7.size() + 2);
show_d("dq7 extended by 2: ", dq7);
dq7.resize(dq7.size() + 2, val3);
show_d("dq7 extended and filled with val3: ", dq7);
dq7.clear();
show_d("dq7 after clear: ", dq7);
}
int main() {
deque_test(1.2, 2.3, 3.5);
deque_test(string("alpha"), string("beta"), string("gamma"));
deque_test(make_pair(1,2), make_pair(2,3), make_pair(3,5));
}
---------------- cut here ---------------------
More information about the Gcc
mailing list