delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/12/14/08:30:05.2

Message-ID: <3DFB3123.4020902@earthlink.net>
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
X-Accept-Language: en-us, en, de, fr, ru, el, zh
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with malloc and free
References: <ateunh$mv3$1 AT news DOT online DOT de> <atf1n7$o7e$1 AT news DOT online DOT de>
Lines: 134
Date: Sat, 14 Dec 2002 13:23:55 GMT
NNTP-Posting-Host: 67.210.12.112
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread1.prod.itd.earthlink.net 1039872235 67.210.12.112 (Sat, 14 Dec 2002 05:23:55 PST)
NNTP-Posting-Date: Sat, 14 Dec 2002 05:23:55 PST
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Lars O. Hansen wrote:
> Still stripped down, but this always crashes when compiled with gcc -pedantic -Wall is.c -lalleg
When I comment out the allegro calls, add the #inclusion of <stdio.h> to 
provide the declaration for the variadic function printf  (required bt the 
standard), and change the #inclusion of "stdlib.h", you code compiles and 
runs for me with no problems:

#include <stdio.h>              /* mha - added. */
#include <stdlib.h>             /* mha - "stdlib.h".  Are you sure you
                                  * don't have a "stdlib.h" masking
                                  * <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)
{
#if 0                           /* mha */
     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);
#endif
}

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?");
#if 0                           /* mha */
         while (!kbhit()) ;
#endif
         exit(0);
     }
     free(cm);
     free(cxphi);
     free(ak);
}

void setupall(void)
{
     atexit(freeall);
     init_arrays();
}










- Raw text -


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