Re: Stack or Heap



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
up."

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)

int main()
{
int a[100][100][100];
}

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
benefit.

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
one.

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.

.



Relevant Pages

  • Re: ALLOCATABLE arrays
    ... || If try to create a very large array on the stack and you do not have enough ... || Allocating on the heap gives you access to a hell of a lot more memory (well ... and "heap" (and there probably are/were some computers which don't/didn't ... | Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Re: Need some help understanding array definitions
    ... Data structures defined with VARIABLE, CREATE, VALUE, CONSTANT, and related words are, indeed, all global. ... Unlike some languages, Forth doesn't discourage defining global data structures, but it's important to understand their proper use. ... They provide for "persistent" data, as well as space for strings and arrays. ... Strings and arrays should be in defined data structures and referenced by address and length or address on the stack. ...
    (comp.lang.forth)
  • Re: heap allocation of arrays
    ... | to force all arrays to be allocated on the heap. ... | the stack would be replaced with pointers on the stack. ... | heap is easier to detect than failure to allocate space on the ...
    (comp.lang.fortran)
  • Re: how to allow large static space allocation in c/c++?
    ... operator to allocate enough space, then we just simulate a 2-dim array, like ... >> just three big arrays, but after I compile and run this program, ... > these arrays on the stack, and the stack is not a really good ...
    (comp.unix.programmer)