Home > Blockchain >  c : Why does my boolean store a value greater than 1?
c : Why does my boolean store a value greater than 1?

Time:02-14

I'm learning C and am right now starting with the concept of classes. And I learned that a new created object (without constructor) will have randomized attributes. So I wanted to test that and wrote a short code:

#include <iostream>
#include <vector>

class Person;
class Person {
public:
  bool const isHungry() {
    return hungry;
  }
  bool const isOld() {
    return age;
  }
private:
  bool hungry;
  bool age;
};

int main() {
  Person person1,person2,person3;
  std::vector<Person> persons {person1,person2,person3};
  for (auto i : persons) {
    std::cout << "Hungrig: " << i.isHungry() << ";\t Alt: " << i.isOld();
    std::cout << std::endl;
  }
}

And the result were:

Hungrig: 96; Alt: 147

Hungrig: 222; Alt: 46

Hungrig: 252; Alt: 127

So yes, it is randomized, but a now I got a new question: How can that be?? It's a boolean, it should be 0 or 1!

Edit: I think the trouble I have with it is that I thought the boolean converts integers greater than 1 to 1. Like this:

  bool test;
  test= 32;
  std::cout << test << std::endl;

Result is always 1. So it puzzles me why it doesn't do it with the boolean in my class.

CodePudding user response:

And I learned that a new created object (without constructor) will have randomized attributes.

This is wrong. The uninitialized members will have indeterminate values until they are assigned to. Trying to read an indeterminate value causes the program to have undefined behavior, which means that there will be no guarantee on how the program will behave. This is different from simply producing an unspecified value. It allows the program to behave in any way, whether or not it fits e.g. the constraints of types that you expect from a valid program.

Practically speaking, it often happens that the undefined behavior of reading uninitialized variables manifests simply in producing unspecified values, but these values are not randomized either. They are deterministic and a result of the specific way in which the compiler compiled the invalid program.


You are correct that a bool variable can have only values 0 or 1, but the memory that the variable occupies may have more states than that and so it is possible that multiple object representations represent the same value. For example a bool variable may occupy a whole byte of memory and one possible interpretation would be that all states of this memory are valid object representations with one specific bit identifying the value of the bool. But it may also be that each value has only one valid object representation, in which case it is likely that the code reading and printing the bool will not bother extracting such a bit and print non-sense if the memory is in an invalid state for the type, such as is likely to happen when you don't initialize the memory.

CodePudding user response:

if you didn't initialize variables, that occur random value(uninitialized).

global, static var is initialized by compiler if you didn't. Assign false to variable, or you can consider Constructor()

  • Related