Corrupted variables?

I've got misbehaving code. Here's the relevant excerpt:

b2[3]=0;
Serial.println(cmd);
b2[4]=0;
Serial.println(cmd);

"cmd" changes from 4 to 0 in the above lines. I thought I might be out of SRAM for variable space, but I slashed my resource requirements well under 2k & I'm still getting the same thing. What's going on?

cmd was allocated on the stack and b2 has replaced it.

Please post your complete code, using code tags (the "#" button above the row of smileys).

I've got misbehaving code. Here's the relevant excerpt:

Don't you just hate it when this happened. Bad code!

b2[4]=0; may be writing into the cmd location.
Are you sure you made b2[] large enough?

LarryD:
b2[4]=0; may be writing into the cmd location.
Are you sure you made b2[] large enough?

Another possibility: he has declared b2 as a pointer, made it point to the wrong address(cmd) and now using it (b2) as an array.
There's only so many ways to mess this one up. (S)he should dump the addresses of cmd and b2.

There's only so many ways to mess this one up.

The number of ways might be limited, but it isn't a small number. 8)

b2 is a simple array of bytes, which has been declared thusly: "byte b2[4];"

cmd is an integer.

Declaring byte b2[4] means that its elements are numbered 0 .. 3.

The following statement is going beyond the end of the array.

b2[4]=0;

Which shows why to tell you to post ALL the code.

Mark