C++ stack overflow 局部数组变量定义超过所分配的最大空间
2011-12-26 14:28
483 查看
局部数组变量定义所分配的最大空间为多少?如何设置大小
有两个程序A:
#include "stdafx.h"
int _tmain(int
argc,
_TCHAR* argv[])
{
int nArray[256000]
= {0};
nArray[1]
= 5;
printf("array 1 is %d",nArray[1]);
return 0;
}
B:
#include "stdafx.h"
int _tmain(int
argc,
_TCHAR* argv[])
{
int nArray[260000]
= {0};
nArray[1]
= 5;
printf("array 1 is %d",nArray[1]);
return 0;
}
大家通过运行可以发现,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”
- 怪异问题 调试到位置(test dword ptr [eax],eax ; probe page.)局部数组变量定义所分配的最大空间为1M
- 堆栈溢出问题 调试到位置(test dword ptr [eax],eax ; probe page.)局部数组变量定义所分配的最大空间为1M
- 堆栈溢出问题 调试到位置(test dword ptr [eax],eax ; probe page.)局部数组变量定义所分配的最大空间为1M
- 局部变量,全局变量?探寻程序上面的栈(STACK)空间的最大值!
- C语言学习7 :二级指针定义,强制转换,多级指针初步,6级指针构造,错误应用*p=&a,错误应用 二级p2,void型指针的兼容性,malloc函数基本用法,malloc分配空间和堆栈空间的区别,验证malloc函数内存的分配,验证malloc函数的越界,内存泄漏,指针不能返回局部变量地址,内存分配
- heap stack 堆栈 全局变量 静态变量 进程空间 内存分配
- 从一个微型例子看“C/C++的内存分配机制”和“数组变量名与指针变量名”
- c、c++在定义变量,数组时的内存布局及内存字节对齐
- C++ 动态分配数组空间,以及opencv中Mat类的初始化
- 关于c语言中自动变量(局部自动变量)的空间的分配的问题
- 定义两个整型指针,分别用malloc、calloc对其分配空间保存3个元素,malloc分配的空间用memset清零,随机对数组进行赋值随机范围1-3,赋值后用memcmp比较两个数组。如果相同打印G
- C++命名空间里面定义变量出现重定义错误
- day18 函数定义、参数;名称空间;全局变量及局部变量。
- 全局变量,静态局部变量,局部变量空间的堆分配和栈分配
- thread线程栈size及局部变量最大可分配size【转】
- 从一个微型例子看“C/C++的内存分配机制”和“数组变量名与指针变量名”
- C++中局部和全局变量的地址分配
- 3.定义两个整型指针,分别用malloc、calloc对其分配空间保存3个元素,malloc分配的空间用memset清零,随机对数组进行赋值随机范围1-3,赋值后用memcmp比较两个数组。如果相同打
- 3 定义两个整型指针,分别用malloc、calloc对其分配空间保存3个元素,malloc分配的空间用memset清零,随机对数组进行赋值随机范围1-3,赋值后用memcmp比较两个数组。如果相同