Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory issue, Not sure why. what(): std::bad_alloc
    primarykey
    data
    text
    <p>Im trying to write a program that outputs the next permutation of a numerical string in lexicographic order but i am getting a memory error:</p> <pre><code>terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted </code></pre> <p>I'v never had this problem before, any ideas? here is the full program:</p> <pre><code>#include&lt;iostream&gt; #include&lt;vector&gt; using namespace std; vector&lt;char&gt; Next_Permutation(vector&lt;char&gt; InList); void Reverse_Tail(vector&lt;char&gt;&amp; List,int k); vector&lt;char&gt; Swap(vector&lt;char&gt; InputList,int k, int l); vector&lt;char&gt; GetInput(); int Find_K(vector&lt;char&gt; List); int Find_l(vector&lt;char&gt; List,int k); int Factorial(int n); int main(){ vector&lt;char&gt; Input = GetInput();//Getting initial input use 1234 for test int limit = Factorial(Input.size()); // finds how many permutations will be made vector&lt;char&gt;* AllPerms = new vector&lt;char&gt;[limit]; //AllPerms is the collection of all permutations(it is an array of vectors where each vector is a permutation) AllPerms[0] = Input; //setting intial permutation; for(int i=1;i&lt;2;i++){ // here is where i believe the program crashes. I've tried test = Next_Permutation(AllPerms[i-1]) then // doing AllPerms[i] = Test and the program runs through the first line fine but crashed on AllPerms[i] = Test? AllPerms[i] = (Next_Permutation(AllPerms[i-1])); } for(int j=0; j &lt; limit;j++){ for(int i=0;i&lt;AllPerms[j].size();i++){ cout &lt;&lt; AllPerms[j][i] &lt;&lt; " " ; } cout &lt;&lt; endl; } //cout &lt;&lt; endl &lt;&lt; "K = " &lt;&lt; K &lt;&lt; endl&lt;&lt; "l = " &lt;&lt; l &lt;&lt; endl&lt;&lt; endl; cout &lt;&lt; endl&lt;&lt; endl; return 0; } int Factorial(int n){ if(n==0) return 1; else return n*Factorial(n-1); } vector&lt;char&gt; Next_Permutation(vector&lt;char&gt; InList){ int K = Find_K(InList); int l = Find_l(InList,K); vector&lt;char&gt; Output = Swap(InList,K,l); Reverse_Tail(Output,K); } void Reverse_Tail(vector&lt;char&gt;&amp; List,int k){ int i = k+1; int lim = (List.size() - i)/2; int len = List.size()-1; while(i &lt; (List.size() - lim -1)){ List = Swap(List,i,len); len--; i++; } } vector&lt;char&gt; Swap(vector&lt;char&gt; InputList,int k, int l){ vector&lt;char&gt; OutList = InputList; int Temp = OutList[l]; OutList[l] = InputList[k]; OutList[k] = Temp; return OutList; } int Find_l(vector&lt;char&gt; List,int k){ int l=List.size()-1; while(List[l] &lt; List[k]){ l--; } return l; } int Find_K(vector&lt;char&gt; List){ int k = List.size()-2; while(List[k] &gt; List[k+1]){ k--; } if(k == List.size()-1){ return -1; } return k; } vector&lt;char&gt; GetInput(){ vector&lt;char&gt; InputString; cout &lt;&lt; "Please input the string of symbols you would like to gain all permutations of: "; char temp = 0 ; while( temp != '\n' ){ cin.get(temp); InputString.push_back(temp); } InputString.pop_back(); return InputString; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload