C++ 奇怪的临时数组损坏
我试图创建一个排列,当我完成我的问题时,我收到这个奇怪的错误:C++ 奇怪的临时数组损坏,c++,arrays,corrupt,C++,Arrays,Corrupt,我试图创建一个排列,当我完成我的问题时,我收到这个奇怪的错误: Stack around the variable "temp" was corrupted 变量段位于嵌套的for循环中: for(int i = 0 ; i < str_length ; i++) { for(int j = 0 ; j < str_length ; j++) { char temp[1]; temp[1] = text[i]; te
Stack around the variable "temp" was corrupted
变量段位于嵌套的for循环中:
for(int i = 0 ; i < str_length ; i++)
{
for(int j = 0 ; j < str_length ; j++)
{
char temp[1];
temp[1] = text[i];
text[i] = text[j];
text[j] = temp[1];
cout << text << endl;
}
}
for(int i=0;i 不能使用temp[0]
访问第一个元素使用temp[0]
访问第一个元素您只需要使用char-temp;
并将其作为temp=text[i];
等进行访问
您正在访问堆栈上超过temp一个字节的点,这是无效的。在这种情况下,因为您只需要一个字符,所以根本不需要数组。您只需要使用char-temp;
并将其作为temp=text[i];
等进行访问
您正在访问堆栈上超过temp一个字节的点,这是无效的。在这种情况下,由于您只需要一个字符,因此根本不需要数组。temp[1]不存在,您应该执行temp[0]。或者,类似这样:
char temp;
temp = text[i];
text[i] = text[j];
text[j] = temp;
或
temp[1]不存在,您应该执行temp[0]。或者,类似以下操作:
char temp;
temp = text[i];
text[i] = text[j];
text[j] = temp;
或
如果您想使用唯一的排列,则考虑使用<代码>(int j= i+1;< /代码>)如果您想使用唯一的排列,考虑使用<代码>(int j=i+1;< />代码>