memory used: std::list v.s. std::forward_list

Viewed 6257

Because list have one more pointer(previous pointer) than forward_list, so if they both hold the same number of element, i.e. 1<<30, list will use almost 1/3 more memory. Right?

Then if I repeat calling resize larger and larger, forward_list must be able to resize much larger than list.

Test code:

#include<forward_list>
#include<list>
#include<iostream>
int main(){
    using namespace std;
    typedef list<char> list_t;
    //typedef forward_list<char> list_t;
    list_t l;
    list_t::size_type i = 0;
    try{
        while(1){
            l.resize(i += (1<<20));
            cerr<<i<<" ";
        }
    }
    catch(...){
        cerr<<endl;
    }
    return 0;
}

To my surprise, when the process is killed, they have almost the same size ... Anybody could interpret it?

2 Answers
Related