Question: Recently I changed some code

```
double d0, d1;
// ... assign things to d0/d1 ...
double result = f(d0, d1)

```

to

```
double d[2];
// ... assign things to d[0]/d[1]
double result = f(d[0], d[1]);

```

I did not change any of the assignments to `d`, nor the calculations in `f`, nor anything else apart from the fact that the doubles are now stored in a fixed-length array.

However when compiling in release mode, with optimizations on, `result` changed.

My question is, why, and what should I know about how I should store doubles? Is one way more efficient, or better, than the other? Are there memory alignment issues? I'm looking for any information that would help me understand what's going on.

---

**EDIT:** I will try to get some code demonstrating the problem, however this is quite hard as the process that these numbers go through is huge (a lot of maths, numerical solvers, etc.).

However **there is no change when compiled in Debug**. I will double check this again to make sure but this is almost certain, i.e. the double values are *identical* in Debug between version 1 and version 2.

Comparing Debug to Release, results have never ever been the same between the two compilation modes, for various optimization reasons.

Answer: 
You probably have a 'fast math' compiler switch turned on, or are doing something in the "assign things" (which we can't see) which allows the compiler to legally reorder calculations. Even though the sequences are equivalent, it's likely the optimizer is treating them differently, so you end up with slightly different code generation. If it's reordered, you end up with slight differences in the least significant bits. Such is life with floating point.

You can prevent this by not using 'fast math' (if that's turned on), or forcing ordering thru the way you construct the formulas and intermediate values. Even that's hard (impossible?) to guarantee. The question is really "Why is the compiler generating different code for arrays vs numbered variables?", but that's basically an analysis of the code generator.
With regard to floating-point semantics, these are equivalent. However, it is conceivable that the compiler might decide to generate slightly different code sequences for the two, and that could result in differences in the result.

Can you post a complete code example that illustrates the difference? Without that to go on, anything anyone posts as an answer is just speculation.

To your concerns: memory alignment cannot effect the value of a double, and a compiler should be able to generate equivalent code for either example, so you don't need to worry that you're doing something *wrong* (at least, not in the limited example you posted).