C/C++ the result of the uninitialized array

后端 未结 2 1068
离开以前
离开以前 2020-12-22 14:15

It might be a boring question! thanks!

Here\'s the code:

#include 
#include 
using namespace std;

int main()
{
               


        
相关标签:
2条回答
  • 2020-12-22 14:48

    That is undefined behavior. There is no guarantee, if these zeros are there, that just accidentally is true.

    The explanation is, that for some random reason at these places in memory a 0 was stored before it was reused for your purpose here. Since you allocate your arrays on the stack, these zeroes are probably from a prior function call and might be some padding. The compiler will do that as he pleases.

    0 讨论(0)
  • 2020-12-22 14:51

    The behaviour on reading uninitialised elements of an array is undefined. The compiler is allowed to do anything.

    (All the elements of a can be read due to the brace initialisation, although in C++ you can write int a[5] = {};).

    0 讨论(0)
提交回复
热议问题