Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!decwrl!adobe!hawley From: hawley@adobe.COM (Steve Hawley) Newsgroups: comp.sys.mac.programmer Subject: Re: THINK C 4.0 bug? Message-ID: <6280@adobe.UUCP> Date: 5 Sep 90 17:06:23 GMT References: <5364@munnari.oz.au> Reply-To: hawley@adobe.UUCP (Steve Hawley) Organization: Adobe Systems Incorporated, Mountain View Lines: 59 In article <5364@munnari.oz.au> iand@mullian.ee.mu.OZ.AU (Ian Robert DOBSON) writes: >I just went out and bought THINK C 4.0 and have come across an interesting >bug. Using the "ANSI" and "MacTraps" libraries with default project settings >(384K, no 68020/68881 etc.) bombed the machine when executing the following >code: > >#include > >main() >{ > char array[32000]; > > printf("\nHello, world!\n"); >} Here's why it bombs: array is a local variable to the function main. array is allocated on the stack when main is entered. This allocation happens before the printf, and hoses the machine since 32000 bytes is really an awful lot to ask from the machine as local variable space for a single function, since the limit imposed by the compiler is 32K. If you REALLY feel you have to do this, try making your array static (which will limit you to only another 767 bytes of global and static data), which puts it in global space. Or (I know this isn't what you want to hear) allocate it on the fly from the heap. The heap will probably be much much larger than the stack. Remember the purpose of local variables: small amounts of storage for variables you will need temporarily that can be used re-entrantly (otherwise recursion won't work as well as you'd like it to). If you want to see death in a C environment, try the following program on a variety of machines: #include int deathcount = 0; death() { char a[1024]; /* 1K allocated on the stack */ a[0] = a[0]; /* prevent a from being linked out by an optimizer */ printf("%dK"\n", ++deathcount); fflush(stdout); death(); } main() { death(); } On a Sun 3/80 with all the wonders of UNIX memory management, it can only cope with 2036K (!!) before it core dumps. Try it on a Mac to see what it's limits are (I can't, since I don't have a Mac anymore (sniff)) under Think C. Steve Hawley hawley@adobe.com -- "I can always telephone, but I can't tell it much." -Roy Blount