56

当然のことながら、スタック割り当てに関する多くの関連する質問があります

スタックとヒープとは何ですか?

スタック サイズに制限があるのはなぜですか?

スタックおよびヒープ メモリのサイズ

ただし、さまざまな * nix マシンで bash コマンドを発行できます

ulimit -s unlimited

または csh コマンド

set stacksize unlimited

これにより、プログラムの実行方法がどのように変わりますか? プログラムまたはシステムのパフォーマンスに何らかの影響がありますか (たとえば、これがデフォルトにならない理由など)?

より多くのシステムの詳細が関連する場合、私は主に、x86_64 ハードウェアで実行されている Linux で GCC でコンパイルされたプログラムに関心があります。

4

3 に答える 3

29

When you call a function, a new "namespace" is allocated on the stack. That's how functions can have local variables. As functions call functions, which in turn call functions, we keep allocating more and more space on the stack to maintain this deep hierarchy of namespaces.

To curb programs using massive amounts of stack space, a limit is usually put in place via ulimit -s. If we remove that limit via ulimit -s unlimited, our programs will be able to keep gobbling up RAM for their evergrowing stack until eventually the system runs out of memory entirely.

int eat_stack_space(void) { return eat_stack_space(); }
// If we compile this with no optimization and run it, our computer could crash.

Usually, using a ton of stack space is accidental or a symptom of very deep recursion that probably should not be relying so much on the stack. Thus the stack limit.

Impact on performace is minor but does exist. Using the time command, I found that eliminating the stack limit increased performance by a few fractions of a second (at least on 64bit Ubuntu).

于 2013-01-23T03:05:45.257 に答える
2

スタックサイズ実際に無制限にすることができます。_STK_LIMデフォルトで、_STK_LIM_MAXはアーキテクチャごとに異なるもので、以下からわかるようにinclude/asm-generic/resource.h:

/*
 * RLIMIT_STACK default maximum - some architectures override it:
 */
#ifndef _STK_LIM_MAX
# define _STK_LIM_MAX           RLIM_INFINITY
#endif

この例からわかるように、一般的な値は無限大です。ここRLIM_INFINITYでも、一般的なケースでは次のように定義されています。

/*
 * SuS says limits have to be unsigned.
 * Which makes a ton more sense anyway.
 *
 * Some architectures override this (for compatibility reasons):
 */
#ifndef RLIM_INFINITY
# define RLIM_INFINITY          (~0UL)
#endif

したがって、本当の答えは次のとおりだと思います-スタックサイズはいくつかのアーキテクチャによって制限される可能性があり、無制限のスタックトレース_STK_LIM_MAXは定義されているものを意味し、無限の場合は無限です。それを無限に設定することの意味とそれが持つ可能性のある意味の詳細については、他の回答を参照してください。それは私のものよりもはるかに優れています。

于 2013-01-23T03:02:10.463 に答える