what is run-time check failure in visual c++? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

what is run-time check failure in visual c++?

#include "stdafx.h" #include <iostream> using namespace std; int main() { char num[1]; for (int i = 0; i <= 10; i++) { cin >> num[i]; } for (int i = 0; i <= 10; i++) { cout << num[i]; } system("pause"); return 0; } in the above code I initialized the 'char num' with its length=1. But when I run this code because of for loop it took 11 input and also printed all the 11 inputs, why? why didn't it took only one input? why did't it show compilation error? finally i got and error after the program is been executed - " Run-Time Check Failure #2 - Stack around the variable 'num' was corrupted." what is this run-time check failure?

22nd Sep 2018, 4:54 AM
Suraj Jha
Suraj Jha - avatar
1 Resposta
+ 1
You allocated a one character array but in both your loops you are accessing elements 1 through 10 which are not part of your array. Some old compilers will compile and run (the older borland compiler for example) but at some point you get a corrupted stack. Change char num[1] to char num[11] and run it again (11 because 0 thru 10 is 11 entries. If you are very curious about this, read up on the stack with reference to how it is used to store temporary variables and program return addresses.
22nd Sep 2018, 6:23 AM
Nestor