開發平台(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,所以我想應該是這行的問題