Home > Mobile >  C crashing when reading strings and storing on a vector
C crashing when reading strings and storing on a vector

Time:12-31

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

typedef vector<string> VS;

void back(VS &paraules, VS &sol, int n, int i) {
    if(i == n) {
        cout << "{" << sol[0];
        for(int j = 1; j < n; j  ) {
            cout << "," << sol[i];
        }
        cout << "}" << endl;
    }
    else {
        for(int j = 0; j < n; j  ) {
            sol[i] = paraules[j];
            back(paraules, sol, n, i 1);
        }
    }

}

int main() {
    int n;
    cin >> n;
    VS sol(n);
    VS paraules(n);
    for(int i = 0; i < n; i  ) {
        cin >> paraules[i];
    }
    cout << "This won t print";
    back(paraules, sol, n, 0); 
}

Pasted the whole code now. A backtracking that takes n words, and just prints all the permutations of the words.

I initially thought it was a problem with the reading since the this wont print wasn't printing. After some testing, I've discovered that commenting the function call on the last line makes the error disappear, and the code doesn't crash anymore.

So it's maybe the function? This still doesn't explain why it's not printing, since the call happens after the cout.

As an example input might be:

2 hi bye

CodePudding user response:

The problem is that for the case when i == n is true, then you've the statement:

cout << "," << sol[i]; //this leads to undefined behavior

In the above shown statement, the size of the vector sol is n. But note since i is equal to n in this case, you are trying to going out of bounds by writing sol[i].

This is because, the indexing starts from 0 and 1.

For example, say the the vector sol size if 4. That is n = 4.

Now what you're essentially doing is :

cout << "," << sol[4];

But you can only use(safely) elements upto index 3, i.e., accessing sol[0], sol[1], sol[2], sol[3] is safe. On the other hand, using sol[4] leads to undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

For example here the program doesn't seem to crash but here it does crashes.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

CodePudding user response:

I don't believe there is an issue with the code itself. You are getting segment faults, but it would be from something else.

However, I am assuming your code looks like this:

#include <vector>
#include <iostream>

using namespace std;
int main() {
        int n;
        cin >> n;
        vector<string> v(n);
        for(int i = 0; i < n; i  ) {
            cin >> v[i];
        }
}

Because your code is missing a ending parenthesis, and a #include <iostream>. Would you mind copy and pasting your entire code, or linking a repository?

  • Related