The default value for an integer?

My program requires several floats that will be set to the default number when the program starts. As the program starts, these integers will be set to their true values. However, these true values ​​can be any real number. My program will constantly check these numbers to see if their default value has been changed.

For example, let's say I have integers A, B, C. All of these integers will be set to the default value at the beginning (say -1). Then, as the program progresses, let's say that A and B are set to 3 and 2 with respect. Since C is still located by default, the program may conclude that C has not yet been assigned a default value.

The problem occurs when trying to find a unique default value. Since the values ​​of numbers can be set to anything, if the value set in its value is equal to the default value, my program will not know if the default value has a default value or its true value just matches the default value.

I considered NULL to be the default, but NULL is 0 in C ++, which leads to the same problem!

I could create an entire object consisting of bool and float as members, where bool indicates whether the float was once again assigned its value. This, however, seems redundant. Is there a default value, can I set my floats so that this value is not identical to any other value? (Examples include infinity or i)

I am asking for C / C ++ solutions.

+4
source share
8 answers

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

What you described is called a type with a null value in .NET. C ++ implementation of boost :: optional :

boost::optional<int> A; if (A) do_something(*A); 
+7
source

There is an integer value on a two-component machine that is less useful than others: INT_MIN . You cannot make a valid positive value by denying it. Since this is the least useful value in the integer range, it makes a good choice for the marker value. It also has an easily recognizable hex value, 0x80000000.

+5
source

There is no bit pattern that you can assign to an int that is not an actual int. You need to keep separate flags if you really don't have integer values ​​that go beyond.

+2
source

If the range of valid int values ​​is not limited, the only choice is a control bit indicating whether it is assigned or not.

But are you sure that MAX_INT is the desired option?

+1
source

It cannot be guaranteed that the value assigned to int will not equal another random int. The only way to ensure that what you want is to create a separate bool to account for changes.

+1
source

No, you will need to create your own data type that contains information about whether it was assigned or not.

+1
source

If, as you say, not a single integer value is out of bounds, you cannot set the default value to "uninitialized". Just use the structure with int and bool, as you suggest in your question.

0
source

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

My first guess was to use the flag efficiently and flag each variable. But this, of course, is not your only choice.

  • You can use pointers (which can be NULL) and dynamically assign memory. Not very comfortable.
  • You can select a custom value that is almost never used. You can then define this value as the default value. Ofc, for some time you will need to assign this value to your floats, but this case will not happen often, and you just need to keep track of these variables. Given the occurrence of such a case, a simple linked list should be made.
0
source

All Articles