程序与操作系统的问题,望高手赐教

wyc_ 2015-01-03 09:28:39
一个程序调入内存开始执行时,它的堆的大小还有栈的大小是怎么确定的?
还有既然虚拟存储空间可以很大,怎么感觉一个简单递归就经常出现堆栈溢出等问题呢?
比如一个最简单的程序只有一条输出语句,操作系统还会为它分配堆空间吗?
...全文
203 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-01-05
  • 打赏
  • 举报
回复
“给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
nadleeh 2015-01-05
  • 打赏
  • 举报
回复
引用 楼主 cumtwyc 的回复:
一个程序调入内存开始执行时,它的堆的大小还有栈的大小是怎么确定的? 还有既然虚拟存储空间可以很大,怎么感觉一个简单递归就经常出现堆栈溢出等问题呢? 比如一个最简单的程序只有一条输出语句,操作系统还会为它分配堆空间吗?
堆大小是运行时才确定的,不然也不用判断malloc的返回值了。 栈大小虽然最终体现在运行上,不过从源码级别也能够判断,大小和源码息息相关。 递归搞挂了肯定是没归回来,查查代码吧。
赵4老师 2015-01-04
  • 打赏
  • 举报
回复
《程序员的自我修养——链接、装载与库》
wyc_ 2015-01-04
  • 打赏
  • 举报
回复
引用 6 楼 C0909 的回复:
老许要老婆么 2015-01-04
  • 打赏
  • 举报
回复


不好意思 刚配图配错了
老许要老婆么 2015-01-04
  • 打赏
  • 举报
回复
栈是有限制大小的,好像是2M左右,具体忘了 楼主可以试试 创建一个数组 VS会报栈溢出的

wyc_ 2015-01-04
  • 打赏
  • 举报
回复
回答的都很好啊
一根烂笔头 2015-01-04
  • 打赏
  • 举报
回复
不同平台栈的大小可能不一样! Linux下,THREAD_SIZE_ORDER指定了栈的大小,通过alloc_thread_info进行分配。目前kernel-3.16源码THREAD_SIZE_ORDER值依旧为1,即进程分配的栈依旧是一页!至于页的大小有OS指定,可以是4k,1M等等
赵4老师 2015-01-04
  • 打赏
  • 举报
回复
/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.
一根烂笔头 2015-01-04
  • 打赏
  • 举报
回复
栈的大小一般都是一页的样子,而且不能扩充,由OS指定,而堆则大的多。理论上,以32位系统的1:3比例划分,4G可用内存,有3G都可以作为堆来分配。
Evankaka 2015-01-03
  • 打赏
  • 举报
回复
我看书写的是栈一般是向低字节分配的,一般有2M,也不是肯定的,还和系统有关。堆是向高字节分配的,所以理论来讲没有空间限制(还有硬盘空间呢!这个也可以拿来用)
fly_dragon_fly 2015-01-03
  • 打赏
  • 举报
回复
栈有限,递归不结束肯定溢出。
输出语句会用到c库或c++库,库会申请堆空间
encoderlee 2015-01-03
  • 打赏
  • 举报
回复
在Windows系统上程序运行起来后栈大小默认是1MB,当然这个值可以在编译程序的时候通过链接器命令来修改。 堆上内存空间需要程序向操作系统申请,比如malloc或new,不申请就不分配。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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