spasmous wrote:
Yo,
I'm running a subroutine that mallocs a 10 or so Mb, then frees it on
exit. Then when it's called again it mallocs/frees again, and so on
for maybe 100 calls.
Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail. Or... am I just
a bad programmer ;)
Thanks for your input.
How about looking at what you're doing between the calls of malloc and
free? Maybe something in there is causing the problem. To convince
yourself, why not write a very simple program that just allocates and
frees 10 Meg of memory in a loop?
#include <cstdlib>
void allocate_stuff()
{
char *p = (char *)malloc( 10000000 );
free(p);
}
int main()
{
char *p;
for ( int i = 0; i < 100; ++i )
allocate_stuff();
}
This code does what you describe. Does it cause problems as you've
described them, or does it run smoothly?
Paul