[問題] C 大量宣告一維陣列

看板C_and_CPP (C/C++)作者 (燒仙草冰)時間14年前 (2011/08/17 19:44), 編輯推噓1(1022)
留言23則, 7人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VS2010 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 以使用動態記憶體配置 malloc 配置 之後 程式記憶體還是會overflow 餵入的資料(Input): 圖檔RAW 預期的正確結果(Expected Output): 預期因該要有8個320*240大小的一維陣列產生 但是只能產生5個 錯誤結果(Wrong Output): 目前只能產生5個 剩下3個會overflow 程式碼(Code):(請善用置底文網頁, 記得排版) int *dhes9, *dhes15, *dhes21,*dhes27, *dhes39, *dhes51, *dhes75, *dhes99; dhes9 =(int*) malloc(img_size * sizeof(int)); dhes15 =(int*) malloc(img_size * sizeof(int)); dhes21 = (int*)malloc(img_size * sizeof(int)); dhes27 =(int*) malloc(img_size * sizeof(int)); dhes39 =(int*) malloc(img_size * sizeof(int)); ------------------------------------------------------ dhes51 =(int*) malloc(img_size * sizeof(int)); dhes75 = (int*)malloc(img_size * sizeof(int)); dhes99 = (int*)malloc(img_size * sizeof(int)); 虛線以下只能宣告 不能寫值 若要寫值程式會當掉 meaning: 我把會使用到虛線以下的3個陣列拿掉之後 就可以執行,若是放入要寫入3個陣列的code下去 之後,程式就會當掉。 補充說明(Supplement): 下午有問過板上的高手們,建議我使用malloc做為記憶體宣告。 使用過後,還是有就有問題阿。 不過有改善的比較好就是了 所以再次發文,有沒有還可以改進的地方。 現在再寫SURF演算法,這個step只是寫mask運算 所以每一個陣列產生其實都差不多。code部分我有很仔細看過 並不會有overflow的狀況產生。 -- ˋ │ │ ˋ φhacoolman 現在簽博班的話, /│ │____ │ │\ alan99 碩班就能畢業囉! / │ │ │ │ \ 簽嗎不簽嗎 │ ╰────╯ ╰────╯ │ 簽嗎不簽嗎 不簽嗎不簽嗎不簽嗎不\ │ 嗎不簽嗎 不簽嗎不簽嗎不簽嗎不簽 \ ︶︶ / 嘿~嘿~嘿~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.130.34.134

08/17 19:57, , 1F
dhes9 =(short*) malloc(img_size * sizeof(short));
08/17 19:57, 1F

08/17 19:57, , 2F
dhes9 =(int*) malloc(img_size * sizeof(int));
08/17 19:57, 2F

08/17 19:58, , 3F
別混著用..
08/17 19:58, 3F

08/17 20:07, , 4F
喔~沒改到 趕緊來改一下 感恩
08/17 20:07, 4F
※ 編輯: jonnyldy 來自: 140.130.34.134 (08/17 20:18)

08/17 20:21, , 5F
你的記憶體有多大...
08/17 20:21, 5F

08/17 20:23, , 6F
其實全用 int 也可以過啦
08/17 20:23, 6F

08/17 20:45, , 7F
我比較建議你,去改你原篇的文章.因為這篇很容易被刪除
08/17 20:45, 7F

08/17 20:50, , 8F
要看內文的多寡
08/17 20:50, 8F

08/17 20:52, , 9F
可以把錯誤訊息po上來嗎? 就是vs當掉時,會有一個對話框
08/17 20:52, 9F

08/17 20:53, , 10F
這樣比較不會有瞎子摸象的感覺
08/17 20:53, 10F

08/17 21:50, , 11F
絕對是你的程式其它地方有bug啊
08/17 21:50, 11F

08/17 21:58, , 12F
我先補一下他原本寫法好了,改過後情況等他回報..
08/17 21:58, 12F

08/17 21:59, , 13F
int* dhes9=(int*) malloc(img_size * sizeof(short));
08/17 21:59, 13F

08/17 22:01, , 14F
覺得問題是在別的地方...只能慢慢trace了
08/17 22:01, 14F

08/18 08:17, , 15F
sizeof(short)<--是不是要改成sizeof(int)阿
08/18 08:17, 15F

08/18 09:42, , 16F
謝謝樓上諸位 用心幫我看CODE 我今天在一行一行看
08/18 09:42, 16F

08/18 09:43, , 17F
因為我寫的CODE 是先開另外一個檔案先寫一次 TEST OK
08/18 09:43, 17F

08/18 09:44, , 18F
再移到我要的code裡面所以我覺得因該都OK才對(自我感良好
08/18 09:44, 18F

08/18 09:45, , 19F
回復fjrejox 我的記憶體為2G
08/18 09:45, 19F

08/18 10:27, , 20F
如果你用malloc,有記得free掉嘛? 這也是個原因喔
08/18 10:27, 20F

08/18 14:51, , 21F
謝謝提醒~我會注意的
08/18 14:51, 21F

08/18 20:03, , 22F
fjrejox....
08/18 20:03, 22F

08/19 10:00, , 23F
SORRY~f大
08/19 10:00, 23F
文章代碼(AID): #1EIwctMS (C_and_CPP)
文章代碼(AID): #1EIwctMS (C_and_CPP)