Re: Stack or Heap
- From: The Star King <jfb@xxxxxxxxx>
- Date: Mon, 7 Dec 2009 03:45:56 -0800 (PST)
On Dec 6, 8:53 pm, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:
Louis Krupp <lkrupp_nos...@xxxxxxxxxxxxxxxxx> wrote:
The Star King wrote:
will probably cause the computer to lock up with a segmentation fault..
Yes, the program will probably fail, but no, the computer won't "lock up."
Indeed. Segmentation faults are sort of user-hostile for debugging to
pretty much anyone who isn't a Unix geek, but they sure are a lot better
than (and are a completely different thing from) the computer "locking
Yes, I wasn't accurate. On windows, it will say "this program has
encountered an error and needs to close" there is then an option to
send a bug report to Microsoft. But not much use for debugging!
I think, in general, Fortran compiler vendors tend to assume (rightly
in my case!) that Fortran programmers are scientists and do not know
about stack/heap details. They therefore try to shield Fortran
programmers from such details.
Sadly, this is true.
They may try, but my observation is that quite often the end result is
exactly the opposite. There are *MANY* cases of people asking on this
newsgroup why their program failed when it turns out that the reason was
it ran out of stack space.
Personally I've never encountered such a problem in 15 years of
Fortran coding, so g77 and now gfortran seem pretty good at shielding
the likes of me at least!
On the other hand the very first C-program I wrote (recently)
Compiled without warning and immediately crashed with a segmentation
fault (in gcc). It took me ages to figure out what was wrong.
The combination of default low stack size
limits, combined with a default that puts many arrays on the stack
pretty much guarantees that you can't do much of substance without
learning enough about the stack to either know how to increase its size
limit or control what goes on it.
I understand you can increase the stack on your own computer, but what
if your program is designed to run on other people's computers? How
can a program check/adjust what the stack size is when it runs? When I
install a new program, I don't expect to alter the stack size for its
I still don't understand why default stack limits are so low. Yes, I've
seen explanations, but I still don't understand how they support the
typical defaults. The explanations basically seem to fall into one of
two casses: that there are trivial performance benefits in obscure
cases, or that one particular kind of broken program will abort fairly
quickly instead of hogging lots of memory before aborting. I just don't
understand how those benefits, which seem obscure to me, justify forcing
pretty much every programmer to learn enough about stack management to
know how to work around the effects of the defaults.
Yes, they are absurdly low. I also think its wrong that the above C
program, which conforms with the C-standard will compile without error
and then crash. Sure, gcc may be "complying" in behaving like this,
but compilers aught to go beyond complying and try to be useful as
well! There's no mention of this issue at all in C-programming books
(eg K+R). I know this is an implementation detail, but if people are
not taught this stuff in textbooks how are they meant to know?
I would prefer that the defaults were such that an average Fortran
programmer could get by without needing to know anything about system
stacks and heaps. Unless things have changed with recent compiler
releases (which is possible), that hasn't been the case.
I think this is the case with gfortran. You set up an array and it
"just works". I've never come across a case where it doesn't. I've
also had students, with no previous programming knowledge, writing
relatively complex Fortran code with gfortran and they've never had a
seg fault due to stack/heap issues. So kudos to gfortran.
BTW, if you ask a typical C-programmer why the above code crashes, he
will simply advise you not to use arrays! Most experienced C
programmers never use arrays prefering linked lists etc.
Based on my experience, I would say that this is not the case. Any
programmer who would tell you to use a linked list when you need an
array -- in any language -- is smoking something.
Indeed. Somone must have an awfully narrow view of what constitutes
"most experienced C programmers". I personally know lots of experienced
C programmers. Not one of them would make a statement like that - not
Of course I exaggerated to say "most experienced programmers".
However, there are many newsgroups where people have written a C-
programs like my one above and it's crashed and they want to know why
(you can imagine how common this problem is!). The answer is
invariably not to use arrays but to use malloc instead. The reason
they give is that it's impossible to find out what the stack size is
(especially if your program runs on someone else's computer) so you
just can't take the risk. Malloc gets memory from the heap so this
avoids all problems.
I also asked a colleague of mine about this and he gave the same
advice "dude, don't use arrays!". He's an experienced C programmer.
So, yes, I have not interviewed every C programmer, but all the ones I
spoke to (or heard from) said not to use arrays.
- Re: Stack or Heap
- From: Richard Maine
- Re: Stack or Heap
- Prev by Date: Re: transfer from real to logical
- Next by Date: Re: transfer from real to logical
- Previous by thread: Re: Stack or Heap
- Next by thread: Re: Stack or Heap