www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/22/09:30:41

Message-ID: <33D585D8.5224@bj.col.co.cn>
Date: Tue, 22 Jul 1997 21:17:28 -0700
From: Tan Pinghui <ph DOT tan AT bj DOT col DOT co DOT cn>
Reply-To: ph DOT tan AT mh DOT bj DOT col DOT co DOT cn
MIME-Version: 1.0
To: Kenn White <kennw AT tidalwave DOT net>
CC: djgpp AT delorie DOT com
Subject: Re: Weird static/dynamic allocation crash...
References: <3 DOT 0 DOT 1 DOT 32 DOT 19970722035329 DOT 0069bfd4 AT postoffice DOT tidalwave DOT net>

Kenn White wrote:
> 
> Hello DJGPP gurus.
> 
> Here's a strange one for you.
> 
> I'm having trouble with the *very* simple program below.
> It seems that when the integer x is forceably set (i.e.,
> statically allocated), I get a run-time crash.  When
> is is malloced, however, everything is fine.
> 
> I have tried changing the command.com /e: to as high as 32768,
> with no luck.
> 
> Any ideas?
> 
> Kenn White
> kennw AT tidalwave DOT net
> 
> System: Gateway P-120 w/ 24MB RAM, Windows 95 full-screen DOS Window
> DPMI memory available: 17447 Kb
> DPMI swap space available: 4113 Kb
> TMP=C:\WINDOWS\TEMP
> TEMP=C:\WINDOWS\TEMP
> PROMPT=$p$g
> winbootdir=C:\WINDOWS
> COMSPEC=C:\COMMAND.COM
> PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\NORTON95;C:\DJGPP\BIN;
> SOUND=c:\CREATIVE
> MIDI=SYNTH:1 MAP:E MODE:0
> DJGPP=C:\DJGPP\DJGPP.ENV
> windir=C:\WINDOWS
> BLASTER=A220 I5 D1 H5 P330 T6 E620
> 
> ********************************************
> #include<stdlib.h>
> #include<stdio.h>
> #define SIZE 1000000
> #define STRANGE
> 
> int main(void) {
> 
> #ifdef STRANGE
>  int a, x[SIZE];
> #endif

if you define x[SIZE] this way, the memory is allocated from the stack,
but you didn't have so large a stack, so you got a crash.

> 
> #ifndef STRANGE
>  int a, *x;
>  x=malloc(SIZE*sizeof(int));
>  if (x==NULL) {
>     printf("Malloc fail.\n");
>     exit(0);
>   }
>  printf("Passed malloc \n");
> #endif

here, the memory is allocated from the heap, so it works.

> 
> for (a=0; a<SIZE; a++)
>  x[a]=a;
> printf("Finished loop fill. \n");
> return 0;
> }
> 
> ***************************************************************
> This is the output when x is set statically (STRANGE defined):
> ****************************************************************
> 
> C:\32c>q
> 
> Exiting due to signal SIGSEGV
> Stack Fault at eip=00001571
> eax=00000000 ebx=00142160 ecx=00000000 edx=0000033e esi=00000054 edi=0000c4d0
> ebp=0013ffd4 esp=ffd6f6d0 program=C:\32C\Q.EXE
> cs: sel=00a7  base=82b86000  limit=0014ffff
> ds: sel=00af  base=82b86000  limit=0014ffff
> es: sel=00af  base=82b86000  limit=0014ffff
> fs: sel=0087  base=0000bfb0  limit=0000ffff
> gs: sel=00bf  base=00000000  limit=ffffffff
> ss: sel=00af  base=82b86000  limit=0014ffff
> 
> Call frame traceback EIPs:
>   0x00001571
> 
> *********************************************************************
> This is the output with dynamic allocation (STRANGE commented out)
> *********************************************************************
> 
> C:\32c>q
> Passed malloc
> Finished loop fill.

if you move the x[SIZE] declaration out of main(), ie, if you define
a global x[SIZE], it works, too. 
or you can enlarge your stack.

- Raw text -


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