如何在C++中解决此运行时间错误? - “围绕变量的堆栈已损坏”。
我尝试获得 的最小
两个整数
,实数
,strings
,,和整数数组
通过实现获得与功能超载的功能。
所以我写这样的代码
#include <iostream>
#include <cstring>
using namespace std;
int GetMin(int a, int b);
double GetMin(double a, double b);
char* GetMin(char* a, char* b);
int GetMin(int* arr, int size);
int main()
{
int num1, num2;
double num3, num4;
char s1[30], s2[30];
int arr[10];
cout << "Enter two integers : ";
cin >> num1 >> num2;
cout << "The minimum value is " << GetMin(num1, num2) <<"\n";
cout << "Enter two real numbers : ";
cin >> num3 >> num4;
cout << "The minimum value is " << GetMin(num3, num4) <<"\n";
cout << "Enter two strings : ";
cin >> s1 >> s2;
cout << "The minimum value is " << GetMin(s1,s2) <<"\n";
cout << "Elements of an Array : ";
cin >> arr[10];
cout << "The minimum value is " << GetMin(arr[10], 10);
}
int GetMin(int a, int b)
{
if (a > b) return b;
else return a;
}
double GetMin(double a, double b)
{
if (a > b) return b;
else return a;
}
char* GetMin(char* a, char* b)
{
if (strcmp(a, b) < 0) return a;
}
int GetMin(int* arr, int size)
{
int min = arr[0];
for (int i = 1; i<size; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
这是所需的结果。
Enter two integers : 10 20
The minimum value is 10
Enter two real numbers : 56.84 120.26
The minimum value is 56.84
Enter two strings : orange apple
The minimum value is apple
Elements of an Array : 41 67 34 25 0 54 98 21 58 62
The minimum value is 0
我收到了此错误消息。
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
I try to obtain the minimum of two integers
, real numbers
, strings
, and integer arrays
by implementing functions that obtain similar treatment results as function overloading.
So I write code like this
#include <iostream>
#include <cstring>
using namespace std;
int GetMin(int a, int b);
double GetMin(double a, double b);
char* GetMin(char* a, char* b);
int GetMin(int* arr, int size);
int main()
{
int num1, num2;
double num3, num4;
char s1[30], s2[30];
int arr[10];
cout << "Enter two integers : ";
cin >> num1 >> num2;
cout << "The minimum value is " << GetMin(num1, num2) <<"\n";
cout << "Enter two real numbers : ";
cin >> num3 >> num4;
cout << "The minimum value is " << GetMin(num3, num4) <<"\n";
cout << "Enter two strings : ";
cin >> s1 >> s2;
cout << "The minimum value is " << GetMin(s1,s2) <<"\n";
cout << "Elements of an Array : ";
cin >> arr[10];
cout << "The minimum value is " << GetMin(arr[10], 10);
}
int GetMin(int a, int b)
{
if (a > b) return b;
else return a;
}
double GetMin(double a, double b)
{
if (a > b) return b;
else return a;
}
char* GetMin(char* a, char* b)
{
if (strcmp(a, b) < 0) return a;
}
int GetMin(int* arr, int size)
{
int min = arr[0];
for (int i = 1; i<size; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
Here's the desired result.
Enter two integers : 10 20
The minimum value is 10
Enter two real numbers : 56.84 120.26
The minimum value is 56.84
Enter two strings : orange apple
The minimum value is apple
Elements of an Array : 41 67 34 25 0 54 98 21 58 62
The minimum value is 0
I got this error message.
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的行为不确定,这是由于这种尝试将索引读成阵列以外的索引而引起的:
我认为您正在尝试阅读整个数组,但这是不可能的。而且,如果是(例如,通过重载
&gt;
)将使用arr
而不是arr [10]
。您可以通过超载
&gt;&gt;
来解决此问题(尽管我怀疑这是不必要的复杂...)。或通过循环阅读。You have undefined behaviour, caused by this attempt to read into an index beyond the array:
I think you are trying to read the whole array in, but that is not possible like that. And if it were (e.g. by overloading
>>
) it would be usingarr
instead ofarr[10]
.You can solve this by overloading
>>
(though I suspect that to be unnecessarily complex...). Or by reading in with a loop.使用
cin
将值分配给整个数组是错误的。更常见的方法是在> 结构中使用或
。我建议您阅读此了解输入和输出。
另外,您是否观察到计划中的警告?
阵列ARR [10]的索引应为ARR [0] 〜ARR [9]。关于C6385,您应该阅读此。
最好不要将函数的参数用作返回值。常见方法是定义一个可存储要返回的参数的变量。
It is wrong to use
cin
to assign values to the entire array. The more common method is to use thefor
orwhile
structure. I recommend you to read this issue to understand input and output.Also, have you observed warnings in your program?
The index of the array arr[10] should be arr[0]~arr[9]. About C6385 you should read this document.
It is best not to use the parameters of the function as the return value. The common method is to define a variable to store the parameters to be returned.
您不能
将单个值输入到十个元素数组的第十一元素(导致 undfined行为)中。
您需要使用循环来一个一个元素:
在不相关的注释上,我建议您了解有关
std :: Array
和std :: String
array array andray 。他们将使您成为C ++程序员的生活变得更加简单。You can't input to an array like
Instead that inputs a single value into the eleventh element of your ten-element array (leading to undefined behavior).
You need to use a loop to input each element one by one:
On an unrelated note, I recommend you learn more about the standard containers like
std::array
andstd::string
. They will make your life as a C++ programmer much simpler.