473,573 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ STL list memory issues

1 New Member
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.fron t(): " << tList.front() << endl;
cout << "tList.back (): " << tList.back() << endl;

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

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

cout << "tList.fron t(): " << 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 6287
9,065 Recognized Expert Moderator Expert
// According to myself this is where I delete all elements in the list
tList.erase(Ite r, 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
1 New Member
I too have a problem with the allocation of memory when using list.

#define pi 3.1415926535897 932384626433832 795028841971693 9937510
#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>::i terator i;

ofstream test1("test.dat ",ios::out) ;
for(i=xlist.beg in(),j=ylist.be gin(); 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

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

Similar topics

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, that is derived from the STL class List and uses exception handling.
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 the characters in reverse order.
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 char* formatter, ...); Then, I want to call it as so:
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 process. Is it correct?
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 aspx page. But the problem is these tend to really slow the development -- it takes up to 15 seconds for the page to come up in VS.NET design...
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 specificatiosn in their server machines that have IIS to runnning WebApplications. This can change for example in RAM memory that is available. One...
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.
by: Max2006 | last post by:
Hi, What is the limit for memory that a .NET process or AppDomain can use? Thank you, Max
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 memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.