C_and_CPP 板


LINE

開發平台(Platform): (Ex: Win10, Linux, ...) FreeBSD 11.2 CentOS 7.5.1804 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VS code 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我想要new一個int陣列用來當作pipe使用, 舉例來說假設指令是ls | cat | cat, 那我會有一個int pipenum=2,再執行 int *pfd = new int[2*pipenum],然後分別執行 pipe(pfd) pipe(pfd+2),再來使用這兩個pipe傳資料 但是有時候在new array的時候會有下面這個錯誤訊息 然後程式就被terminate了 請問是我new array的方式有什麼問題嗎? 還是跟OS有關呢? 感謝 預期的正確結果(Expected Output): new 一個int array 錯誤結果(Wrong Output): terminate called after throwing an instance of 'std::bad_array_new_length' what(): std::bad_array_new_length Abort trap (core dumped) 可能的原因我google了一下有: 1. If the array size is less than zero. 2. If the array size is greater than an implementation-defined limit. 3.If the number of elements in the initializer list exceeds the number of elements to initialize. 1是不可能的,所以我猜是2,那這樣算是OS的問題嗎? 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) vector <string> commands=SplitString(line," | "); pipenum=commands.size()-1; cout<<"here"<<endl; int *pfd = new int [2*pipenum]; cout<<"there"<<endl; line是我的input command 結果只有印出here,所以我想應該是這行的問題 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.71.119
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1542631151.A.00A.html
1F:→ ilikekotomi: 沒有完整可再現的code只能猜是pipenum有問題 11/19 20:56
2F:→ ilikekotomi: cppreference寫說有三個情況會跳這個exception 11/19 20:57
3F:→ ilikekotomi: 這行code有可能問題是數字太大或是負的 11/19 20:58
4F:→ ilikekotomi: 要不要先嘗試把pipenum在here那邊印出來 11/19 20:59
我補上比較完整的code了 pipenum一定不會有問題,因為有時候我重開再跑一次, 如果第一次new array有成功,那我的程式就過了(我不止會new一次) 但是有時候就是會卡在第一次new array就失敗 而且我被terminate的那一次指令,我只是要new一個4格int的array... 感覺是OS拒絕我new 這個array?
5F:推 Fenikso: 就印出來看看啊 說不定是split寫壞讓size()有時候傳回0 11/19 21:12
印出來是2,我的問題應該不在這裡,我如果重開程式,第一次new array有成功, 那我的程式的執行就一切正常,所以我覺得其他部分的程式是沒有問題的, 但是就是有時候會在第一次new陣列就被terminate...
6F:→ ilikekotomi: 因為不知道spilt line怎麼做的 11/19 21:17
7F:→ ilikekotomi: 要不要把line 和command都印出來看看 11/19 21:18
splitstring是用find + pushback substring去做的 我印出來一切正常,因為有時候我會new成功, 如果成功的話,我的程式就一切正常, 所以關鍵就在於我第一次去嘗試new的時候有沒有成功
8F:→ ilikekotomi: 可以的話希望提供整份可以執行的code和input data 11/19 21:21
程式碼有點長,我可以提供我怎麼切字串的部分 vector<string> SplitString(const string line, const string delimeter){ vector <string> v; size_t pos1, pos2; pos1 = 0;//從頭開始find pos2 = line.find(delimeter);//第一個delimeter的index while(string::npos != pos2){ //將第一個substring push到vector v.push_back(line.substr(pos1, pos2-pos1)); //從delimeter的下一個位址開始find pos1 = pos2 + delimeter.size(); //下一個delimeter的index pos2 = line.find(delimeter, pos1); } //將最後一個substring push到vector if(pos1 != line.length()){ v.push_back(line.substr(pos1)); } return v; } input data就是上面寫的ls | cat | cat
9F:→ ilikekotomi: 剛剛想到有沒有請朋友多跑幾次看看 也許真的是OS問題 11/19 21:40
10F:→ ilikekotomi: 就可以快點重灌電腦了 11/19 21:40
多跑幾次就是有時候可以有時候不行,可以就完美不行就爆炸這樣 環境是工作站啊XD 我沒有辦法重灌... ※ 編輯: TampaBayRays (218.161.71.119), 11/19/2018 21:44:00
11F:推 steve1012: 你覺得new 要了太多個element的size 你有沒有算過你大 11/20 01:00
12F:→ steve1012: 概new 了多少個?這不用猜測吧 11/20 01:00
13F:推 steve1012: 不確定你其他地方寫了什麼 但瞄了一下line要是空字串 11/20 01:04
14F:→ steve1012: 你能確保不會返回零嗎?若是你覺得這不會發生 你可以 11/20 01:05
15F:→ steve1012: 在new array之前throw 或assert 不是零去排除1.的狀況 11/20 01:05
16F:→ steve1012: 自己猜/印不太可靠 11/20 01:05
1.我程式被terminate的那次我new了一個4格int的陣列,而且我每次用完都有delete 2.如果line是空字串或line中沒有|都會先排除,不會執行到這裡 3.我試試看
17F:→ sarafciel: string.find要求字串全符合 你的input全部都是用" | " 11/20 17:19
18F:→ sarafciel: 做連接嗎 還是有" |"、" |"或"|"的case 11/20 17:20
這部分只處理" | "的部分
19F:推 steve1012: 不要信任自己某件事不會發生 讓程式告訴你 11/21 01:32
最後我debug的結果的確是我程式的問題XD 感謝各位的建議! ※ 編輯: TampaBayRays (218.161.71.119), 11/22/2018 08:50:47







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP