Faced with an error:

Run-Time Check Failure # 2 - Stack around the variable 'C' was corrupted.

It is displayed in the debugger and indicates the last function, saying that something is wrong with it. Please help.

Code:

#include "pch.h" #include <iostream> using namespace std; void ini(int arr[5][5]); void vivid(int arr[5][5]); void vividpod(int arr[5][5]); int main() { int arr[5][5]; ini(arr); vivid(arr); vividpod(arr); system("pause"); return 0; } void ini(int arr[5][5]) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { arr[i][j] = rand() % 10; } } } void vivid(int arr[5][5]) { for (int i = 0;i < 5;i++) { for (int j = 0;j < 5;j++) { cout << arr[i][j] << "\t"; } cout << endl; } } void vividpod(int arr[5][5]) { int pod; int C[4]; cout << "\n"; for (int i = 0; i < 4; i++) { pod = arr[4][i]; C[4] = pod; cout << C[4]; cout << endl; } } 

    1 answer 1

    Well, you go beyond the bounds of the array - that’s where the stack breaks:

     int C[4]; ... C[4] = pod; 
    • How can I fix it? - Roman Skrypnik
    • Are you serious? Well, I'll write to you - C[0] = pod; . This error will not happen, but will your program consider what you need? - Harry
    • Many thanks - Roman Skrypnik