全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2190
推到 Plurk!
推到 Facebook!

我開一個array 因為太大..會出現Stack over flow 的狀況

尚未結案
weiliching
初階會員


發表:53
回覆:78
積分:31
註冊:2003-12-27

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-31 17:36:24 IP:210.243.xxx.xxx 未訂閱
var a:array[0..251,0..512,0..512] of double; begin 我開一個array 因為太大..會出現Stack over flow 的狀況 請問要如何解決呢?
rexchiu
中階會員


發表:14
回覆:88
積分:70
註冊:2002-03-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-02-01 15:32:34 IP:220.130.xxx.xxx 未訂閱
首先先問問自己,真的需要那麼大的三維陣列嘛? 先來看看你要的陣列有多大 第一維長度,0..251,所以其長度為252 第二維長度,0..512,            513 第三維     0..512,            513 你的陣列光元素就有252X513X513=66318588個 還不夠嚇人嘛? 好吧..我們在來看看他要吃多少記憶體. double 的大小為8byte,因此你的陣列要吃掉,66318558X8=530548704Byte 530548704/1024=518113.96875KByte==>518113.96875/1024==>505.97MB 夠嚇人了吧... 咦~~你說你還是要知道怎麼用喔? 好吧~~下面給你一個方法,使用動態陣列的方式去做
procedure TForm1.Button1Click(Sender: TObject);
var
 a:array of array of array of double;
 i,j,k :integer;
begin
  SetLength(A, 252);
  for I := Low(A) to High(A) do
  begin
    SetLength(A[I], 513);
    edit1.Text:=inttostr(i);
    for J := Low(A[I]) to High(A[I]) do
    begin
      setLength(A[i,j],513);
      edit2.Text:=inttostr(j);
      for K:= Low(A[I,J]) to High(A[I,J]) do
      begin
        edit3.Text:=inttostr(k);
        A[I,J,K]:=0;
        application.ProcessMessages;
      end;
    end;
  end;
  showmessage('Done');
end;
要跑這個,你的電腦要有多少記憶體該知道了吧! 我原本用128MB的電腦跑,一下子就吃光記憶體了.. 所以我用了一台512MB的電腦跑,截至目前為止,他還在跑.記憶體夠用 < > 光要處理那嘛大陣列的初始值,就要跑好久了..所以強烈建議你去檢討一下 是否真有必要,用那麼大一個陣列去處理事情!< > Best Regards, Rex Chiu
------
Best Regards,
Rex Chiu
weiliching
初階會員


發表:53
回覆:78
積分:31
註冊:2003-12-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-02-01 15:42:23 IP:210.243.xxx.xxx 未訂閱
我是主要處理影像的問題. 是用來處理一些k-means 的運算. 因為放在記憶体去跑速度才會快. 我之前是放到資料庫去跑.. 跑了三天還跑不出效果. 不過至少要有 [0..1024,0..7,0..7] 得陣列在跑. 且還有一個 [0..512,0..512] 的陣列. vincent wei
rexchiu
中階會員


發表:14
回覆:88
積分:70
註冊:2002-03-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-02-01 17:05:43 IP:220.130.xxx.xxx 未訂閱
再給你另一個方法,利用{$M}去設定程式的stack size 以下節錄自delphi help --------------------------------- Type        Parameter Syntax        {$M minstacksize,maxstacksize} {$MINSTACKSIZE number} {$MAXSTACKSIZE number} Default        {$M 16384,1048576} Scope        Global Remarks    The $M directive specifies an application's stack allocation parameters. minstacksize must be an integer number between 1024 and 2147483647 that specifies the minimum size of an application's stack, and maxstacksize must be an integer number between minstacksize and 2147483647 that specifies the maximum size of an application's stack. If there is not enough memory available to satisfy an application's minimum stack requirement, Windows will report an error upon attempting to start the application.    An application's stack is never allowed to grow larger than the maximum stack size. Any attempt to grow the stack beyond the maximum stack size causes an EStackOverflow exception to be raised. The $MINSTACKSIZE and $MAXSTACKSIZE directives allow the minimum and maximum stack sizes to be specified separately. The memory allocation directives are meaningful only in a program. They should not be used in a library or a unit.    -------------------------------------------------- 注意其中的數字是以 byte 計 以下是程式範例,由於使用靜態陣列,在一開始就會自動做初始化. 所以你如果要使用IDE上的RUN,一定要在有足夠記憶體的電腦中執行. 否則就請使用procject中的build,先編譯出執行檔,在copy到有足夠記憶體的 電腦中執行他.
//加上這個以便設定stack size
{$M 530548704,541034464}
procedure TForm1.Button1Click(Sender: TObject);
var
  a:array[0..251,0..512,0..512] of double;
  i,j,k:Integer;
begin
  for I := Low(A) to High(A) do
  begin
    edit1.Text:=inttostr(i);
    for J := Low(A[I]) to High(A[I]) do
    begin
      edit2.Text:=inttostr(j);
      for K:= Low(A[I,J]) to High(A[I,J]) do
      begin
        edit3.Text:=inttostr(k);
        A[I,J,K]:=1;
        application.ProcessMessages;
      end;
    end;
  end;
  showmessage('Done');
end;
------
Best Regards,
Rex Chiu
系統時間:2024-05-22 4:28:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!