V
Victor
Hello,
I've got a situation in which the number of (valid) recursive calls I
make will cause stack overflow. I can use getrlimit (and setrlimit)
to test (and set) my current stack size. However, it is not as
straightforward to determine the base address for my stack space. The
approach I have taken is to save the address of an automatic variable
in main( ), and assume this is a fairly good indicator of my base
address. Then, I can store this in a non-stack variable for use in
the recursive function so that I don't have to keep passing it (and
therefore pushing it on the stack) for each recursive call.
The recursive function can then compare the address of one of its own
automatic variables to the "base" address computed in main( ), and
return with a stack overflow warning, i.e. before the stack is
overflowed. Furthermore, the recursive function can also store the
address of this automatic variable into a static variable to determine
its own stack size requirement on the next recursive call. By
comparison with the corresponding automatic variable in the previous
call, the recursive function can compute the difference.
My question, then is whether there isn't a more straightforward way to
accomplish this goal. I have a couple of issues with my current
methodology.
1) I seem to overflow the stack - that is, the operating system
detects a memory fault and terminates the process - before I've
reached what I believe to be the stack limit. This seems reasonable,
as my guess at the stack base is probably not entirely accurate.
However, I would like to have some accurate way of determining where
my stack limit is. To solve this issue, I simply scaled the available
stack space (what getrlimit reports) by, say 90%.
2) It seems like I'm having to jump through a lot of hoops to guard
against what I consider to be a fairly typical problem. Are there
either system calls that allow me to determine my base address or some
other means to guard against the overflow?
Thanks for your time. I hope that my question is understandable
despite its lack of source code!
Victor Weinstein
I've got a situation in which the number of (valid) recursive calls I
make will cause stack overflow. I can use getrlimit (and setrlimit)
to test (and set) my current stack size. However, it is not as
straightforward to determine the base address for my stack space. The
approach I have taken is to save the address of an automatic variable
in main( ), and assume this is a fairly good indicator of my base
address. Then, I can store this in a non-stack variable for use in
the recursive function so that I don't have to keep passing it (and
therefore pushing it on the stack) for each recursive call.
The recursive function can then compare the address of one of its own
automatic variables to the "base" address computed in main( ), and
return with a stack overflow warning, i.e. before the stack is
overflowed. Furthermore, the recursive function can also store the
address of this automatic variable into a static variable to determine
its own stack size requirement on the next recursive call. By
comparison with the corresponding automatic variable in the previous
call, the recursive function can compute the difference.
My question, then is whether there isn't a more straightforward way to
accomplish this goal. I have a couple of issues with my current
methodology.
1) I seem to overflow the stack - that is, the operating system
detects a memory fault and terminates the process - before I've
reached what I believe to be the stack limit. This seems reasonable,
as my guess at the stack base is probably not entirely accurate.
However, I would like to have some accurate way of determining where
my stack limit is. To solve this issue, I simply scaled the available
stack space (what getrlimit reports) by, say 90%.
2) It seems like I'm having to jump through a lot of hoops to guard
against what I consider to be a fairly typical problem. Are there
either system calls that allow me to determine my base address or some
other means to guard against the overflow?
Thanks for your time. I hope that my question is understandable
despite its lack of source code!
Victor Weinstein