TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Henk den Adel
from: Jeffrey Habets
date: 1997-03-25 14:48:00
subject: trap 0005

Hello Henk!

Saturday, March 22 1997, Henk den Adel wrote to Mike Phillips:

 HdA> I think the compiler wants its variables neatly bundled, so it can
 HdA> allocate adresses and the accompanying amount of memory, without having
 HdA> to gather variables all over the place. Furthermore, since sizeof(struct
 HdA> whatsoever) produces the right size of more complex type of variables,
 HdA> such as structs, the compiler should be able to determine the amount of
 HdA> memory which is needed for a struct, by sheer interpretation of the
 HdA> formal declaration of the struct, as provided in the function or its
 HdA> include files. BTW, i have declared my struct as a variable, not as a
 HdA> pointer.

 HdA> Could you be so kind to indicate which step in my reasoning is
 HdA> incorrect?

Everything is correct, except for one thing.. You are only looking at _one_
single function here..
What if your function calls another-one and that one again calls another
one..? And what about recursion. All those nested function calls need to
put their local variables on the stack.
So the maximum needed size for the stack strongly depends on program-flow
and can only be determined at runtime.

 MP>> That function needs 40k of stack. What if that function called a
 MP>> function in another source file that needs another 40k of stack. How
 MP>> is the compiler to know this?
 HdA> Just add another 40 kB of stack?

Not possible.. The stack-size must be allocated during program-startup, it
is a chunk of continuous memory that may not be fragmented.
What happens in OS/2 is that a stack of the size you specify is allocated
and commit on-the-fly with chunks of (if I'm not mistaken) 64kB.

 HdA> In my opinion the compiler can determine how much memory is needed to
 HdA> contain the variables which have been declared. Isn't that it what
 HdA> declarations are meant for?

Yep, on function-entry the compiler sees your declarations and calculates
the amount of stack size needed. It then reserves that much bytes on the
stack (this is called a stackframe) in which the variables are kept for the
life of the function.
Again, this al happens at runtime.. The way to determine your maximum
needed stacksize would be to go trough all possible program-flows with a
debugger or sampler running and look when the stack is at it's largest. But
why would you? Especially on OS/2 this I don't see the use for it.

Greetz from Maastricht/The Netherlands,
Jeffrey  >

--- GoldED/2 3.00.Alpha4+ /\ FMail/2 1.02+
* Origin: Documentation - The worst part of programming. (2:284/312.3)
SEEN-BY: 50/99 54/99 270/101 620/243 625/155 711/401 413 430 934 712/311 407
SEEN-BY: 712/505 506 517 623 624 704 713/317 800/1
@PATH: 284/312 3 5 280/801 270/101 712/624 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.