From: "Amin" To: Subject: RE: problem with malloc and free Date: Sat, 14 Dec 2002 20:09:00 +0600 Message-ID: <000001c2a37a$59f41060$186a10ac@citechbd.com> MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Importance: Normal In-Reply-To: Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk The program tries to free cm and cxphi at least twice. First, in main(), when freeall() is called directly. Second, at exit, because in setupall(), it is specified that freeall() should be called when the program exits. HTH, Yawar ||-----Original Message----- ||From: Lars O. Hansen [mailto:lars DOT o DOT hansen AT gmx DOT de] ||Sent: Saturday, 14 December, 2002 16:39 ||To: djgpp AT delorie DOT com ||Subject: Re: problem with malloc and free || || ||Still stripped down, but this always crashes when compiled ||with gcc -pedantic -Wall is.c -lalleg || || ||C:\Programme\djgpp\projects>a ||Exiting due to signal SIGSEGV ||General Protection Fault at eip=00003180 ||eax=00000000 ebx=000084f9 ecx=00000000 edx=76202000 ||esi=00000054 edi=000656c4 ebp=000e5668 esp=000e5630 ||program=C:\PROGRA~1\DJGPP\PROJECTS\A.EXE ||cs: sel=00a7 base=84f99000 limit=000effff ||ds: sel=00af base=84f99000 limit=000effff ||es: sel=00af base=84f99000 limit=000effff ||fs: sel=0087 base=000111c0 limit=0000ffff ||gs: sel=00c7 base=00000000 limit=0010ffff ||ss: sel=00af base=84f99000 limit=000effff ||App stack: [000e56c4..000656c4] Exceptn stack: [0006561c..000636dc] || ||Call frame traceback EIPs: || 0x00003180 || 0x000017b1 || 0x0000184a || 0x000015f5 || 0x00040af8 || || || ||#include "stdlib.h" || || ||#define arraysize 360 || ||#define stype int ||#define rtype float ||#define trigtype float || ||#define id 1024 ||#define PHI 1.0 ||#define statusx 10 ||#define statusy 440 || || ||stype array_size=arraysize-1; || ||rtype *cm; ||trigtype *cxphi; ||trigtype *sxphi; ||rtype *a; ||trigtype *b; ||rtype *le; ||trigtype *an; ||stype *ak,*ek; || ||trigtype phi=PHI; ||int ||somecolor0,somecolor1,somecolor2,textforegroundcolor,BACKGROUND_COLOR; ||char status_string[]=" "; || || ||void setupall(void); ||void init_arrays(void); ||void freeall(void); ||void cls(void); || || ||int main() || { || setupall(); freeall(); || exit(0); || } || ||void cls(void) || { || show_mouse(NULL); || clear_to_color(0,BACKGROUND_COLOR); || status_string[sprintf(status_string+19,"%.1f",phi)+19]=32; || textout(0,0,status_string,statusx,statusy,textforegroundcolor); || } || ||void init_arrays(void) || { || int i=array_size-1; || || || cm=malloc(3*sizeof(rtype)*array_size); /* ! we allocate #of ||needed times memory */ || cxphi=malloc(4*sizeof(trigtype)*array_size); || ak=malloc(2*sizeof(stype)*array_size); || || a=cm+sizeof(rtype)*array_size; /* then we adjust the pointers ! */ || le=a+sizeof(rtype)*array_size; || || sxphi=cxphi+sizeof(trigtype)*array_size; /* Please adjust ||the #times and add */ || b=sxphi+sizeof(trigtype)*array_size; /* assignments here ||appropriately ! */ || an=b+sizeof(trigtype)*array_size; || || ek=ak+sizeof(stype)*array_size; || || || if(cm==NULL||cxphi==NULL||ak==NULL) /* so we also have to ||check less! adjust ! */ || { || printf("nomemstring"); || exit(0); || } || || do || { || cm[sizeof(rtype)*i]=id; || } || while(i--); || || cls(); || } || ||void freeall(void) || { || if(cm==0||cxphi==0||ak==0) /* so we also have to check ||less! adjust ! */ || { || printf("0?"); || while(!kbhit()); exit(0); || } || free(cm); || free(cxphi); || free(ak); || } || ||void setupall(void) || { || atexit(freeall); || init_arrays(); || } || || || ||I am looking myself for the error, but perhaps someone else ||is quicker; it's really bothering me. || ||Lars || || ||