delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1993/05/07/00:01:07

Date: Thu, 6 May 93 23:09:08 EDT
From: rezaei AT tristan DOT TN DOT CORNELL DOT EDU (Mohammad Ali Rezaei)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: malloc/free broken??

(I know what you're thinking: "here is another programmer who
        can't find his malloc/free pairs..."
        But please bear with me. I've spent hours isolating this problem).

To make a long story short, after isolating the problem, I came
up with the following test program.
The situation: I like to allocate lots of memory and then free
it, only to go ahead and allocate more of a different size. (and
repeat this over and over). The problem is, if the sizes are sufficiently
different, the program will start using more memory or even swap, *although*
the total allocated *currently* is not more than available memory.
To see what I mean, look at the following program:

#include <stdlib.h>
#include <stdio.h>
#include "gppconio.h"

main()
{
    int i;
    int *p1;
    

    p1=(int *) malloc(1000000 * sizeof(int));
    for(i=0;i<1000000;i+=2048) *(p1+i)=1;
    getch();    
    free(p1);
    getch();    

    p1=(int *) malloc(500000 * sizeof(int));
    for(i=0;i<500000;i+=2048) *(p1+i)=1;
    getch();    
    free(p1);
    getch();    
    
    p1=(int *) malloc(250000 * sizeof(int));
    for(i=0;i<250000;i+=2048) *(p1+i)=1;
    getch();    
    free(p1);
    getch();    
    
}



So it allocates 4MB, 2MB and 1MB. you would expect the 2MB chunk
to fit inside the 4MB, and therefore not require more real memory,
but it does! I run this program in debug32 and just watch the real 
memory being eaten away and then swapping to start.

I would really appreciate help on this. If it is a djgcc problem,
I hope we can fix it. BTW, I'm using 1.09.

                                                -Mohammad
P.S. I know I can do my own memory pooling, but that seems so
        arcahic in a virtual memory paradigm.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019