468,512 Members | 1,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

C++ STL list memory issues

This is for all those STL-heads out there, this mught be trivial for you but I am a noob with the STL. I am having to implement a system in which I wil have many (thousands) of containers, I have been noticing that once you delete an element from a list memory is not released until program termination.

for the sake of argument lets say that the code looks like:

#include <iostream>
#include <list>

using namespace std;

int main(int argc, char *argv[]){
list<int> tList;

for (int i=1; i<=500000; i++){
cout << "i: " << i << endl;

cout << "tList.front(): " << tList.front() << endl;
cout << "tList.back(): " << tList.back() << endl;

list<int>::iterator Iter;
Iter = tList.begin();

// According to myself this is where I delete all elements in the list
tList.erase(Iter, tList.end());

cout << "tList.front(): " << tList.front() << endl;
cout << "tList.back(): " << tList.back() << endl;

//hold for input
int c;

c = getchar();
if(c == EOF) break;
} while(c != '\n');


but after looking at memory, all the memory taken up by the initialization is still allocated, Is there a way of releasing this memory?
Jul 21 '06 #1
2 6041
9,056 Expert Mod 8TB
// According to myself this is where I delete all elements in the list
tList.erase(Iter, tList.end());

The help for erase specifically says that no reallocation takes place (so that other pointers and iterators remain valid). If you want to clear you list why no use


If you only want to remove a few elements then having removed the unwanted elements use

tList.resize( ... );

Jul 22 '06 #2
I too have a problem with the allocation of memory when using list.

#define pi 3.141592653589793238462643383279502884197169399375 10
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <list>
#include <deque>
#include "rkstep.h"
#include "norm.h"
#include "rkdrive.h"

using namespace std;

void func(double x, double* y, double* f)
f[0] = y[1];
f[1] = -y[0];

int main()
int ny = 2;

double x = 0.0;
double y[] = {0.0,2.0};
double h = 0.001;
double eps=0.001;
double acc=0.001;

double b = 20.0;
double y1[ny];
double dy[ny];


list<double> xlist;
xlist.push_back (x);

list<double*> ylist;
ylist.push_back (y);


list<double*>::iterator j;
list<double>::iterator i;

ofstream test1("test.dat",ios::out);
for(i=xlist.begin(),j=ylist.begin(); j!=ylist.end();i++, j++)
test1 << *i << " " << **j << "\n";
return 0;

where rkstep is a function who will change x and y then in rkdrive one numer is added to the xlist using the push_back, while the ylist gets an array added in the end so ylist is now a list of arrays.
Now in the for loop I want to return the values of xlist and ylist. This also works for 1999 out of 2000 entrances. but for the last one I get a random number indicating that the allocation of memory for the list is not done correctly.
How can I avoid this problem?

May 12 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Barry Hynes | last post: by
33 posts views Thread by junky_fellow | last post: by
1 post views Thread by msnews.microsoft.com | last post: by
2 posts views Thread by ruca | last post: by
3 posts views Thread by Radenko Zec | last post: by
22 posts views Thread by Peter | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.