segmentation fault 問題

#include<iostream>

#include<vector>

using namespace std;

void rrrrr(int z1, int z2, vector<int> *ss1, vector<int> *ss2, vector<int> *ss3);

int main()

{

int a,b,c,d;

cout << "Enter the vector 1 size : ";

cin >> a;

cout << "Entet vector 1 of size " << a << " : ";

vector<int> ss1;

ss1.clear();

for(int i=0;i<a;i++)

{

cin >> b;

ss1.push_back(b);

}

cout << "Enter the vector 2 size : ";

cin >> c;

cout << "Enter vector 2 of size " << c << " : ";

vector<int> ss2;

ss2.clear();

for(int j=0;j<c;j++)

{

cin >> d;

ss2.push_back(d);

}

cout << "The vector merged of size " << a+c << " is : ";

int e =ss1.size()+ss2.size();

int f= ss1.size();

int g= ss2.size();

vector<int> ss3(e);

ss3.clear();

rrrrr(f, g, &ss1, &ss2, &ss3);

for(int o=0; o<e; o++)

{

cout << ss3[o] << " ";

}

cout << endl;

return 0;

}

void rrrrr(int z1, int z2, vector<int> *ss1, vector<int> *ss2, vector<int> *ss3)

{

int e = z1+z2;

for(int k=0;k<z1;k++)

{

ss3[k]=ss1[k];

}

int r= z1;

for(int l=0;l<z2;l++)

{

ss3[r+l]=ss2[l];

}

}

為什麼會segmentation fault

Update:

To:prisoner26535

刪除後compile還是segmentation fault...

2 Answers

Rating
  • 7 years ago
    Favorite Answer

    > 為什麼會segmentation fault

    // 有人會大膽到 不看看容器裡是神東東,就去用它

    // 那你的 程式前途 一定是光明無比

    // 記住我這句話 30年後再來感謝我吧!

    ss3.clear(); // 移除這行在跑看看 就知道為神了

    2014-03-04 21:31:51 補充:

    // 還有以下這些奇奇怪怪的東東 也都移除好嗎?

    int e = z1+z2;

    int r= z1;

    2014-03-05 00:41:37 補充:

    void rrrrr(int z1, int z2, vector &ss1, vector &ss2, vector &ss3) {

    int i,k;

    for(i=0;z1>i;++i) ss3[i]=ss1[i];

    for(k=0;z2>k;++k) ss3[i+k]=ss2[k];

    }

    2014-03-05 00:44:35 補充:

    // 你對指標的誤會太深了 我也不知道要怎解釋給你聽 只好給漁 不能教你釣魚了!

    2014-03-05 02:09:43 補充:

    是應該要像2等先生釀寫才對.

  • 7 years ago

    這麼簡單ㄉ東西也被你搞成這樣.

    1. 呼叫函式傳vector指標, 又使用operator[ ],

    vector不是array, 不是這樣玩ㄉ.

    2. 即然你知道vector有提供size(), 幹嘛函式還要多兩個參數傳.

    3. 要merge就merge漂亮一點ㄚ

    http://ideone.com/k4yV0R

Still have questions? Get your answers by asking now.