4

My program requires several floats to be set to a default number when the program launches. As the program runs these integers will be set to their true values. These true values however can be any real number. My program will be consistently be checking these numbers to see if their value has been changed from the default.

For example lets say I have integers A,B,C. All these integers will be set to a default value at the start (lets say -1). Then as the program progresses, lets say A and B are set to 3 and 2 respectfully. Since C is still at the default value, the program can conclude than C hasn't been assigned a non-default value yet.

The problem arises when trying to find a unique default value. Since the values of the numbers can be set to anything, if the value its set to is identical to the default value, my program won't know if a float still has the default value or its true value is just identical to the default value.

I considered NULL as a default value, but NULL is equal to 0 in C++, leading to the same problem!

I could create a whole object consisting of an bool and a float as members, where the bool indicates whether the float has been assigned its own value yet or not. This however seems like an overkill. Is there a default value I can set my floats to such that the value isn't identical to any other value? (Examples include infinity or i)

I am asking for C/C++ solutions.

fdh
  • 5,256
  • 13
  • 58
  • 101
  • C doesn't intrinsically support the ability to determine whether a value has been assigned. One strategy is to create a hashmap (from variable name to assigned value), and if the variable name does not exist in the hashmap, then it was not assigned to. – Chris Betti Jun 20 '12 at 20:19
  • *infinity or i* are not integers. If you were dealing with floating point numbers, you could use Not-a-Number (nan), but there is no such value for integers. You could use boost::optional (implementation should be similar to the combination of a boolean and the integer... or else roll your own, but there is no *invalid* integer (unless there is one in your domain, i.e. in your domain -12345 is not possible...) – David Rodríguez - dribeas Jun 20 '12 at 20:21
  • You mention float at the end. In C++ you could use Nan, std::numeric_limits::NaN(), as a marker. But there is no marker value for ints. – Alex Wilson Jun 20 '12 at 20:21

8 Answers8

7

I could create a whole object consisting of an bool and a integer as members, where the bool indicates whether the number has been assigned its own value yet or not. This however seems like an overkill.

What you described is called a "nullable type" in .NET. A C++ implementation is boost::optional:

boost::optional<int> A;

if (A)
  do_something(*A);
kol
  • 27,881
  • 12
  • 83
  • 120
5

On a two's complement machine there's an integer value that is less useful than the others: INT_MIN. You can't make a valid positive value by negating it. Since it's the least useful value in the integer range, it makes a good choice for a marker value. It also has an easily recognizable hex value, 0x80000000.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

There is no bit pattern you can assign to an int that isn't an actual int. You need to keep separate flags if you really have no integer values that are out of bounds.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

If the domain of valid int values is unlimited, the only choice is a management bit indicating whether it is assigned or not.

But, are you sure MAX_INT is a desired choice?

wallyk
  • 56,922
  • 16
  • 83
  • 148
1

There is no way to guarantee that a value you assign an int to is not going to be equal to another random int. The only way to assure that what you want to happen occurs, is to create a separate bool to account for changes.

NominSim
  • 8,447
  • 3
  • 28
  • 38
1

No, you will have to create your own data type which contains the information about whether it has been assigned or not.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
0

If as you say, no integer value is off limits, then you cannot assign a default "uninitialised" value. Just use a struct with an int and a bool as you suggest in your question.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
0

I could create a whole object consisting of an bool and a integer as members, where the bool indicates whether the number has been assigned its own value yet or not. This however seems like an overkill.

My first guess would be to effectively use a flag and mark each variable. But this is not your only choice of course.

  1. You can use pointers (which can be NULL) and assign dynamically the memory. Not very convenient.
  2. You can pick a custom value which is almost never used. You can then define this value to be the default value. Ofc, some time, you will need to assign this value to your floats, but this case won't happen often and you just need to keep track of this variables. Given the occurrence of such case, a simple linked list should do.
Samy Arous
  • 6,794
  • 13
  • 20