函数堆栈的默认大小是多大?

u0116snail 2014-11-13 11:12:02
请问:

1, 函数的堆栈,默认的最大大小是多少?

2. 线程堆栈默认大小是多少? 1M ?

3, 函数的堆栈,和 线程的堆栈,有什么区别?
...全文
3211 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-11-14
  • 打赏
  • 举报
回复
引用 26 楼 u011642451 的回复:
[quote=引用 25 楼 zhao4zhong1 的回复:] /STACK (Stack Allocations) Home | Overview | How Do I | Linker Options The Stack Allocations (/STACK:reserve[,commit]) option sets the size of the stack in bytes. To find this option in the development environment, click Settings on the Project menu. Then click the Link tab, and click Output in the Category box. The Reserve text box (or in the reserve argument on the command line) specifies the total stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds up the specified value to the nearest 4 bytes. The optional value specified in the Commit text box (or in the commit argument on the command line) is subject to interpretation by the operating system. In Windows NT, it specifies the amount of physical memory to allocate at a time. Committed virtual memory causes space to be reserved in the paging file. A higher commit value saves time when the application needs more stack space, but increases the memory requirements and possibly the startup time. Specify the reserve and commit values in decimal or C-language notation. Another way to set the size of the stack is with the STACKSIZE statement in a module-definition (.DEF) file. STACKSIZE overrides the Stack Allocations (/STACK) option if both are specified. You can change the stack after the .EXE file is built by using the EDITBIN tool.
Kao,还是你牛逼 下次可以把这个字体弄小写,不要搞得这么大……[/quote] 我还想用更大字体对应的图片代替呢!下次如果有人再问这个问题。
勤奋的小游侠 2014-11-13
  • 打赏
  • 举报
回复
引用 4 楼 u011642451 的回复:
[quote=引用 2 楼 u011642451 的回复:] 下面是我的测试代码,我发现一个很奇怪的现象: 编译器是 VS2010,Debug模式下的
void  Func()
{
	char *p = (char*)_alloca(1018092);  //Stack overflow 栈溢出

	//奇怪的现象:
		//编译器运行第一次,不会奔溃
		//清理后,再运行一次,也不会崩溃
		//不清理,总是运行,就会崩溃
}

int main(int argc, char *argv[])
{
	Func();
	return 0;
}
谁能帮我解释一下,这是为什么?先谢谢了[/quote] 堆和栈是不一样的。stack 和 heap。stack有时又翻译成堆栈。heap通常只翻译成堆。 你问的stack在这里应该是 栈。 你的代码里清理和不清理是什么意思?没有看到有清理空间的代码?
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 5 楼 binaryhead 的回复:
[quote=引用 楼主 u011642451 的回复:] 请问: 1, 函数的堆栈,默认的最大大小是多少? 2. 线程堆栈默认大小是多少? 1M ? 3, 函数的堆栈,和 线程的堆栈,有什么区别?
看你在什么平台上,Windows平台上,居我所知: 1、一个函数并不限制堆栈大小,而是函数被调用线程堆栈大小有限制。 2、线程堆栈大小在线程被创建的时候设定,具体参考创建API的说明。没有显式创建线程的程序其实是Windows帮你创建的进程缺省主线程,这时,线程的堆栈限制就是进程的堆栈限制。 3、VC6链接器在没有指定保留堆栈大小的情况下,缺省值是1MB,当然你可以改变缺省设置,而且因为是保留的缘故,在设置比较大值的情况下(比如16MB),操作系统初始并不提交那么多,根据程序运行情况增加提交,直到保留的空间被全部用光。 4、或许你问函数堆栈大小限制的本意是想问:编译器对函数内部声明的局部数组空间的最大限制是多少。这个在没找到编译器的官方文档前,只有具体试验才知道。 最后,请分清栈和堆的区别,有时有人把栈称为堆栈,此时,尽管堆栈中有一个堆字,但与堆没有关系。堆的概念请用搜索引擎。 [/quote] 我具体实验过了,你看你楼上,我贴的代码
二进制脑袋 2014-11-13
  • 打赏
  • 举报
回复
引用 4 楼 u011642451 的回复:
[quote=引用 2 楼 u011642451 的回复:] 下面是我的测试代码,我发现一个很奇怪的现象: 编译器是 VS2010,Debug模式下的
void  Func()
{
	char *p = (char*)_alloca(1018092);  //Stack overflow 栈溢出

	//奇怪的现象:
		//编译器运行第一次,不会奔溃
		//清理后,再运行一次,也不会崩溃
		//不清理,总是运行,就会崩溃
}

int main(int argc, char *argv[])
{
	Func();
	return 0;
}
谁能帮我解释一下,这是为什么?先谢谢了[/quote] 应该是你没有释放堆上所分配内存的缘故。
二进制脑袋 2014-11-13
  • 打赏
  • 举报
回复
引用 楼主 u011642451 的回复:
请问: 1, 函数的堆栈,默认的最大大小是多少? 2. 线程堆栈默认大小是多少? 1M ? 3, 函数的堆栈,和 线程的堆栈,有什么区别?
看你在什么平台上,Windows平台上,居我所知: 1、一个函数并不限制堆栈大小,而是函数被调用线程堆栈大小有限制。 2、线程堆栈大小在线程被创建的时候设定,具体参考创建API的说明。没有显式创建线程的程序其实是Windows帮你创建的进程缺省主线程,这时,线程的堆栈限制就是进程的堆栈限制。 3、VC6链接器在没有指定保留堆栈大小的情况下,缺省值是1MB,当然你可以改变缺省设置,而且因为是保留的缘故,在设置比较大值的情况下(比如16MB),操作系统初始并不提交那么多,根据程序运行情况增加提交,直到保留的空间被全部用光。 4、或许你问函数堆栈大小限制的本意是想问:编译器对函数内部声明的局部数组空间的最大限制是多少。这个在没找到编译器的官方文档前,只有具体试验才知道。 最后,请分清栈和堆的区别,有时有人把栈称为堆栈,此时,尽管堆栈中有一个堆字,但与堆没有关系。堆的概念请用搜索引擎。
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 2 楼 u011642451 的回复:
下面是我的测试代码,我发现一个很奇怪的现象: 编译器是 VS2010,Debug模式下的
void  Func()
{
	char *p = (char*)_alloca(1018092);  //Stack overflow 栈溢出

	//奇怪的现象:
		//编译器运行第一次,不会奔溃
		//清理后,再运行一次,也不会崩溃
		//不清理,总是运行,就会崩溃
}

int main(int argc, char *argv[])
{
	Func();
	return 0;
}
谁能帮我解释一下,这是为什么?先谢谢了
追梦浮云 2014-11-13
  • 打赏
  • 举报
回复
堆栈意味着栈 ,我觉着函数是在线程中运行的,没有函数堆栈大小一说吧,另外线程堆栈据说默认为1M 在创建线程时有个参数可以设置的.
HANDLE WINAPI CreateThread(
  _In_opt_   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_       SIZE_T dwStackSize,
  _In_       LPTHREAD_START_ROUTINE lpStartAddress,
  _In_opt_   LPVOID lpParameter,
  _In_       DWORD dwCreationFlags,
  _Out_opt_  LPDWORD lpThreadId
);
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
下面是我的测试代码,我发现一个很奇怪的现象: 编译器是 VS2010,Debug模式下的
void  Func()
{
	char *p = (char*)_alloca(1018092);  //Stack overflow 栈溢出

	//奇怪的现象:
		//编译器运行第一次,不会奔溃
		//清理后,再运行一次,也不会崩溃
		//不清理,总是运行,就会崩溃
}

int main(int argc, char *argv[])
{
	Func();
	return 0;
}
t657645537 2014-11-13
  • 打赏
  • 举报
回复
1, 函数的堆栈,默认的最大大小是多少?默认貌似是1M,可修改。 2. 线程堆栈默认大小是多少? 1M ? 你指的1M,应该是线程函数堆栈默认大小是1M,线程堆栈还有可能是线程父子之间关系的堆栈,这个没听过有大小。 3, 函数的堆栈,和 线程的堆栈,有什么区别?同上
liangkdm 2014-11-13
  • 打赏
  • 举报
回复
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 25 楼 zhao4zhong1 的回复:
/STACK (Stack Allocations) Home | Overview | How Do I | Linker Options The Stack Allocations (/STACK:reserve[,commit]) option sets the size of the stack in bytes. To find this option in the development environment, click Settings on the Project menu. Then click the Link tab, and click Output in the Category box. The Reserve text box (or in the reserve argument on the command line) specifies the total stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds up the specified value to the nearest 4 bytes. The optional value specified in the Commit text box (or in the commit argument on the command line) is subject to interpretation by the operating system. In Windows NT, it specifies the amount of physical memory to allocate at a time. Committed virtual memory causes space to be reserved in the paging file. A higher commit value saves time when the application needs more stack space, but increases the memory requirements and possibly the startup time. Specify the reserve and commit values in decimal or C-language notation. Another way to set the size of the stack is with the STACKSIZE statement in a module-definition (.DEF) file. STACKSIZE overrides the Stack Allocations (/STACK) option if both are specified. You can change the stack after the .EXE file is built by using the EDITBIN tool.
Kao,还是你牛逼 下次可以把这个字体弄小写,不要搞得这么大……
赵4老师 2014-11-13
  • 打赏
  • 举报
回复
/STACK (Stack Allocations) Home | Overview | How Do I | Linker Options The Stack Allocations (/STACK:reserve[,commit]) option sets the size of the stack in bytes. To find this option in the development environment, click Settings on the Project menu. Then click the Link tab, and click Output in the Category box. The Reserve text box (or in the reserve argument on the command line) specifies the total stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds up the specified value to the nearest 4 bytes. The optional value specified in the Commit text box (or in the commit argument on the command line) is subject to interpretation by the operating system. In Windows NT, it specifies the amount of physical memory to allocate at a time. Committed virtual memory causes space to be reserved in the paging file. A higher commit value saves time when the application needs more stack space, but increases the memory requirements and possibly the startup time. Specify the reserve and commit values in decimal or C-language notation. Another way to set the size of the stack is with the STACKSIZE statement in a module-definition (.DEF) file. STACKSIZE overrides the Stack Allocations (/STACK) option if both are specified. You can change the stack after the .EXE file is built by using the EDITBIN tool.
二进制脑袋 2014-11-13
  • 打赏
  • 举报
回复
引用 23 楼 u011642451 的回复:
[quote=引用 22 楼 binaryhead 的回复:] 误会你的词藻风格了。本人水平很有限,但自认为人品还不赖吧,所以你一玩笑,我就有些急了。
[/quote]
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 22 楼 binaryhead 的回复:
误会你的词藻风格了。本人水平很有限,但自认为人品还不赖吧,所以你一玩笑,我就有些急了。
二进制脑袋 2014-11-13
  • 打赏
  • 举报
回复
引用 20 楼 u011642451 的回复:
[quote=引用 18 楼 binaryhead 的回复:] [quote=引用 17 楼 u011642451 的回复:] [quote=引用 15 楼 binaryhead 的回复:] [quote=引用 13 楼 u011642451 的回复:] [quote=引用 12 楼 binaryhead 的回复:] 更正、补充前面9楼:由于是在堆栈上分配的内存,MSDN上没有在See Also中为_alloca列出对应的释放例程(http://msdn.microsoft.com/zh-cn/subscriptions/wb1s57t5(v=vs.71).aspx)。请注意Requirements部分Compatibility列表,没有ANSI,说明是Microsoft的专有实现,函数的下划线前缀也暗示了这一点。即使抛开可移植性,放着Windows下应用程序几个GB大小的堆空间不用,非要使用很有限的栈来动态分配内存,请告诉我你这样用的理由。
你妹,你是来砸帖子的吧[/quote] 你姐,谁在跑题?楼主一开始提的问题是文字概念问题,我的回帖也是概念回答。后来楼主给出具体的代码问题,我就对楼主的代码问题讨论。谁在这里谈论与帖子无关的内容,其他网友自有眼睛。[/quote] 你眼睛要是亮着,就请看看第一楼的问题吧。我读的书少,不要骗我[/quote] 很好,我们就事论事好了。 1, 函数的堆栈,默认的最大大小是多少?默认貌似是1M,可修改。------- 请看我5楼第一点的回答,我是在纠正函数堆栈这个概念的提法,也许你不太明白我说了什么,但我认为我比1楼回答要更有责任精神一点,一个线程难道只有一个函数吗?关于可修改一点,我在第3点中也提到了。我骗你了吗? 2、 线程堆栈默认大小是多少? 1M ? 你指的1M,应该是线程函数堆栈默认大小是1M,线程堆栈还有可能是线程父子之间关系的堆栈,这个没听过有大小。--------我第5楼第3点也提到了,而且由于我只实验了VC6,我特意只给出了VC6下的情形,我骗你了吗? 至于你说书读得少,每个人读的书都不相同,懂得多的不要忽悠懂得少的,恐怕是个优点的吧?我读的书也不一定有你多,就算我读的比你多,但我忽悠你了吗? 2、[/quote] 您别较真呀,老夫玩笑性幽默辞藻,看把你给惹的,我就喜欢嘻嘻哈哈得,直言罪人请莫怪呀…… 要是规规矩矩的,说个话还瞻前顾后的,那以后还能不能开心地发帖、提问了?还能不能愉快地玩耍了……[/quote] 误会你的词藻风格了。本人水平很有限,但自认为人品还不赖吧,所以你一玩笑,我就有些急了。
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 19 楼 majia2011 的回复:
os分配的资源是给线程的,栈也是给线程的,函数只是线程的执行,其他的不会
赞!
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 18 楼 binaryhead 的回复:
[quote=引用 17 楼 u011642451 的回复:] [quote=引用 15 楼 binaryhead 的回复:] [quote=引用 13 楼 u011642451 的回复:] [quote=引用 12 楼 binaryhead 的回复:] 更正、补充前面9楼:由于是在堆栈上分配的内存,MSDN上没有在See Also中为_alloca列出对应的释放例程(http://msdn.microsoft.com/zh-cn/subscriptions/wb1s57t5(v=vs.71).aspx)。请注意Requirements部分Compatibility列表,没有ANSI,说明是Microsoft的专有实现,函数的下划线前缀也暗示了这一点。即使抛开可移植性,放着Windows下应用程序几个GB大小的堆空间不用,非要使用很有限的栈来动态分配内存,请告诉我你这样用的理由。
你妹,你是来砸帖子的吧[/quote] 你姐,谁在跑题?楼主一开始提的问题是文字概念问题,我的回帖也是概念回答。后来楼主给出具体的代码问题,我就对楼主的代码问题讨论。谁在这里谈论与帖子无关的内容,其他网友自有眼睛。[/quote] 你眼睛要是亮着,就请看看第一楼的问题吧。我读的书少,不要骗我[/quote] 很好,我们就事论事好了。 1, 函数的堆栈,默认的最大大小是多少?默认貌似是1M,可修改。------- 请看我5楼第一点的回答,我是在纠正函数堆栈这个概念的提法,也许你不太明白我说了什么,但我认为我比1楼回答要更有责任精神一点,一个线程难道只有一个函数吗?关于可修改一点,我在第3点中也提到了。我骗你了吗? 2、 线程堆栈默认大小是多少? 1M ? 你指的1M,应该是线程函数堆栈默认大小是1M,线程堆栈还有可能是线程父子之间关系的堆栈,这个没听过有大小。--------我第5楼第3点也提到了,而且由于我只实验了VC6,我特意只给出了VC6下的情形,我骗你了吗? 至于你说书读得少,每个人读的书都不相同,懂得多的不要忽悠懂得少的,恐怕是个优点的吧?我读的书也不一定有你多,就算我读的比你多,但我忽悠你了吗? 2、[/quote] 您别较真呀,老夫玩笑性幽默辞藻,看把你给惹的,我就喜欢嘻嘻哈哈得,直言罪人请莫怪呀…… 要是规规矩矩的,说个话还瞻前顾后的,那以后还能不能开心地发帖、提问了?还能不能愉快地玩耍了……
majia2011 2014-11-13
  • 打赏
  • 举报
回复
os分配的资源是给线程的,栈也是给线程的,函数只是线程的执行,其他的不会
二进制脑袋 2014-11-13
  • 打赏
  • 举报
回复
引用 17 楼 u011642451 的回复:
[quote=引用 15 楼 binaryhead 的回复:] [quote=引用 13 楼 u011642451 的回复:] [quote=引用 12 楼 binaryhead 的回复:] 更正、补充前面9楼:由于是在堆栈上分配的内存,MSDN上没有在See Also中为_alloca列出对应的释放例程(http://msdn.microsoft.com/zh-cn/subscriptions/wb1s57t5(v=vs.71).aspx)。请注意Requirements部分Compatibility列表,没有ANSI,说明是Microsoft的专有实现,函数的下划线前缀也暗示了这一点。即使抛开可移植性,放着Windows下应用程序几个GB大小的堆空间不用,非要使用很有限的栈来动态分配内存,请告诉我你这样用的理由。
你妹,你是来砸帖子的吧[/quote] 你姐,谁在跑题?楼主一开始提的问题是文字概念问题,我的回帖也是概念回答。后来楼主给出具体的代码问题,我就对楼主的代码问题讨论。谁在这里谈论与帖子无关的内容,其他网友自有眼睛。[/quote] 你眼睛要是亮着,就请看看第一楼的问题吧。我读的书少,不要骗我[/quote] 很好,我们就事论事好了。 1, 函数的堆栈,默认的最大大小是多少?默认貌似是1M,可修改。------- 请看我5楼第一点的回答,我是在纠正函数堆栈这个概念的提法,也许你不太明白我说了什么,但我认为我比1楼回答要更有责任精神一点,一个线程难道只有一个函数吗?关于可修改一点,我在第3点中也提到了。我骗你了吗? 2、 线程堆栈默认大小是多少? 1M ? 你指的1M,应该是线程函数堆栈默认大小是1M,线程堆栈还有可能是线程父子之间关系的堆栈,这个没听过有大小。--------我第5楼第3点也提到了,而且由于我只实验了VC6,我特意只给出了VC6下的情形,我骗你了吗? 至于你说书读得少,每个人读的书都不相同,懂得多的不要忽悠懂得少的,恐怕是个优点的吧?我读的书也不一定有你多,就算我读的比你多,但我忽悠你了吗? 2、
u0116snail 2014-11-13
  • 打赏
  • 举报
回复
引用 15 楼 binaryhead 的回复:
[quote=引用 13 楼 u011642451 的回复:] [quote=引用 12 楼 binaryhead 的回复:] 更正、补充前面9楼:由于是在堆栈上分配的内存,MSDN上没有在See Also中为_alloca列出对应的释放例程(http://msdn.microsoft.com/zh-cn/subscriptions/wb1s57t5(v=vs.71).aspx)。请注意Requirements部分Compatibility列表,没有ANSI,说明是Microsoft的专有实现,函数的下划线前缀也暗示了这一点。即使抛开可移植性,放着Windows下应用程序几个GB大小的堆空间不用,非要使用很有限的栈来动态分配内存,请告诉我你这样用的理由。
你妹,你是来砸帖子的吧[/quote] 你姐,谁在跑题?楼主一开始提的问题是文字概念问题,我的回帖也是概念回答。后来楼主给出具体的代码问题,我就对楼主的代码问题讨论。谁在这里谈论与帖子无关的内容,其他网友自有眼睛。[/quote] 你眼睛要是亮着,就请看看第一楼的问题吧。我读的书少,不要骗我
加载更多回复(8)

64,687

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧