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.
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);
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.
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.
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?
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.
No, you will have to create your own data type which contains the information about whether it has been assigned or not.
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.
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.
You can use pointers (which can be NULL) and assign dynamically the memory. Not very convenient.
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.
Related
Updates:
The reason I was getting true is because anything that is other than 0 would be considered true which obviously makes sense to how unlikely it would have been for me to get false when uninitialized.
I have read a post similar to my question on StackOverflow, it talked about that it is good practise to initialize all member variables, which I agree with.
Post: Boolean variables aren't always false by default?
This post is a bit old (9 years ago) so I just think maybe somethings might have been changed in the new C++ versions, I am currently using C++17. I also have one slight different question from the ones talked about in the post.
I am aware that if a variable is uninitialized it may contain some "garbage data" or as one of the answers in the post said (which I think that is what they meant but I'm not 100% sure), "if not explicitly initialized -- will contain an arbitrary value.".
I have tried testing that, and the results showed that when I didn't initialize my variables, they contained random numbers (for int, double). I also tested std::string but they are set to "" by default from what I saw.
Anyways, now when I tried for the built in type bool I would always get true (after class is constructed, but again that boolean value never initialized, I would go into debug and see that the value would be true), what I am confused is that no matter how many times I tried to test if that was just a random value out of true and false, and if sometimes it would be false, it was always set to true. If uninitialized shouldn't the value be "random" kind of? Why did it always set to true (again when a member variable of my class which wasn't initialized on construction).
Solutions I tried:
Obviously one is just to initialize on construction, but I thought of another one...
What if on construction I wanted it to be true and not false but when it hasn't been constructed to be set to false then that way when I have a vector of pointers to my object I can check that I am not reading uninitialized objects when following the pointer by checking whether that boolean is set to true (if initialized) or false otherwise. I wouldn't be able to use method 1 which was to initialize on construction to false, also because if we are reversing the behaviour I can't rely on what that uninitialized boolean member variable would be as I mentioned in the above paragraphs I am unsure what behaviour that has due to the results I had been getting. I did the following and it worked...
class Testing{
private:
bool condition{false} // Initalize it here which kind of makes me confused but it works
public:
Testing() : condition{true} {} // Constructor setting the condition value to true
};
Could someone explain if it is wrong to do this, personally I have never seen someone do this but I tried it and no errors were given.
While bool conceptually contains only one bit of information, the requirements of the C++ standard mean that a bool object must take up at least eight bits. There's three main ways that a compiler might represent bool at a bitwise level:
All bits zeroed for false; all bits one'd for true (0x00 versus 0xFF)
All bits zeroed for false; the lowest bit one'd for true (0x00 versus 0x01)
All bits zeroed for false; at least one bit one'd for true(0x00 versus anything else)
(Note that this choice of representation is not ordinarily visible in the effects of a program. Regardless of how the bits are represented, a bool becomes a 0 or 1 when casted to a wider integer type. It's only relevant to the machine code being generated.)
In practice, modern x86/x64 compilers go with option 2. There are instructions which make this straightforward, it makes casting bool to int trivial, and comparing bools works without additional effort.
A side effect is that if the bits making up a bool end up set to, say, 0x37, weird stuff can happen, because the executable code isn't expecting that. For instance, both branches of an if-statement might be taken. A good debugger should loudly yell at you when it sees a bool with an unexpected bit pattern, but in practice they tend to show the value as true.
The common theme of all those options is that most random bit patterns are not the bit pattern for false. So if the allocator really did set it to a "random" value, it almost certainly would be shown as true in the debugger.
I have an if condition in my code, which I check the value which is set in a specific address in memory. If this value is 0, then I assume that this address is not set and will do some actions. But there can be the case where I do this check and value is 0 because I have set it to 0 somewhere else in the code. How can I distinguish between these two conditions?
You don't. There's no way to distinguish a 0 from a 0.
You can't. You'll have to define additional, static variable, say
int initialized = 0;
and set it to 1 when you assign your memory location any new value. Then you can test if(initialized)... to tell zero from zero.
You cannot distinguish between different '0', 0 is 0.
It is also intrinsically difficult to represent more than 2 states with a binary variable.
As far as I understand you have more than 2 states.
1. Variable =0 not initialized, do stuff.
2. Variable =1 initialized, do other stuff.
3. Variable =0 and initialized, do different stuff.
There is no way to understand the state without using additional information.
So you could use another data structure to save more information.
The two zeros are indistinguishable.
You could use a std::pair<T, bool> to encapsulate the initialisation status (in the bool) of a variable of type T.
(std::pair<T, bool> is used in the C++ standard library as the return value of an insertion into a std::map.)
How would I initialize all memory in a c or c++ program to NaN (Not-A-Number) at startup for debugging with gdb?
I believe by default gdb initializes with zeros, but this is often not helpful for finding code that crashes due to initialization error.
PS: I want to initialize every variable and array as NAN (or some garbage) for debugging only. The program I am working with has thousands of variables, so rather tedious to change every one...
Those hex numbers might be correct in Rafael's post, but I would recommend a more semantic way.
See http://en.cppreference.com/w/cpp/types/numeric_limits/quiet_NaN
#include <limits>
double nan1 = std::numeric_limits<double>::quiet_NaN();
double nan2 = std::numeric_limits<double>::signaling_NaN();
Note that there are two kinds of NaN.
You can cast your floats to 32-bit ints and set them to any number between
0x7FC00000 and 0x7FFFFFFF or
0xFFC00000 and 0xFFFFFFFF
For doubles cast to u64 and set them to any number between
0x7FF8000000000000 and 0x7FFFFFFFFFFFFFFF or
0xFFF8000000000000 and 0xFFFFFFFFFFFFFFFF
What do you mean by "initialize all memory"? This is probably only possible on some bare metal embedded system, not in Linux. And even there gdb does nothing like that.
Each program has two relevant special memory regions: one is zero initialized, which of course needs to be filled by zeros. This variables are allocated withing region marked as such and their value is not stored in executable. Other is initialized to some explicitly defined value. These values are stored within executable.
While it should be possible to get boundaries of this region (just like C library code does), the question is, why would you want to fill zero initialized region with NaNs. It would cause unwanted side-effects elsewhere in your code. For example, if you have some global int that is initialized to 0, filling this region with NaNs would also change the initial value of that integer to something entirely unexpected.
If you need some variables or array initialized to NaN, just initialize variables appropriately when declaring them (as explained by Notinlist and Rafael). You could use some macro(s), if you really don't want to repeat that ugly long statement every time, something like
#define NaNdouble(X) double X = std::numeric_limits<double>::quiet_NaN();
Many times people will combine a boolean check by just re-using an int variable they already have and checking for -1 if something exists or not.
However, what if someone wants to use unsigned integers but still wants to use this method and also where 0 actually has a different meaning besides existance.
Is there a way to have a data range be -1 to 4,294,967,294?
The obvious choice here is to just use a bool that detects what you are after but it is my understanding that a bool is a byte, and can really add to the storage size if you have an array of structs. This is why I wondered if there was a way to get the most useful numbers you can (postivies) all while leaving just one number to act as a flag.
Infact, if it is possible to do something like shifting the data range of a data type, it would seem like shifting it to something like -10 to 4,294,967,285 would allow you to have 10 boolean flags at no additional cost (bits).
The obvious hacky method here is just to add whatever number to what your storing and remember to account for it later on, but I wanted to keep it a bit more readable (I guess if thats the case I shouldnt even be using -1, but meh).
If you simply want to pick a value which can not exist in your interpretation of the variable and to use it to indicate an exception or error value, why not to simply do it? You can take such a value, define it as a macro and use it. For example if you are sure that your variable never reaches the max limit, put:
#define MY_FUN_ERROR_VALUE (UINT_MAX)
then you can use it as:
unsigned r = my_function_maybe_returning_error();
if (r == MY_FUN_ERROR_VALUE) {handle error}
you shall also ensure that my_function_maybe_returning_error does not return MY_FUN_ERROR_VALUE in normal conditions when actually no error happens. For this you may use an assert:
unsigned my_function_maybe_returning_error() {
...
// branch going to return normal (not error) value r
assert(r != MY_FUN_ERROR_VALUE);
return(r);
}
I do not see anything wrong on this.
You just asked how to use a value that can be 0 or something greater than 0 to hold the three states: whatever 0 means, something greater than 0, and does not exist. So no, (by the pigeonhole principle I guess) it's not possible.
Nor should it be. Overloading a variable is bad practice unless you're down to your last 3 bytes left of RAM, which you almost certainly aren't. So yes, please use another variable with a correct name and clear purpose.
I'm trying to use memset to set all values in an enum array to a single value, but I'm not seeing the correct results. The first memset works, the second does not. My code:
// definitions
#define NUM_THREADS 1
enum ThreadState
{
INITIALIZING,
READY_TO_CALCULATE,
CALCULATED,
READY_TO_UPDATE,
UPDATED
};
// Later on, in the code...
ThreadState Thread_States[NUM_THREADS];
// Somehow this works - after this statement, every entry in Thread_States is INITIALIZING
memset(Thread_States, INITIALIZING, NUM_THREADS* sizeof(ThreadState));
// ... later on (or even immediately right after) ...
// Failure - after this statement, every entry in Thread_States is 16843009
memset(Thread_States, READY_TO_CALCULATE, NUM_THREADS* sizeof(ThreadState));
As explained in the comments, the first time I call memset, the values are set to what I expect (INITIALIZING, i.e., 0). When I run the second statement, I don't see the values set to READY_TO_CALCULATE (i.e., 1). Rather, they're set to 16843009, when I check the debugger.
Is there a reason this relatively simple use of memset is inconsistent in its behavior?
Thank you.
The memset function sets each byte of the memory to the second argument (after the second argument is truncated). As enumerations are (normally) the size of int you will get the wrong result. The only time it will work is for an enumeration value of zero, as it will then set all bytes to zero.
If you use e.g. READY_TO_CALCULATE you will set each byte to 1, which will create int values of 0x01010101 instead of 0x00000001.
Is your question C or C++?
In case of C, remember to loop over the array to set each value, as memset is just not meant for this kind of function. You're trying to set elements, not memory.
In case of C++, use an enum class for the state. Also, try to encapsulate your thread state in its own class that manages it, and make the default initializer construct it properly.