You need to show us the definitions for nod
. There's a good chance (based on the fact you're starting arrays at 1, not 0) that you're overwriting memory.
For example, if nod is defined as:
int nod[3][2];
the possible array subscripts are 0-2
and 0-1
, not 1-3
and 1-2
:
nod[0][0] nod[1][0] nod[2][0]
nod[0][1] nod[1][1] nod[2][1]
If that is the case, you're memory is almost certainly being over-written, in which case all bets are off. You could be corrupting any other piece of data.
If len
is placed in memory immediately following nod
, this memory overflow would explain why it's being changed. The following diagram will (attempt to) illustrate this. Let's say your nod
definition is:
int nod[3][2];
but you attempt to set nod[1-3][1-2]
instead of nod[0-2][0-1]
:
+-----------+
+0000 | nod[0][0] |
+-----------+
+0004 | nod[0][1] |
+-----------+
+0008 | nod[1][0] |
+-----------+
+000c | nod[1][1] |
+-----------+
+0010 | nod[2][0] |
+-----------+
+0014 | nod[2][1] |
+-----------+
+0018 | len[0] | and nod[3][0], should you be foolish enough to try :-)
+-----------+
+001c | len[1] | and nod[3][1] *
+-----------+
+0020 | len[2] | and nod[3][2] *
+-----------+
C/C++ will not check regular array bounds for overflow. So, if you attempt to set nod[3][something-or-other]
, you'll find yourself in trouble very similar to what your question describes.
The bit patterns you're using (3 and 4) equate to IEEE754 single-precision 4.2x10-45 and 5.6x10-45 respectively so they'd certainly give 0.0000
when printing (since you don't appear to be using a format string which would give you the more precise value).
A good way to test this theory would be to output the len
variables immediately before and after setting the relevant nod
variables, something like:
printf ("before: len1 = %f, len2 = %f\n", len[1], len[2]);
nod[3][1] = 3;
nod[3][2] = 4;
printf ("after : len1 = %f, len2 = %f\n", len[1], len[2]);
Actual details as to how the variables are laid out in memory may be different to that described above but the theory still holds.
Two possible solutions if that turns out to be the problem.
- Use zero-base arrays as C/C++ intended; or
- Define them with enough space to handle your unusual use, such as
int nod[4][3]
.