有关堆栈溢出(in vs 2005)的读书笔记--堆栈中 申请大数组
部分原創
?
| 66下一步 | Vs 堆棧 理論修改的上限是多少 ?
問題2: 全局變量去 最大的上限是多少?
堆棧(休養版本測試)
2012年3月31日17:31:27 3月
? |
?
?
?
從 10^5到 10^6數組,導致vs…
當前:搭好腳手架。
?
改進: 修改vs系數。(byte 為單位i)
改進二: 細化 時間粒度。
?
?
以上是我在項目匯總的修改。下面是閱讀筆記
?
?
?
?
?
test dword ptr [eax],eax ; probe page.
2010年05月18日 星期二 20:19
局部數組變量定義超過所分配的最大空間
-----------------------------------
Posts Tagged 變量
局部數組變量定義所分配的最大空間為多少?如何設置大小
有兩個程序
A:
B:
| int?nArray[260000] = {0}; | //66 大于 1m了 |
?
大家通過運行可以發現,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就都可以通過了
?
源文檔 <http://hi.baidu.com/linzch/blog/item/e5107ff0a6cf6ccf7831aa02.html>
?
轉載于:https://www.cnblogs.com/titer1/archive/2012/03/31/2427422.html
總結
以上是生活随笔為你收集整理的有关堆栈溢出(in vs 2005)的读书笔记--堆栈中 申请大数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐藏无线网络技巧
- 下一篇: arguments.callee()事例