局部数组变量定义超过所分配的最大空间

-----------------------------------

Posts Tagged 变量

有两个程序
A:

  1. #include "stdafx.h"
  2. int _tmain(int argc, _TCHAR* argv[])
  3. {
  4. int nArray[256000] = {0};
  5. nArray[1] = 5;
  6. printf("array 1 is %d",nArray[1]);
  7. return 0;
  8. }

B:

  1. #include "stdafx.h"
  2. int _tmain(int argc, _TCHAR* argv[])
  3. {
  4. int nArray[260000] = {0};
  5. nArray[1] = 5;
  6. printf("array 1 is %d",nArray[1]);
  7. return 0;
  8. }

大家通过运行可以发现,A是可以正常运行的,B虽然编译通过了,可是当运行时就会弹出错误

错误的原因,就是栈溢出
局部变量的申请空间是存放于栈中,windows里默认栈内存是1M

所以当申请空间大于1M时就会出现溢出错误

通过debug就会进入以下文件chkask.asm

page ,132
title chkstk – C stack checking routine
;***
;chkstk.asm – C stack checking routine
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; Provides support for automatic stack checking in C procedures
; when stack checking is enabled.
;
;*******************************************************************************
.xlist
include cruntime.inc
.list
; size of a page of memory
_PAGESIZE_ equ 1000h

CODESEG
page
;***
;_chkstk – check stack upon procedure entry
;
;Purpose:
; Provide stack checking on procedure entry. Method is to simply probe
; each page of memory required for the stack in descending order. This
; causes the necessary pages of memory to be allocated via the guard
; page scheme, if possible. In the event of failure, the OS raises the
; _XCPT_UNABLE_TO_GROW_STACK exception.
;
; NOTE: Currently, the (EAX < _PAGESIZE_) code path falls through
; to the "lastpage" label of the (EAX >= _PAGESIZE_) code path. This
; is small; a minor speed optimization would be to special case
; this up top. This would avoid the painful save/restore of
; ecx and would shorten the code path by 4-6 instructions.
;
;Entry:
; EAX = size of local frame
;
;Exit:
; ESP = new stackframe, if successful
;
;Uses:
; EAX
;
;Exceptions:
; _XCPT_GUARD_PAGE_VIOLATION – May be raised on a page probe. NEVER TRAP
; THIS!!!! It is used by the OS to grow the
; stack on demand.
; _XCPT_UNABLE_TO_GROW_STACK – The stack cannot be grown. More precisely,
; the attempt by the OS memory manager to
; allocate another guard page in response
; to a _XCPT_GUARD_PAGE_VIOLATION has
; failed.
;
;*******************************************************************************
public _alloca_probe
_chkstk proc
_alloca_probe = _chkstk
push ecx
; Calculate new TOS.
lea ecx, [esp] + 8 – 4 ; TOS before entering function + size for ret value
sub ecx, eax ; new TOS
; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.
sbb eax, eax ; 0 if CF==0, ~0 if CF==1
not eax ; ~0 if TOS did not wrapped around, 0 otherwise
and ecx, eax ; set to 0 if wraparound
mov eax, esp ; current TOS
and eax, not ( _PAGESIZE_ – 1) ; Round down to current page boundary
cs10:
cmp ecx, eax ; Is new TOS
jb short cs20 ; in probed page?
mov eax, ecx ; yes.
pop ecx
xchg esp, eax ; update esp
mov eax, dword ptr [eax] ; get return address
mov dword ptr [esp], eax ; and put it at new TOS
ret
; Find next lower page and probe
cs20:
sub eax, _PAGESIZE_ ; decrease by PAGESIZE
test dword ptr [eax],eax ; probe page.
jmp short cs10
_chkstk endp
end

提示栈溢出

所以解决此问题的方法就是扩大栈空间的大小

方法为

项目->属性->链接器->系统->堆栈保留大小

注:这里填的是字节数
如果你想把他扩大为2M的话,
1024*1024*2 = 2097152

然后再编译运行的话A,B就都可以通过了

test dword ptr [eax],eax ; probe page.相关推荐

  1. test dword ptr [eax],eax ; probe page. 是怎么回事?

    [问题描述] 编译时没有错误,但是调试或者运行时程序就报错了!一脸懵逼~~ debug调试提示: xxx.exe 中的 0x00e731d7 处未处理的异常: 0xC00000FD: Stack ov ...

  2. sub eax, _PAGESIZE; decrease by PAGESIZE test dword ptr [eax],eax ; probe page

    ; Find next lower page and probe cs20:sub eax, _PAGESIZE_ ; decrease by PAGESIZEtest dword ptr [eax] ...

  3. 堆栈溢出问题 调试到位置(test dword ptr [eax],eax ; probe page.)

    今天在写一个柜外清程序的时候,碰到一个很怪异的问题,调试程序的位置为:test dword ptr [eax],eax ; probe page.编译时没有错误,但是debug时就会跳出此问题. 经过 ...

  4. mov eax,dword ptr fs:[0] 指令

    FS寄存器指向当前活动线程的TEB结构(线程结构) 偏移 说明 000 指向SEH链指针 004 线程堆栈顶部 008 线程堆栈底部 00C SubSystemTib 010 FiberData 01 ...

  5. C代码+汇编 C的 函数汇编学习分析 rep stos dword ptr [edi]

    如分析有误,请在评论区中,指出 谢谢合作 主要是分析C的函数调用在汇编中的执行流程 本章主要是说一下函数调用时堆栈的变化,重点理解部分 C代码 #include"stdafx.h" ...

  6. stos dword ptr es:[edi]是什么意思

    stos dword ptr es:[edi] stos,串操作指令,该指令把寄存器知EAX中(0)的值存于以指针ES:EDI(ES=023H为段选择子,EDI=12EAB5H为线形地址偏移,经段描述 ...

  7. 汇编当中 dword ptr [ ] 是什么意思

    dword 双字 就是四个字节 ptr pointer缩写 即指针 []里的数据是一个地址值,这个地址指向一个双字型数据 比如mov eax, dword ptr [12345678] 把内存地址12 ...

  8. 汇编语言(王爽 第三版)检测点9.1 解决-a无法输入jmp dword ptr es:[1000h]

    第一个: assume cs:code,ds:data data segmentdb 0h data ends code segment start:mov ax,datamov ds,axmov b ...

  9. xor eax,eax

    xor eax,eax 异或运算,操作数两数相反为1:两数相同为0.由于这两个数相同,异或后等于清0 要比mov eax,0效率高

最新文章

  1. 51单片机怎么学啊?有推荐的线上网课和书籍么?
  2. Keil仿真出现Can not read register xx while CPU is running
  3. 刷新,开启云信下一个 5 年:专注做技术长跑里,最重要的事
  4. 了解DSP的体系结构
  5. php内核介绍及扩展开发指南,4.5. 发布扩展信息
  6. jq 获取引入页面url_jqURL获取页面URL及参数
  7. 源码0501-07-GCD的基本使用
  8. 牛客网编程题——字符串_确定两串乱序同构
  9. 网站建设-部署与发布
  10. 构造Linux流媒体服务器收藏
  11. 适合程序员的简历模板
  12. 纯HTML标签详解(摘自阿里西西)
  13. 【GamePlay】Unity手机屏幕UI适配问题
  14. 朋友圈发圈助手文案,头像,壁纸组合微信小程序源码下载
  15. Games101课程笔记_lecture20_color颜色
  16. 微信如何为好友设置特殊铃声提醒提示音
  17. java cmd进入目录_cmd进入某个目录
  18. Python描述 LeetCode 334. 递增的三元子序列
  19. java httpClient Digest Auth 认证
  20. 13.鼠标拖拽:案例:鼠标拖拽小方块,小方格跟着移动(桌面图标拖动)

热门文章

  1. 毕设日志——增加多尺度对应的不一样scale的RPN网络
  2. 11_条件随机场CRF1_统计学习方法
  3. 开发利器之IntelliJ IDEA学习笔记
  4. Yarn篇--搭建yarn集群
  5. iOS核心动画高级技术(九) 图层时间
  6. Hibernate(十二):HQL查询(一)
  7. 手把手教你在友善之臂tiny4412上用uboot启动Linux内核
  8. Eclipse/MyEclipse注释模板和格式化模板的使用
  9. VS2010工程转VS2005工程的方法
  10. SSIS常用的包—发送Email任务