[comp.lang.c] What goes on stack?

roetzhei@sdsu.UUCP (William Roetzheim) (10/20/88)

  Help!  I'm working with Turbo C version 1.5.  My program is large (about
30,000 lines) and deals with large arrays (static and dynamic) and many
string literals in the code.  The stack size required is huge.  Anything
less than about 55K for a stacksize causes a stack overflow on program
execution.  I've looked at the obvious:

	1.  I don't do any recursion;

	2.  I don't pass a huge number of paramaters to anything, maybe
	    an average of two or three paramaters;

	3.  The program is not heavily nested.  From a top level routine
	    down to the lowest level 'put a character to the screen' I 
	    probably have 15 levels of nesting.

  Other than return addresses, register values, and paramaters, what else
does Turbo C put on the stack?  What the heck would cause it to grow so 
big?  This really is a problem because Turbo C appears to allocate the stack
in the global data segment (is this true), thus leaving me no room for some
global data I want to work with.  Correct me if I'm wrong, but passing
arrays (including arrays of pointers) should be no problem because they
are passed by reference, thus costing only 4 bytes (I'm using the large
memory model).

  Any help would be appreciated.

William

swilson%thetone@Sun.COM (Scott Wilson) (10/21/88)

>  Help!  I'm working with Turbo C version 1.5.  My program is large (about
>30,000 lines) and deals with large arrays (static and dynamic) and many
>[...]
>  Other than return addresses, register values, and paramaters, what else
>does Turbo C put on the stack?

The other important category that uses stack space is the non-register
local variables of a function.  If the "large arrays (... dynamic)" that
you refer to are a function's local data (as opposed to mallocing space
for an array) then this is probably where your problem is.


--
Scott Wilson		arpa: swilson@sun.com
Sun Microsystems	uucp: ...!sun!swilson
Mt. View, CA

lazer@mnetor.UUCP (Lazer Danzinger) (10/21/88)

In article <3221@sdsu.UUCP> roetzhei@sdsu.UCSD.EDU (William Roetzheim) writes:
>
>  Help!  I'm working with Turbo C version 1.5.  My program is large (about
>30,000 lines) and deals with large arrays (static and dynamic) and many
>string literals in the code.  The stack size required is huge.  Anything
>less than about 55K for a stacksize causes a stack overflow on program
>execution.  
>
>  Other than return addresses, register values, and paramaters, what else
>does Turbo C put on the stack?  What the heck would cause it to grow so 
>big?  
>

    As far as I know, it is not at all uncommon (perhaps, even standard
    practice) for all dynamic variables, including "large (dynamic) arrays" 
    to be allocated from stack space. This is very likely your problem.

    Depending on the particulars, the following may be possible solutions:

    1. Allocate memory for dynamic variables (your large arrays) at
       run-time, using a routine like malloc. Then free the storage
       at the end of the module. 

    2. (This is a little ugly: ) If speed is not too much of an issue,
       try using disk storage instead of memory.

    3. Check your Turbo manual to find out if there is a way of specifying
       a larger stack size.

    4. Although string literals do not come off the stack,
       if you are using the same literals more than once, it is a good
       practice to strcpy them into externs. This, besides conserving
       sometimes oodles of space, also makes program modification
       a lot easier.


-- 
-----------------------------------------------------------------------------
Lazer Danzinger      | "Ben Zoma said, 'Who is wise?  One who learns from 
lazer@mnetor         | every  person...Who is courageous?  One who conquers 
                     | his [evil] inclination...Who is wealthy?  One who is 
                     | satisfied with his portion...Who is respected?  One who 
(416) 475-8980       |  respects his fellow man...'"         -- Avot 4:1
----------------------------------------------------------------------------

paik@ati.tis.llnl.gov (Yunki Paik) (10/22/88)

In article <73872@sun.uucp> swilson@sun.UUCP (Scott Wilson) writes:
> [...]
>The other important category that uses stack space is the non-register
>local variables of a function.  [..]
>

 If they are not declared as static, I would think.

---
paik@ati.tis.llnl.gov 

gwyn@smoke.BRL.MIL (Doug Gwyn ) (10/23/88)

In article <3221@sdsu.UUCP> roetzhei@sdsu.UCSD.EDU (William Roetzheim) writes:
>  Other than return addresses, register values, and paramaters, what else
>does Turbo C put on the stack?

It very likely allocates automatic variables off the stack.  If you
have declared a large array as an auto variable, that's probably
the cause of your problem.

knudsen@ihlpl.ATT.COM (Knudsen) (10/25/88)

[Email failed, so this is posted]
All those "dynamic" arrays (known as "automatic" in C-ese
and "local" in CS courses) are also allocated on the stack.
That's what allows their storage to be freed up when their
function returns.

However, what will probably help in your case is to put *more*
of your data into automatics, not less.  You seem to be short
on global data space.  Be sure to declare
global or static only what really needs to be.
Of course any automatic variables in main() will persist
on the stack thruout the whole execution anyway, so no harm
if they're global.
-- 
Mike Knudsen  Bell Labs(AT&T)   att!ihlpl!knudsen
"Lawyers are like handguns and nuclear bombs.  Nobody likes them,
but the other guy's got one, so I better get one too."