c++ - UNIX: What should be Stack Size (ulimit -s) in UNIX? -


how can calculate minimum stack size required program in unix, program never crashed.

suppose program

int main() {          int number;          number++;          return 0; } 

1) can stack size requried run program? how calculated ?

2) unix system gives ulimit -s 512000. value 512mb required small program?

3) , if have big program having multithreads, 500 functions, including libraries, macros, dynamically allocated memory etc. how stack size required ?

most people rely on stack being “large” , programs not using of it, because size has been set large programs fail because run out of stack space unless use large arrays automatic storage duration.

this engineering failure, in sense not engineering: known , largely preventable source of complete failure uncontrolled.

in general, can difficult compute actual stack needs of program. when there recursion, compiler cannot predict how many times routine called recursively, cannot know how many times routine need stack space. complication calls addresses prepared @ run-time, such calls virtual functions or through other pointers-to-functions.

however, compilers , linkers provide assistance. routine uses fixed amount of stack space, compiler, in theory, provide information. routine may include blocks or not executed, , each block might have different stack space requirements. interfere compiler providing fixed number routine, compiler might provide information each block individually and/or maximum routine.

linkers could, in theory, examine call tree and, if static , not recursive, provide maximum stack use linked program. provide stack use along particular call subchain (e.g., 1 routine through chain of calls leads same routine being called recursively) human apply knowledge of algorithm multiple stack use of subchain maximum number of times might called recursively).

i have not seen compilers or linkers these features. suggests there little economic incentive developing these features.

there times when stack use information important. operating system kernels may have stack more limited user processes, maximum stack use of kernel code ought (as engineering practice) calculated stack size can set appropriately (or code redesigned use less stack).

if have critical need calculating stack space requirements, can examine assembly code generated compiler. in many routines on many computing platforms, fixed number subtracted stack pointer @ beginning of routine. in absence of additional subtractions or “push” instructions, stack use of routine, excluding further stack used subroutines calls. however, routines may contain blocks of code contain additional stack allocations, must careful examining generated assembly code ensure have found stack adjustments.

routines may contain stack allocations computed @ run-time. in situation calculating stack space critical, might avoid writing code causes such allocations (e.g., avoid using c’s variable-length array feature).

once have determined stack use of each routine, can determine total stack use of program adding stack use of each routine along various routine-call paths (including stack use of start routine runs before main called).

this sort of calculation of stack use of complete program difficult , performed.

you can estimate stack use of program knowing how data “needs” work. each routine needs stack space objects uses automatic storage duration plus overhead saving processor registers, passing parameters subroutines, scratch work, , on. many things can alter stack use, estimate can obtained way. example, sample program not need space number. since no result of declaring or using number ever printed, optimizer in compiler can eliminate it. program needs stack space start routine; main routine not need except return zero.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -