開發平台(Platform): (Ex: Win10, Linux, ...)
OSX El Capitan
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
Homebrew GCC 7.2.0
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
C++ 預設 STL
問題(Question):
我想要把資料傳入function後,他再依據傳入的值,去執行該做的事
結束後把state改掉,這樣在接下來的時候就不會走到同樣的state
餵入的資料(Input):
無
預期的正確結果(Expected Output):
0
9
8
7
6
5
4
3
2
1
錯誤結果(Wrong Output):
0
9
0
7
6
terminated by signal SIGSEGV (Address boundary error)
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
#include <iostream>
#include <vector>
using namespace std;
vector<int> arr;
int obov(){
arr.push_back(0);
return arr.size()-1;
}
void myfunc(int& id){
if(arr.size()==10) return;
if(!id) id = obov();
myfunc(arr[id]);
cout << arr[id] << '\n';
}
int main(){
int a = obov();
myfunc(a); cout << a << '\n';
return 0;
}
( https://pastebin.com/upZsetu0 )
補充說明(Supplement):
似乎改寫成
int a = arr[id];
myfunc(a); arr[id] = a;
就不會有問題了,想請各位大大幫我指出問題點QQ