Memset enum array values not setting correctly (C/C++) - c++

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.

Related

in c or c++, how can you distinguish between 0 which is set on an address in memory and 0 which is set because that address is not initialized?

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.)

Initialize memory with nan in C++ for debugging

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();

Using a flag number within unsigned integers

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.

Default value of an integer?

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.

Initializer list makes variable uninitialized?

I have a class with the only constructor like this:
IntroScreen::IntroScreen(Game *game) :
View(game), counter(0.0f), message(-1), continueAlpha(255),
continueVisible(false), screenAlpha(255), fadeIn(false), fadeOut(false)
{
}
And somewhere in a method I have this if-statement
if (counter > 10.0f)
And Valgrind says for that line:
Conditional jump or move depends on uninitialised value(s)
But I initialized it in my initializer list! And I think I believe Valgrind. Because, sometimes everything goes correct and sometimes nothing happens.... So, maybe counter gets a wrong value and so it takes long until the counter reaches 10.
I already check my code where I use counter for some errors. But I think you can't "un-initialize a value" with a C++ statement...
These are ALL the lines (except in the initializer list) where I use counter:
counter += speed;
counter = 20.0f;
counter += game->getSpeedFactor();
if (counter >= 15.f)
counter = 15.f;
if (counter > 10.0f)
Valgrind gives the same output for screenAlpha.
Both variables are private and I have no friend classes....
So, what is going on? What the problem could be?
Edit:
I printed the value out:
In the constructor, it was correnct: 0
In my method, it was rubbish. It prited random values like:
-97298.8...
-106542.2...
The print statement is the first line of the method where all assignments to counter are in.
Second Edit:
Can this be the problem!!??
In my Game class, I initialize that IntroScreen like this:
Game::Game() : /* Some other stuff .... */ , view(new IntroScreen(this))`
{}
view is here a pointer to an abstract super-type of IntroScreen called View.
Did you accidentally shadow counter with a local variable that's uninitialized?
Otherwise, it's possible that valgrind is mid-diagnosing this in an object that was already deleted (using sentry values perhaps).
Or valgrind could just be wrong.
Not enough code to reproduce problem.
Generic SO / developer forum advice:
Do provide a minimal code snippet reproducing the problem.
Quite often (about 85% of all cases in my experience) the process of reducing the code snippet already uncovers the bug for you.
Edit: Your addition still doesn't give a compilable example of your problem, but enough information at least to identify then problem - or, at least, one of them:
Game::Game() : /* Some other stuff .... */ , view(new IntroScreen(this))`
{}
I am not sure whether a new() call is even legal in an initializer list. But I am sure that you do not have a fully initialized this at this point, so chances are your IntroScreen constructor does bogus things.
I found it:
getSpeedFactor() returns only the first time I call it a complete wrong number because of time-functions like gettimeofday(). The start value (to time how long it took to update the game) is set initialized to zero and the stop value is micros of the day: which gives a time of the whole day instead of the update time. Once the game loop ran once, the wrong value is corrected (because of the start value gets assigned). But the first time the game-logic was executed, I used getSpeedFactor() to assign counter, so that way counter get a value of -10000...
Thanks all.
Off the top of my head, you may need to define private default and copy constructors that have proper initializers. Haven't used valgrind, but it's a common thing to forget to do.
Just add a debugging printf statement or equivalent, if you have doubts. But I would not believe Valgrind this time.
BTW: delete does not "un-initliase" a value. It deletes object, but the pointer still points to that memory location — it does have a value.