如何在C++中解决此运行时间错误? - “围绕变量的堆栈已损坏”。

发布于 2025-01-22 08:01:36 字数 1997 浏览 0 评论 0原文

我尝试获得 的最小两个整数实数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2025-01-29 08:01:36

您的行为不确定,这是由于这种尝试将索引读成阵列以外的索引而引起的:

cin >> arr[10];

我认为您正在尝试阅读整个数组,但这是不可能的。而且,如果是(例如,通过重载&gt;)将使用arr而不是arr [10]

您可以通过超载&gt;&gt;来解决此问题(尽管我怀疑这是不必要的复杂...)。或通过循环阅读。

You have undefined behaviour, caused by this attempt to read into an index beyond the array:

cin >> arr[10];

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 using arr instead of arr[10].

You can solve this by overloading >> (though I suspect that to be unnecessarily complex...). Or by reading in with a loop.

情话难免假 2025-01-29 08:01:36

使用cin将值分配给整个数组是错误的。更常见的方法是在> 结构中使用。我建议您阅读此了解输入和输出。

另外,您是否观察到计划中的警告?

警告C6201索引'10'超出有效索引范围'0'至9'
可能堆叠分配的缓冲区'arr'。

阵列ARR [10]的索引应为ARR [0] 〜ARR [9]。关于C6385,您应该阅读此

char* GetMin(char* a, char* b)
{
    if (strcmp(a, b) < 0) return a;

}

最好不要将函数的参数用作返回值。常见方法是定义一个可存储要返回的参数的变量。

It is wrong to use cin to assign values to the entire array. The more common method is to use the for or while structure. I recommend you to read this issue to understand input and output.

Also, have you observed warnings in your program?

enter image description here

Warning C6201 Index '10' is out of valid index range '0' to '9' for
possibly stack allocated buffer 'arr'.

The index of the array arr[10] should be arr[0]~arr[9]. About C6385 you should read this document.

char* GetMin(char* a, char* b)
{
    if (strcmp(a, b) < 0) return a;

}

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.

星星的轨迹 2025-01-29 08:01:36

您不能

cin >> arr[10];

将单个值输入到十个元素数组的第十一元素(导致 undfined行为)中。

您需要使用循环来一个一个元素:

for (unsigned i = 0; i < std::size(arr); ++i)
{
    std::cout << "Enter input for element " << i + 1 << ": ";
    std::cin >> arr[i];
}

在不相关的注释上,我建议您了解有关std :: Arraystd :: String array array andray 。他们将使您成为C ++程序员的生活变得更加简单。

You can't input to an array like

cin >> arr[10];

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:

for (unsigned i = 0; i < std::size(arr); ++i)
{
    std::cout << "Enter input for element " << i + 1 << ": ";
    std::cin >> arr[i];
}

On an unrelated note, I recommend you learn more about the standard containers like std::array and std::string. They will make your life as a C++ programmer much simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文