Hi,
I need to use a list structure in my program. I don't use STL list but my own (very simple one, I don't need very sophisticated functions):
And then I have such short code:Code:class element { public: int i; element *next; element() { i=0; next=NULL; } }; class _list { public: element *start, *last; _list() { start=NULL; last=NULL; } int add(int); }; int _list::add(int i) { element *new_; new_=new element; new_->i=i; new_->next=NULL; if(last==NULL) { start=new_; last=new_; } else { last->next=new_; last=new_; } return 0; }
So I have an array of 10^6 lists. And then to each list L[x] I add one element. So the memory taken by that structure should be something like 19 mb (main list class - 2 pointers each 4 bytes, so it's 8 *10^6/1024/1024 = 7.6 Mb; then we have 10^6 elements, each one has a 1 pointer (4b) and integer number (8b) so it's 12*10^6/1024/1024 = 11.4Mb, to if we sum it up to should be ~ 19 Mb). But when I add sleep(1000) before returning the main function and run ps aux I have:Code:_list L[1000000]; int main() { for(int i = 0; i < 1000000; i++) L[i].add(i); return 0; }
25780Kb? 25Mb? How? Can any one explain me that?$ ps aux
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
apacz 3431 22.4 9.8 25780 24716 p0 S+ 10:36PM 0:00.72 ./my_program
Regards,
apacz