Strange behavior of floats in C++

May 27, 2009 at 12:56am
I'm getting some very strange behavior in an algorithm that I am creating.

I have a test algorithm in C++ that is supposed to produce a particular result. At the moment, I'm getting the appropriate result; however, when I make seemingly unrelated additions to the algorithm, the output changes.

The algorithm provides some output to an array of float and this is then output to the console. I want to make some changes to the algorithm. Almost anything I do changes the output of the algorithm, even if that change doesn't have anything to do with the rest of the algorithm. For example, if I declare and define a new variable below the rest of the code, it changes the output of the algorithm. This makes no sense to me.

Does anyone have any suggestions about how I might track down this kind of problem. Any suggestions, would greatly appreciate it.

I didn't include the code because it's fairly big, but I could include it if it is any help.
May 27, 2009 at 1:24am
Without looking at the code, all we can do is guess.

My guess is that your code is relying on the movements of your floats inside the CPU, and breaks when the floats are stored back to memory.
May 27, 2009 at 3:18am
You're probably missing the initialization of something.
May 27, 2009 at 9:18am
i had an almost identical problem once, and it was due to an uninitialized variable.
May 27, 2009 at 10:41pm
Thank you for your suggestions about my vaguely worded question. It appears that you were correct. (I'm still crossing my fingers in case more bugs appear.) The two issues that I discovered were:

1. Some variables were not initialized before they were used.

2. Some static variables needed to be explicitly initialized. (I thought that static variables were automatically initialized to zero, but I guess not.)

I am glad that the problem seems to be resolved, but I am still a little baffled. How is it that variables defined late in the program can mess up things that have happened already? I can only conclude that the compiler does not do everything in the order explicitly stated in the program. The compiler must make spaces in memory for variables defined later in the program. It must do this before it completes executing the earlier pieces of code. By looking at the code, the pieces don't seem to be connected; however, in the memory space they are overwriting each other.

Does anyone have any further explanation? I'm curious to learn more.
Last edited on May 27, 2009 at 10:43pm
May 29, 2009 at 10:44pm
I got to the very bottom of the problem. I found an uninitialized variable. As long as the uninitialized variable was "lucky" enough to be zero, then it produced the appropriate output.

However, defining new variables caused different pieces of memory to be used. This changed the initial value of the uninitialized variable, thereby messing up the output.

I don't know whether static variables are automatically initialized to zero. They appear to be, but I don't trust it. The general consensus is yes, but that it's not good to depend on it. Also, I have found the occasional complaint about static variables NOT automatically initialized to zero.

If this is the case, I need to find a way to initialize a big array to zero without doing this:

static float foo[1000] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... };

That's all.

Cheers!
May 29, 2009 at 10:51pm
static float foo[1000]={0};
OR
memset(foo,0,sizeof(float)*1000);
May 29, 2009 at 11:00pm
Thanks! I forgot that.
Topic archived. No new replies allowed.