[問題] new int array的方法
開發平台(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
11/19 20:56, 1F
→
11/19 20:57,
7年前
, 2F
11/19 20:57, 2F
→
11/19 20:58,
7年前
, 3F
11/19 20:58, 3F
→
11/19 20:59,
7年前
, 4F
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
11/19 21:12, 5F
印出來是2,我的問題應該不在這裡,我如果重開程式,第一次new array有成功,
那我的程式的執行就一切正常,所以我覺得其他部分的程式是沒有問題的,
但是就是有時候會在第一次new陣列就被terminate...
→
11/19 21:17,
7年前
, 6F
11/19 21:17, 6F
→
11/19 21:18,
7年前
, 7F
11/19 21:18, 7F
splitstring是用find + pushback substring去做的
我印出來一切正常,因為有時候我會new成功,
如果成功的話,我的程式就一切正常,
所以關鍵就在於我第一次去嘗試new的時候有沒有成功
→
11/19 21:21,
7年前
, 8F
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
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
11/20 01:00, 11F
→
11/20 01:00,
7年前
, 12F
11/20 01:00, 12F
推
11/20 01:04,
7年前
, 13F
11/20 01:04, 13F
→
11/20 01:05,
7年前
, 14F
11/20 01:05, 14F
→
11/20 01:05,
7年前
, 15F
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
11/20 17:19, 17F
→
11/20 17:20,
7年前
, 18F
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
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章