473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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++){
tList.push_back(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;

do
{
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 6272
Banfa
9,065 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

tList.clear();

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

tList.resize( ... );

http://msdn2.microsoft.com/en-us/library/00k1x78a.aspx
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];

rkstep(&func,x,y,ny,h,y1,dy);

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


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

rkdrive(&func,&xlist,&ylist,ny,b,acc,eps,h);

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?

Cheers,
Sovedyr
May 12 '10 #3

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

Similar topics

2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
33
by: junky_fellow | last post by:
Consider a singly-linked list. Each node has a structure, struct node { char c; struct node *next; }; Each of the nodes contain an alphabetic character. Can anybody suggest a way to print...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
2
by: ruca | last post by:
Hi gurus, Can anyone tell me please when I really must setup the value of memory that aspnet_wp or w3wp must use in IIS server machine? What I mean is that we have clients that have diferent...
3
by: Radenko Zec | last post by:
I have filled generic list with data on one form. When I close form generic list stay in memory waiting GC to collect. I want to implement code on form close to dispose generic list.
5
by: Max2006 | last post by:
Hi, What is the limit for memory that a .NET process or AppDomain can use? Thank you, Max
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.