|
|
A problem assigning a value
to an array of a structure
Update 29 March 2015:I
found the problem (at bottom of page here)
I created a very simple structure: |
struct testFloatStruct
{
double
A;
double
B;
};
Then I made an array of four of them: |
struct testFloatStruct
threeSetsOfFloats[4];
That creates a total of six values, and there is a corresponding "text
box" for each of them so the user can enter a value in them.
However, to illustrate the problem I am having, I simplified the code so that it ignores what the user types and puts in unique value into each element of the array. For example,: |
threeSetsOfFloats[0].A
= 0.1;
I can enter a value into the first value of the array without any trouble, but when I enter a value into any of the others, the valllue following it is altered. Specifically, if I set the second value like this: |
threeSetsOfFloats[0].B
= 0.2;
The debugger shows that as soon as that statement is executed, the
following
element in the array, ( [1].A ) picks
up a bizarre value.
The screen image below shows that Visual Studio is just about to execute the assignment statement. Notice that in the watch window, all elements in the structure array are zero. The program has just begun, and this is the very first assignment. |
The screen image below shows what happens after that assignment has been made. Notice that in the watch window, four values turned red. And notice the strange value in element [1].A |
If I change the structure from doubles to floats, then
when I assign a value in element [0].B, element ( [1].A ) picks
up the exact same value rather than some bizarre number.
What am I doing wrong? I am not too familiar with Visual Studio since I was previously using the Borland compiler, so I don't know if I'm setting something wrong with the hundreds of compiler options. I simplified this test project to the point where it only has six assignment statements, and I zipped it up so that you can try it yourself: Also, note that I created four elements in this array ( threeSetsOfFloats [ 4 ] ), but I access only the first three elements. However, when I set element [2].B, it sets element [3].A If I did not have four elements, I suppose it would alter whatever is
in memory beyond [2].B
|
It appears to be a bug with the Visual Studio debugger.
After posting this problem at stackoverflow.com, here, one of the suggestions caused me to wonder what would happen if I used sprintf to display the values in the array. To my amazement, the values were correct when using sprintf, even though the debugger was showing an incorrect value. I then switched the array from doubles to floats to see if it would have the same effect, and it did. The screen image below shows this bizarre situation. |
Notice that in the image above, execution is at line 255. The sprintf
statement was just completed, with i having a value of one. The mouse is
hovering over the variable:
threeSetsOfFloats[ i ].A as shown by the red arrow in the image, and you can see underneath it that the debugger is claiming that A has a value of .2. Then notice that in the watch window, the debugger al;so shows [ 1 ].A as .2, but tempbuf shows that it is actually zero. So, we could say that this is a bug with the debugger.
Incidentally, in the image above, I am using the function OutputDebugString()
to show the values in the output window, but it would not show anything
in this particular project. It works in one of my other projects, so why
not this one? Another Microsoft mystery! Maybe it has to do with the hundreds
of confusing compiler and project options.
Now that this problem has been solved, perhaps somebody can deal with the art ball question here. Or, maybe you would like to try the program to calculate the speed of bicycles or other vehicles here, and perhaps fix it up a bit. Eric erichuf@aol.com 29 March 2015 |