Interview Question
Developer Program EngineersCountry: India
This will solve the issue temporarily.
I need a programming technique i.e we write our program in such a way that it deals with above described problem optimally.
increasing stack size is helpful, but to what limit can we increase? second thing is, we may come across above situations every time, so increasing stack may not be required.
so I need a dynamic way to deal with it..
Hi
Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:
call getrlimit to get current stack size
if current size < required stack size then
call setrlimit to increase stack size to required size
In C that might be coded something like this:
#include <sys/resource.h>
int main (int argc, char **argv)
{
const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
// ...
return 0;
}
I'm not sure whether you're asking how to increase the stack limit, how to emulate a function call stack using a loop and a stack datastructure, or how to debug a situation where a bug results in runaway recursion and a stack overflow.
Hi,
My query was to know a optimal programing technique to deal with the problem as described below.
like i have a program where, there is a multilevel of invocation of methods within from prev method.
like: I invoked method A(), which in turn calls method B() within from itself. Now B() will get executed and its activation frame will occupy space in stack. before B() gets completed , it calls C(), later C() calls another D().... etc etc....
so if this continues , at one point of time, we will exhaust the allocated stack space for our program(process).
if such situation arises, whats the best programming technique to deal with this ?
I am not talking about increasing the stack size.
Hi,
My query was to know a optimal programing technique to deal with the problem as described below.
like i have a program where, there is a multilevel of invocation of methods within from prev method.
like: I invoked method A(), which in turn calls method B() within from itself. Now B() will get executed and its activation frame will occupy space in stack. before B() gets completed , it calls C(), later C() calls another D().... etc etc....
so if this continues , at one point of time, we will exhaust the allocated stack space for our program(process).
if such situation arises, whats the best programming technique to deal with this ?
I am not talking about increasing the stack size.
I suppose the question would really be why your program's stack becomes so big. Are you seeing more stack frames than you're supposed to be seeing because of a bug in your code?
in my opinion it is *really hard* to overflow the stack (which is about 8mb in linux) unless you program has unterminated recursions or plays around with stack frame pointer..
though one quite natural suggestion would be not to pass the function parameters "by value" and try to minimize the number of formal parameters (e.g, using global variables) especially for recursive calls
Hi
Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:
call getrlimit to get current stack size
if current size < required stack size then
call setrlimit to increase stack size to required size
In C that might be coded something like this:
- rahul chaudhary March 20, 2012