[問題] new int array的方法

看板C_and_CPP (C/C++)作者 (光芒今年拿冠軍)時間7年前 (2018/11/19 20:39), 7年前編輯推噓4(4015)
留言19則, 4人參與, 7年前最新討論串1/1
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1542631151.A.00A.html

11/19 20:56, 7年前 , 1F
沒有完整可再現的code只能猜是pipenum有問題
11/19 20:56, 1F

11/19 20:57, 7年前 , 2F
cppreference寫說有三個情況會跳這個exception
11/19 20:57, 2F

11/19 20:58, 7年前 , 3F
這行code有可能問題是數字太大或是負的
11/19 20:58, 3F

11/19 20:59, 7年前 , 4F
要不要先嘗試把pipenum在here那邊印出來
11/19 20:59, 4F
我補上比較完整的code了 pipenum一定不會有問題,因為有時候我重開再跑一次, 如果第一次new array有成功,那我的程式就過了(我不止會new一次) 但是有時候就是會卡在第一次new array就失敗 而且我被terminate的那一次指令,我只是要new一個4格int的array... 感覺是OS拒絕我new 這個array?

11/19 21:12, 7年前 , 5F
就印出來看看啊 說不定是split寫壞讓size()有時候傳回0
11/19 21:12, 5F
印出來是2,我的問題應該不在這裡,我如果重開程式,第一次new array有成功, 那我的程式的執行就一切正常,所以我覺得其他部分的程式是沒有問題的, 但是就是有時候會在第一次new陣列就被terminate...

11/19 21:17, 7年前 , 6F
因為不知道spilt line怎麼做的
11/19 21:17, 6F

11/19 21:18, 7年前 , 7F
要不要把line 和command都印出來看看
11/19 21:18, 7F
splitstring是用find + pushback substring去做的 我印出來一切正常,因為有時候我會new成功, 如果成功的話,我的程式就一切正常, 所以關鍵就在於我第一次去嘗試new的時候有沒有成功

11/19 21:21, 7年前 , 8F
可以的話希望提供整份可以執行的code和input data
11/19 21:21, 8F
程式碼有點長,我可以提供我怎麼切字串的部分 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

11/19 21:40, 7年前 , 9F
剛剛想到有沒有請朋友多跑幾次看看 也許真的是OS問題
11/19 21:40, 9F

11/19 21:40, 7年前 , 10F
就可以快點重灌電腦了
11/19 21:40, 10F
多跑幾次就是有時候可以有時候不行,可以就完美不行就爆炸這樣 環境是工作站啊XD 我沒有辦法重灌... ※ 編輯: TampaBayRays (218.161.71.119), 11/19/2018 21:44:00

11/20 01:00, 7年前 , 11F
你覺得new 要了太多個element的size 你有沒有算過你大
11/20 01:00, 11F

11/20 01:00, 7年前 , 12F
概new 了多少個?這不用猜測吧
11/20 01:00, 12F

11/20 01:04, 7年前 , 13F
不確定你其他地方寫了什麼 但瞄了一下line要是空字串
11/20 01:04, 13F

11/20 01:05, 7年前 , 14F
你能確保不會返回零嗎?若是你覺得這不會發生 你可以
11/20 01:05, 14F

11/20 01:05, 7年前 , 15F
在new array之前throw 或assert 不是零去排除1.的狀況
11/20 01:05, 15F

11/20 01:05, 7年前 , 16F
自己猜/印不太可靠
11/20 01:05, 16F
1.我程式被terminate的那次我new了一個4格int的陣列,而且我每次用完都有delete 2.如果line是空字串或line中沒有|都會先排除,不會執行到這裡 3.我試試看

11/20 17:19, 7年前 , 17F
string.find要求字串全符合 你的input全部都是用" | "
11/20 17:19, 17F

11/20 17:20, 7年前 , 18F
做連接嗎 還是有" |"、" |"或"|"的case
11/20 17:20, 18F
這部分只處理" | "的部分

11/21 01:32, 7年前 , 19F
不要信任自己某件事不會發生 讓程式告訴你
11/21 01:32, 19F
最後我debug的結果的確是我程式的問題XD 感謝各位的建議! ※ 編輯: TampaBayRays (218.161.71.119), 11/22/2018 08:50:47
文章代碼(AID): #1Rygxl0A (C_and_CPP)
文章代碼(AID): #1Rygxl0A (C_and_CPP)