mardi 6 septembre 2016

C/C++ the result of the uninitialized array

It might be a boring question! thanks!

Here's the code:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
        int a[5] = {0};
        int b[5];
        cout << a << endl;
        cout << b << endl;
        for (int i = 0; i < 5; i++)
        {
                cout << a[i] << " ";
        }
        cout << endl;
        for (int i = 0; i < 5; i++)
        {
                cout << b[i] << " ";
        }
        cout << endl;
        return 0;
}

in Ubuntu: g++ a.cpp

enter image description here

In windows with DEV C++ ,MinGW GCC 4.7.2: enter image description here

So the question is focused on the array b:

I know I haven't initialized the array b.

Array b is full of garbage values, but why there is always having '0' with the fixed position like "X 0 X 0 X"??

What happens inside?? Just a protection mechanism?

Aucun commentaire:

Enregistrer un commentaire