Re: Recovering from out of memory on the stack



Flash Gordon wrote:
jacob navia wrote, On 10/01/08 20:27:
You can have an idea of how much stack you are using if you do
the following

char *StartOfStack;
int main(...)
{
char foo;

StartOfStack = &foo;

Of course, if you do not call main recursively the compiler might decide to allocate foo where it allocates "global" variables. I can see reasons why this optimisation might make sense.


You are daydreaming.
I bet you can't name a single compiler that does that kind of nonsense!

}

This will tell you approximately WHERE the stack begins.

You can see how much you have used if in the function "fn" you do:

int fn(void)
{
char foo;
char *CurrentStack = &foo;

size_t StackSize = CurrentStack - StartOfStack;

Of course, if the stack grows in the opposite direction (as it does on the PowerPC) it could give very unexpected results.

And the fix is to take the absolute value... We are interested in the
distance.

Not to mention on embedded compilers which people have posted about as recently as this week that do things like allocating *all* local variables statically.


Who cares?

We are speaking about STACK OVERFLOW here, so those compilers are
OFF TOPIC for this thread.


You can then TEST before you call a function that uses a lot of stack
if you are beyond some limit.

However you have no way of knowing (in general) what limit you need to test against, so it does not help you.


Of course you know, and if you don't you can measure it with a program
like the above one.

#include <stdio.h>
char *StartOfStack;
int main(int argc,char *argv[])
{
char foo,*p;
char m[8192*16];
if (argc > 0)
p = StartOfStack = &foo;
else
p = &foo;
printf("Stack %d\n",StartOfStack-p);
main(0,NULL);
// Not reached :-)
}

Output:
Stack 0
Stack 131100
Stack 262200
Stack 393300
Stack 524400
Stack 655500
Stack 786600

You can adjust the size of the array to make more finer measures.

Of course this is very compiler specific too, but should work
in most systems.

Apart from PowerPC and related processors (because you have the stack direction wrong), so it won't work on big iron (or even relatively small servers like those we run our accounts system on in my company) from IBM or Apples from before they switched to x86 processors, or the afore mentioned embedded compilers...



HELLO HELLO

This thread is about stack overflows!

We are speaking about stack overflows here.

http://dictionary.reference.com/browse/pedant

pedant

–noun
1.a person who makes an excessive or inappropriate display of learning.
2.a person who overemphasizes rules or minor details.
3.a person who adheres rigidly to book knowledge without regard to common sense.


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
.



Relevant Pages

  • Re: Question about function call in a microprocessor?
    ... > or subroutine call in a assembly code. ... > about the stack here. ... > foo uses lets says 5 local varaibles. ... compiler and look at the output listing. ...
    (sci.electronics.design)
  • Re: Verbose functional languages?
    ... Give me staging or room to convince the compiler that it can inline ... heuristic you need to get code that runs faster than lex is clear, ... let rec foo s i = ... (match char 'a' s j with ...
    (comp.lang.functional)
  • Re: Horses & water
    ... foo (char *bar) { ... compiled with the same compiler, ... If you think it's just a nul-terminated string, and the "size" of what foobar points to is whatever size of that remaining string is, then you might be severly borked, because bar might not be used as a string at all, but might in fact just be an array of 8-bit bytes. ...
    (comp.os.vms)
  • Re: Where are returned values stored?
    ... >>the point when Foo returns (return effectively does ... >>Once the compiler knows that the caller's use of the returned ... >>value S is no longer required, the stack frame can be shrunk ... > variable that is allocated on the secondary stack. ...
    (comp.lang.ada)
  • Re: Need some info on const char **
    ... in foo(although my compiler still complains and I ... haven't yet figured out why - or what what nasty things ... I still could do in foo();-) ... char *StringArrayToMultistring(const char * const *stringarray, ...
    (comp.lang.c)