0
#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

Wizard
  • 63
  • 4
  • [Cannot duplicate](https://ideone.com/ImkJ9u). – PaulMcKenzie Dec 30 '21 at 20:24
  • 1
    Maybe it does reach `func`, enter `func` and crash somewhere inside `func` before the program can emit whatever output you're expecting or perhaps after the output is emitted but before the terminal draws it on the screen.. – user4581301 Dec 30 '21 at 20:41
  • 2
    The true beauty of the [mre] (MRE) is it's hard to make a good MRE without isolating the bug and reducing the noise around it to the point that the problem becomes obvious. A found bug is usually a dead bug, eliminating the need for the question. Make the MRE early and you're often done early. When it's not you've either got a misunderstanding, we tend to resolve those very quickly, or the problem is a tough one that requires a domain expert. – user4581301 Dec 30 '21 at 20:50
  • `this still doesn t explain why it s not printing, since the call happens after the cout` What you are passing to `cout` is buffered, so if the application crashes before the buffer is flushed nothing is displayed. Console output a for sure helpful for debugging, but you should always combine it with a debugger so see which parts of the code actually execute. – t.niese Dec 30 '21 at 20:56
  • 1
    For the input "2 hi bye", this: `for(int j = 1; j < n; j++) { cout << "," << sol[i];` -- goes out-of-bounds. Change `[]` to `at()`, and you will see the issue. `for(int j = 1; j < n; j++) {cout << "," << sol.at(i);`. When you use `at()`, a `std::out_of_range` exception will be thrown if you go out-of-bounds of the vector. – PaulMcKenzie Dec 30 '21 at 21:14

3 Answers3

0

The problem is that for the case when i == n is true, 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 going out of bounds by writing sol[i] and this will result in undefined behavior.

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

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

Now what you're essentially doing is :

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

But you can only safely use elements upto index 3, i.e., using 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.

Jason
  • 36,170
  • 5
  • 26
  • 60
0

It appears that you are going out of bounds in your back function for loop due to the if statement, try using n-1 instead

    if(i == n-1) 
ynota
  • 36
  • 4
-1

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?

  • 1
    i added the whole code now, it wasn t a parethesis or the include problem, that was just a mistake when i pasted – Wizard Dec 30 '21 at 20:53