강희주
강희주

Reputation: 21

How can I solve this run time error in C++? - "Stack around variable 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.

Upvotes: 2

Views: 137

Answers (3)

Yujian Yao - MSFT
Yujian Yao - MSFT

Reputation: 954

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.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

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.

Upvotes: 0

Yunnosch
Yunnosch

Reputation: 26703

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.

Upvotes: 2

Related Questions