From: tomster@bigwpi.WPI.EDU (Thomas Richard Dibble)
Newsgroups: comp.lang.c
Subject: Re: Need info for Turbo-C on storing lots of data within source.
Date: 22 Jan 93 21:59:15
Organization: ZikZak Corporation
NNTP-Posting-Host: bigwpi.wpi.edu
In-reply-to: noah@ucscb.UCSC.EDU's message of 22 Jan 93 05:43:45 GMT

>>>>> On 22 Jan 93 05:43:45 GMT, noah@ucscb.UCSC.EDU (Noah Spurrier) said:


  NS> Keywords: Turbo C data

  NS> I have a program that stores a compressed picture of its title
  NS> screen. The title screen is a graphic image. Also the program
  NS> stores some documentation.  The program is fairly short. I do
  NS> not want to store this data in separate files. What I did is use
  NS> HUGE arrays with initialization to the big chunk of data. This
  NS> works fine, but I notice that some people have had the program
  NS> crash with something like the following error:

  NS> STACK OVERFLOW

  NS> or

  NS> STACK FULL, SYSTEM HALTED

Say no more.  First, you need to understand how Intel machines
(running MS-DOS) handle their memory -- in 64k chunks.  No matter how
much memory a DOS machine has, it can only have 64k segments of
memory.  For more info, see any good basic or advanced C book and look
up Memory Models in the index.  I will assume you can find this
yourself and just give you the solution for this particular problem next.

  NS> Another person told me that these huge arrays were being stored
  NS> in the stack segment and that because they just sit on the stack
  NS> and never move this does not give my program much room to work
  NS> with.

  NS> Would simply making the array STATIC help things?

(no.)

  NS> I also farmalloc a 64K buffer, but I don't think that should
  NS> hurt the stack at all.

  NS> How can I get Turbo-C to store these variables in the data
  NS> segment instead of the stack?

Okay, here we go:

  NS> Here is an example of how I declare and initialize one of my
  NS> arrays: unsigned char far TITLE_DATA [(X_TITLE*Y_TITLE/4)] = {
  NS> ....  ....};

Stop right there.  First off, the maximum Stack Length is 0xffff, of
64k.  is X_TITLE*YTITLE/4 > 64k?  If so, your program probably won't
even get past the startup code.  Otherwise, I suspect that
X_TITLE*Y_TITLE/4 is a pretty large number, right?  I also suspect
that you have a few other variables in your program which (since they
are declared like this, but at the beginning of functions) also must
fit in the stack.  That's your problem.

The first solution you would try, if you really didn't want to think
about it at all is (since you are in Turbo C) put the line:
	extern unsigned _stacklen = 0xffff;
at the top of your main file to set the stack as large as it can get
and to switch into a memory model which can support this size of a
stack.
	But that won't work.  Or it won't work once you start adding
new functions (and therefore, variables) on top of your basic program.

	What you want to do is just put a pointer to that humungous
array on the stack.  This is done by putting the line:
	unsigned char far *TITLE_DATA;
in the global var's section of your file.  Then, in your main or in a
function you call before using TITLE_DATA, put the line:
	TITLE_DATA = (unsigned char far *)calloc(X_.../4,sizeof(char));
If X_TITLE*Y_TITLE/4 is greater than 64k, you will have to split this
up into 64k chunks (say, two chunks of X_TITLE*Y_TITLE/8, etc).  THEN
write data to the memory locations you just alloc'd.  Remember to free
TITLE_DATA before you exit();

  NS> Thanks for even bothering to read this damn thing!

  NS> noah@ucscb.ucsc.edu

		---- tomster@wpi.wpi.edu

--
/****************************************************************************\
| "I sometimes used to try to catch her, :	FROM:   tomster@wpi.wpi.edu  |
| but never even caught her name."	 :	TO:	who(m)ever reads it  |
|	---- the cure			 :	CC:	programming language |
\vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/
Xref: sparky comp.lang.c:20146 comp.std.c:3406