delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/12/14/05:45:07

From: "Lars O. Hansen" <lars DOT o DOT hansen AT gmx DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with malloc and free
Date: Sat, 14 Dec 2002 11:38:33 +0100
Organization: 1&1 Internet AG
Lines: 135
Message-ID: <atf1n7$o7e$1@news.online.de>
References: <ateunh$mv3$1 AT news DOT online DOT de>
NNTP-Posting-Host: p5083867a.dip0.t-ipconnect.de
X-Trace: news.online.de 1039862311 24814 80.131.134.122 (14 Dec 2002 10:38:31 GMT)
X-Complaints-To: abuse AT online DOT de
NNTP-Posting-Date: 14 Dec 2002 10:38:31 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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


- Raw text -


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