For C/C++ coders discussions and solutions
Wed Dec 12, 2012 1:10 pm
Hi to all!
Some time ago I have a strange problem with memory alocation (while using stl containers). I attached c++ source files (ok.cpp and fail.cpp). In ok.cpp everything work "almost" fine, I mean almost all the memory is released after deleting all the elements. BUT the number of 500*8 elements on the list seems to be a limit somehow. In fail.cpp, the number of elements on the list is 500*16 (the size of single element is decreased to 1024*64 from 1024*128). Here, after deleting all the elements of the list and going out of the scope where the list is declared, there is no memory released.
1 How is that possible?
2 What to do? How to fix it?
Thanks for ANY help.
Sun Dec 16, 2012 10:00 pm
Please post the code?
Mon Dec 17, 2012 3:35 pm
msi_333 wrote:Please post the code?
+
Part of the code or the debugger report
this is important!
Tue Dec 18, 2012 1:27 pm
Both !
Tue Dec 18, 2012 3:05 pm
#include <list>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
{
printf ("Allocating\n");
list<char*> testList;
for (int a = 0; a < 500 * 16; a++)
{
char *test = new char[1024 * 64];
memset(test, 'A', 1024 * 64);
testList.push_back(test);
}
usleep(10000000);
printf ("Deallocating\n");
for (list<char*>::iterator a = testList.begin(); a != testList.end(); a++)
{
delete[] *a;
}
}
usleep(10000000);
return 0;
}
ok.cpp C++ 543 bytes
#include <list>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
{
printf ("Allocating\n");
list<char*> testList;
for (int a = 0; a < 500 * 8; a++)
{
char *test = new char[1024 * 128];
memset(test, 'A', 1024 * 128);
testList.push_back(test);
}
usleep(10000000);
printf ("Deallocating\n");
for (list<char*>::iterator a = testList.begin(); a != testList.end(); a++)
{
delete[] *a;
}
}
usleep(10000000);
return 0;
}
Wed Dec 19, 2012 5:09 pm
the problem is solved!
Wed Dec 19, 2012 5:25 pm
how , i was think about it ?
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.