delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/12/10:25:17

Date: Sun, 12 Dec 1999 14:05:50 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Wormy <wormy AT technologist DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Problems with MALLOC and FREE
In-Reply-To: <82rh3j$sfu@cantine.wu-wien.ac.at>
Message-ID: <Pine.SUN.3.91.991212140531.19096Y-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Fri, 10 Dec 1999, Wormy wrote:

> >This usually means that your program either overwrites the buffers that
> >you allocate (like if it puts more than 64 bytes into each 64-byte
> >buffer), or that it free()'s some of the buffers more than once.
> 
> BUT WHY DOES IT FREE MORE THAN ONCE??!

Most probably, because of bug(s) in your code.

> It even happens sometimes if I execute the following (one after the other):
> 
> char *mem[12000];
> 
> for (i=0; i<12000; i++)                                    // THIS PART
> ALWAYS WORKS RIGHT ON MY COMP
>                                                                          //
> THE MEMORIES ARE PROPERLY ALLOCATED I CAN
>                                                                         //
> WRITE AND READ TO THEM
> {
>   if ((mem[i]=(char *)malloc(64*sizeof(char)))==NULL)
>     HANDLE ERROR
>   else
>     memset(mem[i], 0, 64*sizeof(char));
> }
> 
> // AND NOW WITHOUT DOING ANYTHING
> 
> for (i=0; i<12000; i++)
> {
>   if (mem[i] != NULL)
>     free (mem[i]);
> }
> 
> // AND SOMETIMES IT HAPPENS THAT THE PROGRAM SHUTS DOWN WITH A SIGSEGV!

Please post the smallest complete program that can be compiled and
used to reproduce these problems on your machine.  It is very hard to
help you without seeing the complete program.  I'm guessing that the
buggy code is somewhere in the fragments that you omitted.

- Raw text -


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