473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Should running a program at the smallest call stack depth be pursued?


Is there any value to pursuing program designs that mimimize
the mainline call stack? For example, within main() I could
code up something like:

while(GetMsg(m))
DispatchMsg(m);

instead of doing Program.Run() from main() or even worse
calling Run() from the Program constructor.

Call Stack example 1:
main()
GetMsg()/DispatchMsg()

Call Stack example 2:

main()
Program::Program()
Program::Run()
GetMsg()/DispatchMsg()

Tony
Dec 7 '06 #1
6 1420
Tony wrote:
Is there any value to pursuing program designs that mimimize
the mainline call stack? For example, within main() I could
code up something like:

while(GetMsg(m))
DispatchMsg(m);

instead of doing Program.Run() from main() or even worse
calling Run() from the Program constructor.

Call Stack example 1:
main()
GetMsg()/DispatchMsg()

Call Stack example 2:

main()
Program::Program()
Program::Run()
GetMsg()/DispatchMsg()

Tony
Given that your platform appears to be Win32, no. For certain embedded
systems with fixed/minimal stack, the answer may be yes.

It's platform and application dependent, and YMMV.

Dec 7 '06 #2

Tony wrote:
Is there any value to pursuing program designs that mimimize
the mainline call stack?
That's a general question. As with any optimisation question, the
general answer is "no need". That should be your starting point. There
are two sets of circumstances where you will change from "no" to "yes".

1. You have proven that the depth of your call-stack makes it
impossible for your program to meet its requirements.[*]
2. You have sufficient knowledge and experience of your compiler and
target platform that you know in advance that the depth of call-stack
you would like to write will make it impossible for your program to
meet its requirements.

If either of the above applies, and only you know that, then make the
optimisation.
[*] In this case the requirements will presumably be in terms of stack
size.

Gavin Deane

Dec 8 '06 #3

"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
>
Tony wrote:
>Is there any value to pursuing program designs that mimimize
the mainline call stack?

That's a general question. As with any optimisation question, the
general answer is "no need".
I wasn't necessarily asking from an optimization standpoint. My concern
would be more from the robustness standpoint: if there's a larger surface
area for potential corruption, then maybe it's wise to minimize that.

Tony
Dec 8 '06 #4

Tony wrote:
"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...

Tony wrote:
Is there any value to pursuing program designs that mimimize
the mainline call stack?
That's a general question. As with any optimisation question, the
general answer is "no need".

I wasn't necessarily asking from an optimization standpoint. My concern
would be more from the robustness standpoint: if there's a larger surface
area for potential corruption, then maybe it's wise to minimize that.

Tony
Unless you have a specific threat in mind that you have very good
reason to think that trying to minimize stack height will help prevent
[note: I can't think of any], my feeling is that you're more likely to
introduce errors trying to minimize stack height than you are just
leaving it.

Evan

Dec 8 '06 #5

Tony wrote:
"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...

Tony wrote:
Is there any value to pursuing program designs that mimimize
the mainline call stack?
That's a general question. As with any optimisation question, the
general answer is "no need".

I wasn't necessarily asking from an optimization standpoint. My concern
would be more from the robustness standpoint: if there's a larger surface
area for potential corruption, then maybe it's wise to minimize that.
Well, no. Recursive functions /can/ be difficult to debug or validate.
They /can/ also be slower than their loopy counterparts. It is rare
that this is really important though and often times an algorithm is
just plain simpler in a recursive form.

Now, as to the general question. How would you alleviate the "problem"
of stack "depth" should you see such a problem? Well, the obvious
answer would be to pull all the functions you are calling up into their
caller so that instead of having a bunch of small, simple, easy to
manage functions you have one great big one. That's bad.

No, there is no need to pursue designs that minimize call depth...in
fact I would say they should be avoided in almost all, if not all,
situations.

Dec 8 '06 #6

"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>
Tony wrote:
>"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@16g2000cwy.googlegr oups.com...
>
Tony wrote:
Is there any value to pursuing program designs that mimimize
the mainline call stack?

That's a general question. As with any optimisation question, the
general answer is "no need".

I wasn't necessarily asking from an optimization standpoint. My concern
would be more from the robustness standpoint: if there's a larger surface
area for potential corruption, then maybe it's wise to minimize that.

Well, no. Recursive functions /can/ be difficult to debug or validate.
They /can/ also be slower than their loopy counterparts. It is rare
that this is really important though and often times an algorithm is
just plain simpler in a recursive form.

Now, as to the general question. How would you alleviate the "problem"
of stack "depth" should you see such a problem? Well, the obvious
answer would be to pull all the functions you are calling up into their
caller so that instead of having a bunch of small, simple, easy to
manage functions you have one great big one. That's bad.
I was only really concerned with the main() branch, pretty much like
my examples, rather than in general. I'm trying to come up with an entry
point mechanism other than main().
No, there is no need to pursue designs that minimize call depth...in
fact I would say they should be avoided in almost all, if not all,
situations.
Well that's some good news for someone considering dumping main()
then. :)

Tony
Dec 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: G Patel | last post by:
Can people please comment on the layout/style of my problem? The major issue I had was the layout. I ended up having to put a relatively large switch statement, inside an if statement, which is...
22
by: CK | last post by:
Hi EveryBody! This is the World Smallest Program in c. main(l ,a,n,d)char**a;{ for(d=atoi(a)/10*80- atoi(a)/5-596;n="@NKA\ CLCCGZAAQBEAADAFaISADJABBA^\ SNLGAQABDAXIMBAACTBATAHDBAN\...
13
by: Ben R. Bolton | last post by:
The documentation indicates that the threads "default stack size" is 1MB. The work "default" implies that it can be changed. Is it possible to change the StackSize in .NET? If so how? Is it...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
6
by: dspfun | last post by:
I would like to analyze my running c-program. What I would like to know for example is the range of the entire address space of my running c-program (memory reserved for/by the running program),...
13
by: Bob | last post by:
Hi, I have a newbie question. What is the smallest type object size? I am doing some calculation with individual bits and want to minimize memory usage. I though bool might do it but it seems...
2
by: PJ6 | last post by:
I have an algorithm that processes data recursively. When I'm testing it works fine, but when I feed it data from the actual application, I get "stack overflow". What bothers me is that the data my...
60
by: harshal | last post by:
Hi all, Can we read the stack frame's of the current process. as we know that whenever a function call is made in c new functions stack frame is created and pushed on to the stack. and when the...
4
by: lianne.ribeiro | last post by:
I am using Visual C++ 6 IDE,with 512MB RAM. I have coded a recursive function that has a correct end condition for recursion. There is no infinite recursion. For some input data,the recursive...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.