0

I'm trying to permute vector using recursion, but this produce segmentation fault(core dump),what is wrong with the code? the idea is using backtracking, delete and choose string to add to vector, recur, and backtrack

#include <bits/stdc++.h>
#include <vector>
using namespace std;

// Function to print permutations of string
// This function takes three parameters:
// 1. String
// 2. vector to store string permutation
// 3. dummy vector to store each permutation
vector<vector<int> > permute(vector<int> nums, vector<vector<int> >& k, vector<int> li)
{
    if (nums.empty()) {
        k.push_back(li);
    }
    else {
        for (int i = 0; i < nums.size(); i++) {
            int chos = nums[i];
            li.push_back(chos);
            nums.erase(nums.begin() + i);
            permute(nums, k, li);
            nums.insert(nums.begin() + i, chos);
            li.erase(li.end()-1);
        }
    }
    return k;
}

// Driver Code
int main()
{
    vector<int> permut = { 1, 2, 3 };
    vector<vector<int> > k;
    vector<int> li;

    k = permute(permut, k, li);
    return 0;
}
devss
  • 145
  • 7
  • 3
    `#include ` -- Use the proper header files, not this one. – PaulMcKenzie Oct 27 '19 at 03:08
  • What is the stack trace in the core dump? – Thomas Sablik Oct 27 '19 at 03:11
  • When you [debugged this program](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/), on what line did you see the segmentation fault occur? – JaMiT Oct 27 '19 at 03:12
  • 2
    `li.erase(li.end());` -- What are you trying to do here? – PaulMcKenzie Oct 27 '19 at 03:14
  • Please format your code in the future. If your IDE doesn't support it you can use http://format.krzaq.cc/ – Thomas Sablik Oct 27 '19 at 03:15
  • *i'm trying to permutate vector using recursion* -- Also, why do they assign these recursion homeworks on things that no one would do recursively? – PaulMcKenzie Oct 27 '19 at 03:22
  • @ThomasSablik it was after the first push back to k , using gdb i got this error " Program received signal SIGSEGV, Segmentation fault. __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:484 484 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory." – devss Oct 27 '19 at 03:26
  • @devss -- Your problems start before that -- see my comment. You are identifying where your program finally falls apart, but the errors happen before that. If you don't fix that illegal access to the `end()` iterator, the behavior of the program after that illegal access doesn't matter. – PaulMcKenzie Oct 27 '19 at 03:27
  • @PaulMcKenzie im trying to delete the last character that is added to the vector, because it is backtracking, but maybe i wrote wrong syntax. this problem is from leetcode, not homework – devss Oct 27 '19 at 03:29
  • @devss -- There is no character at `end()`. So what are you deleting? The `end()` iterator points to one position **after** the last character. – PaulMcKenzie Oct 27 '19 at 03:30
  • @PaulMcKenzie do you mean the end() cause the error? but in the recursion call, the program already crashed, though it not yet reach the li.end() call – devss Oct 27 '19 at 03:31
  • @PaulMcKenzie ok, i should change it to li.end()-1 – devss Oct 27 '19 at 03:32
  • You should *never* `#include `. It is not proper C++. It ruins portability and fosters terrible habits. Questions using it will usually be downvoted on Stack Overflow. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Oct 27 '19 at 03:40
  • Incidentally, please try to avoid `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Oct 27 '19 at 03:40

1 Answers1

0

what is wrong with the code?

One thing certainly wrong with the code is this line:

li.erase(li.end());

You are calling erase on the end() iterator, which is undefined behavior.

If you want to delete the last character, the proper way to do that would be:

if ( !li.empty())
    li.erase(std::prev(li.end(), 1));
PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
  • Why `prev(li.end(), 1)`? `li.end() - 1` is fine, or at least `prev(li.end())`. – L. F. Oct 27 '19 at 03:39
  • The answer works for any container, not just vector. – PaulMcKenzie Oct 27 '19 at 03:41
  • Then why not use a template and SFINAE to actually support all containers? The way the resulted sequences are output can also be sorted based on a custom criteria by using an output iterator. Anyway, AFAIK, `prev(li.end())` is always equivalent to `prev(li.end(), 1)`. – L. F. Oct 27 '19 at 03:44
  • thankyou but when i debug the code, it not correctly push back the string permutation vector li to the k vector (vector to store all permutation string), it only push back the first string permutation. how can i preserve it? – devss Oct 27 '19 at 03:47
  • @devss -- Your question concerned the segmentation fault, which I believe was answered. Your program giving the wrong output is a different story and you need to debug your code. – PaulMcKenzie Oct 27 '19 at 03:49
  • @PaulMcKenzie ok thankyou i can fix it by change the parameter to vector>&k ,but how do you know the error is because the li.end()? i mean i know it is wrong, but does the error message segmentation fault is about that? – devss Oct 27 '19 at 05:17