From: Mark S. <ms...@cl...> - 2005-05-19 21:41:49
|
It looks like it's not a problem with realloc() directly, but a problem with copying between large allocated chunks. This program: #include <stdlib.h> #include <string.h> void my_memcpy(char *dest, char *src, unsigned int len) { for(int i=3D0; i<len; i++) { dest[i] =3D src[i]; } } int main(int argc, char **argv) { int len =3D 0; char *data =3D NULL; int do_copy =3D 1; for(int i=3D0; i<2000; i++) { len +=3D 4096; char *new_data =3D (char *) malloc(len*sizeof(char)); if (data && do_copy) { my_memcpy(new_data, data, len-4096); } free(data); data =3D new_data; } free(data); return 0; } Exibits the same problem if do_copy is 1, but behaves properly if do_copy is 0. --Mark -----Original Message----- From: val...@li... [mailto:val...@li...] On Behalf Of Nicholas Nethercote Sent: Thursday, May 19, 2005 2:16 PM To: Thomas Steffen Cc: val...@li... Subject: Re: [Valgrind-users] Problem with realloc() + valgrind 2.4.0 (incl. sample program) On Thu, 19 May 2005, Thomas Steffen wrote: > If you look at the realloc function in memcheck, you will find this is > by design. Realloc always returns a fresh piece of address space, so=20 > that it can check for any old pointer that are not updated. Of course=20 > this uses a lot of address space, which means it uses a lot of memory. Are you sure? It looks to me like it reuses the same memory if the new=20 size is smaller or equal to the old size. It allocates new memory if the=20 new size is bigger. But the allocator does avoid recycling freed blocks for a while, which=20 might explain the 700MB figure. N ------------------------------------------------------- This SF.Net email is sponsored by Oracle Space Sweepstakes Want to be the first software developer in space? Enter now for the Oracle Space Sweepstakes! http://ads.osdn.com/?ad_id=3D7412&alloc_id=3D16344&op=3Dclick _______________________________________________ Valgrind-users mailing list Val...@li... https://lists.sourceforge.net/lists/listinfo/valgrind-users |