2

I run the program and see that the values aren't always 0 and 1. But this is a bool data type right?

#include<iostream>
#include<math.h>

using namespace std;

int main()
{
    bool arr[5][5];
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    Crom only knows what's in that uninitialized block of memory. – user4581301 Sep 10 '21 at 18:13
  • Welcome to [Undefined Behavior](https://stackoverflow.com/questions/54120862/). – Drew Dormann Sep 10 '21 at 18:19
  • @Samiul Islam To make the code valid write bool arr[5][5] = {}; – Vlad from Moscow Sep 10 '21 at 18:20
  • The compiler must use *at least* one bit for every `bool` element. The smallest addressable unit is a `uint8_t` (or `char`). Thus, the compiler can use at least 25 `uint8_t` for the array. If you are interested in packing bits (or `bool`), consider using `std::bitset`. If the processor word size is greater than 8 bits, the compiler may consider using an `unsigned int` instead of a `bool`; this has the advantage of faster performance (one fetch, no shifts or truncations). – Thomas Matthews Sep 10 '21 at 18:24
  • Any non-zero value is "true" in a bool, it isn't restricted to 0 and 1. – L. Scott Johnson Sep 10 '21 at 18:39
  • What output do you see? – alfC Sep 11 '21 at 06:09
  • `bool` takes the size of an `char` integer (a byte) in your platform. It is interesting that `ostream` prints the value of the integer instead of 0 and 1. If you set up `ostream` in `boolalpha`-mode you will see `true` and `false` only. https://godbolt.org/z/94jh4jdsv. I think there is nothing wrong with leaving stack variables uninitialized. The misconception, maybe, is that `cout< – alfC Sep 12 '21 at 03:47

2 Answers2

5

You are not initializing the array, the printed values are garbage stored in the memory where your declared array now lives, since you don't initialize it we can't know what's there.

Note that the size of a bool is implementation defined so we can't really guess a range of values for the aforementioned undefined behavior when printing unitialized members of the array.

The usual behavior we see is the compiler using the most efficient available type which usually means an unsigned 1 byte/8 bit wide type. In this situation you could expect the uninitialized values to be in the range of 0 to 255.

To initialize all the values in the array you could do this:

bool arr[5][5]{};

With this value initialization the array would be zero initialized and your program would output:

0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

You can use boolalpha to print actually true or false:

Example:

https://godbolt.org/z/xvh67jWWK

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

You are experiencing undefined behavior due to reading uninitialized values.

The presence of UB makes program behavior meaningless. The reason you see random garbage which is not only 0 or 1 is because bool occupies one byte, not one bit. The compiler does not compensate for this because in a valid program such situation is impossible, so there is no need to do the extra work.

If you initialize the bool array properly, the issue will be resolved:

bool arr[5][5] = {};

See this related question in which, due to UB a bool is both true and false at the same time.

rustyx
  • 80,671
  • 25
  • 200
  • 267