作者TampaBayRays (光芒今年拿冠軍)
看板C_and_CPP
標題[問題] new int array的方法
時間Mon Nov 19 20:39:07 2018
開發平台(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