Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I deal with segmentation fault, although I used vector and then use that vector as a parameter?

+2 votes
asked Jan 25, 2022 by Abdullah-Mert (140 points)
#include <iostream>
#include <vector>
using namespace std;

double add(vector<double>& sums) {
    double result;
    for(int i = 1; i <= sums.size(); i++) {
        result += sums[i-1];
    }
    return result;
}

int main(){
.
.
.
vector<double> sums;
.
case '+':
        cout << "Type in all the numbers you want to add or substract." << endl;
        cout << "Type 0 to stop" << endl;
        do {
            cin >> n;
            sums.insert(sums.begin() + i, n);
            i++;
        } while (n != 0);
        cout << add(sums);
        break;
}

I have thus code to take in an unknown amount of numbers from the user and add them. When I run the code without warnings, it works as expected but when I try debugging it I have this segmentation fault. Not only I don't understand why I have segmentation fault even though it is a vector, I also don't know how to fix it. I think it has something to do with pointers, and to be honest I added '&' in function parameter only because I thought it would be more efficient.

I didn't want to share the whole code because it is 160 lines and the other parts are not relevant, but I can share it if necessary.

2 Answers

0 votes
answered Feb 22, 2022 by Peter Minarik (86,040 points)
Please, share your whole code as it is not clear where the segmentation fault occurs.

The reason for segmentation fault is memory violation: trying to access freed memory, double delete, or trying to use uninitialized pointers.

You can share your whole project via the "Share" button (3 buttons to the right of the "Run" button).
0 votes
answered Feb 22, 2022 by shaan tyagi (140 points)
I hope this code may help you some what !

I make a little change to this code, so if you want to change it your real code.

#include <iostream>
#include <vector>
using namespace std;

double add(vector<double>& sums) {
    double result;
    for(int i = 0; i <sums.size(); i++) {
        result += sums[i];
    }
    return result;
}

int main(){
vector<double> sums;
        cout<<"Enter the total number you want for the sum."<<endl;
        int n;
        cin>>n;
        cout << "Type in all the numbers you want to add or substract." << endl;
        for(int i=0;i<n;i++){
            int value;
            cin>>value;
            sums.push_back(value);
        }
        cout << add(sums);
}
commented Feb 24, 2022 by Peter Minarik (86,040 points)
It's nice to give suggestions to others on how to do things better by sharing a modified code. It would make your comment even better if you'd explain *what* you modified and *why*.

For instance explaining the difference between std::vector::insert() and std::vector::push_back().
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...