内存问题,不知道为什么。什么(): std::bad_alloc
我试图编写一个程序,按字典顺序输出数字字符串的下一个排列,但出现内存错误:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
我以前从未遇到过这个问题,有什么想法吗?这是完整的程序:
#include<iostream>
#include<vector>
using namespace std;
vector<char> Next_Permutation(vector<char> InList);
void Reverse_Tail(vector<char>& List,int k);
vector<char> Swap(vector<char> InputList,int k, int l);
vector<char> GetInput();
int Find_K(vector<char> List);
int Find_l(vector<char> List,int k);
int Factorial(int n);
int main(){
vector<char> Input = GetInput();//Getting initial input use 1234 for test
int limit = Factorial(Input.size()); // finds how many permutations will be made
vector<char>* AllPerms = new vector<char>[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<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 < limit;j++){
for(int i=0;i<AllPerms[j].size();i++){
cout << AllPerms[j][i] << " " ;
}
cout << endl;
}
//cout << endl << "K = " << K << endl<< "l = " << l << endl<< endl;
cout << endl<< endl;
return 0;
}
int Factorial(int n){
if(n==0)
return 1;
else
return n*Factorial(n-1);
}
vector<char> Next_Permutation(vector<char> InList){
int K = Find_K(InList);
int l = Find_l(InList,K);
vector<char> Output = Swap(InList,K,l);
Reverse_Tail(Output,K);
}
void Reverse_Tail(vector<char>& List,int k){
int i = k+1;
int lim = (List.size() - i)/2;
int len = List.size()-1;
while(i < (List.size() - lim -1)){
List = Swap(List,i,len);
len--;
i++;
}
}
vector<char> Swap(vector<char> InputList,int k, int l){
vector<char> OutList = InputList;
int Temp = OutList[l];
OutList[l] = InputList[k];
OutList[k] = Temp;
return OutList;
}
int Find_l(vector<char> List,int k){
int l=List.size()-1;
while(List[l] < List[k]){
l--;
}
return l;
}
int Find_K(vector<char> List){
int k = List.size()-2;
while(List[k] > List[k+1]){
k--;
}
if(k == List.size()-1){
return -1;
}
return k;
}
vector<char> GetInput(){
vector<char> InputString;
cout << "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;
}
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:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
I'v never had this problem before, any ideas? here is the full program:
#include<iostream>
#include<vector>
using namespace std;
vector<char> Next_Permutation(vector<char> InList);
void Reverse_Tail(vector<char>& List,int k);
vector<char> Swap(vector<char> InputList,int k, int l);
vector<char> GetInput();
int Find_K(vector<char> List);
int Find_l(vector<char> List,int k);
int Factorial(int n);
int main(){
vector<char> Input = GetInput();//Getting initial input use 1234 for test
int limit = Factorial(Input.size()); // finds how many permutations will be made
vector<char>* AllPerms = new vector<char>[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<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 < limit;j++){
for(int i=0;i<AllPerms[j].size();i++){
cout << AllPerms[j][i] << " " ;
}
cout << endl;
}
//cout << endl << "K = " << K << endl<< "l = " << l << endl<< endl;
cout << endl<< endl;
return 0;
}
int Factorial(int n){
if(n==0)
return 1;
else
return n*Factorial(n-1);
}
vector<char> Next_Permutation(vector<char> InList){
int K = Find_K(InList);
int l = Find_l(InList,K);
vector<char> Output = Swap(InList,K,l);
Reverse_Tail(Output,K);
}
void Reverse_Tail(vector<char>& List,int k){
int i = k+1;
int lim = (List.size() - i)/2;
int len = List.size()-1;
while(i < (List.size() - lim -1)){
List = Swap(List,i,len);
len--;
i++;
}
}
vector<char> Swap(vector<char> InputList,int k, int l){
vector<char> OutList = InputList;
int Temp = OutList[l];
OutList[l] = InputList[k];
OutList[k] = Temp;
return OutList;
}
int Find_l(vector<char> List,int k){
int l=List.size()-1;
while(List[l] < List[k]){
l--;
}
return l;
}
int Find_K(vector<char> List){
int k = List.size()-2;
while(List[k] > List[k+1]){
k--;
}
if(k == List.size()-1){
return -1;
}
return k;
}
vector<char> GetInput(){
vector<char> InputString;
cout << "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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会从
Next_Permutation
返回值。未能从除main
之外的任何函数返回值都会调用未定义的行为,其中编译器和程序可以自由地执行它们喜欢的任何操作。我的 Solaris 编译器拒绝接受该程序,而我的 Linux 编译器编译该程序,但随后程序崩溃,因为free
在某个时刻检测到无效指针。两者都是处理未定义事物的有效方法。当你有未定义的行为时,花太多精力去弄清楚你的程序行为异常的确切原因通常是不明智的,因为你所看到的任何东西都不一定能被其他人重现,甚至可能无法被你重现< /em>。确保您有一个有效的程序,然后开始调试。尽管您可能会合理地耗尽内存,但更有可能的是,在尝试复制一开始就不是有效对象的内容时引发异常。
您的编译器可能提供一些诊断信息,但您可能必须在运行它时请求它们。如果您使用的是 g++,请确保包含
-Wall
选项,该选项将启用“所有”警告。 (有一些不起眼的功能未启用,但您通常不需要担心它们。)You don't return a value from
Next_Permutation
. Failure to return a value from any function butmain
invokes undefined behavior, where the compiler and the program are free to do anything they like. My Solaris compiler refuses to accept the program, whereas my Linux compiler compiles the program, but then the program crashes becausefree
detected an invalid pointer at some point. Both are valid ways to treat something that's undefined.When you have undefined behavior, it's generally not wise to put much effort into figuring out exactly why your program is behaving strangely because whatever you're seeing isn't necessarily reproducible by anyone else, and possibly not even reproducible by you. Make sure you have a valid program, and then start debugging. Although it's possible that you're legitimately running out of memory, it's more likely that the exception is being thrown while trying to copy something that's not even a valid object in the first place.
Your compiler probably offers some diagnostics, but you might have to request them when you run it. If you're using g++, make sure to include the
-Wall
option, which enables "all" warnings. (There are a few obscure ones that aren't enabled, but you generally don't need to worry about them.)