Re: Recovering from out of memory on the stack
- From: jacob navia <jacob@xxxxxxxxxx>
- Date: Fri, 11 Jan 2008 00:43:55 +0100
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
.
- Follow-Ups:
- Re: Recovering from out of memory on the stack
- From: James Kuyper
- Re: Recovering from out of memory on the stack
- From: Flash Gordon
- Re: Recovering from out of memory on the stack
- From: Walter Roberson
- Re: Recovering from out of memory on the stack
- References:
- Recovering from out of memory on the stack
- From: sandysimons53
- Re: Recovering from out of memory on the stack
- From: jacob navia
- Re: Recovering from out of memory on the stack
- From: Flash Gordon
- Recovering from out of memory on the stack
- Prev by Date: Re: Recovering from out of memory on the stack
- Next by Date: Re: Recovering from out of memory on the stack - TPA
- Previous by thread: Re: Recovering from out of memory on the stack
- Next by thread: Re: Recovering from out of memory on the stack
- Index(es):
Relevant Pages
|