Note that in C and C++, array indexes are 0-based. So, if you have an array of N items, valid index values are 0,1,2,...,(N-1).
In your case, N = 7, so valid index values are 0,1,2,3,4,5,6.
So, substitute <=
with <
in your for loop:
UINT8 temp[7];
for (int i = 0; i < 7; i++) // Use <, not <=
....
Moreover, there is a convenient _countof()
macro available with VS in <stdlib.h>
, which makes your code easier to read and maintain (instead of using the "magic number" 7 in your for
loop):
for (int i = 0; i < _countof(temp); i++)
Using _countof()
, if you change the size of the array, your loop code will still work, without modifying 7 to the new array size.
Note also that in C++11 it's possible to use range-based for loops (but they are not available in VC10/Visual Studio 2010, which you used as a tag in this question).
Note that if you want to fill a buffer with a given byte sequence you can use memset
(a C-like way):
memset(temp, 0x01, sizeof(temp));
Or, more generally (even for elements larger than one byte), you can use C++ std::fill
from <algorithm>
:
fill(begin(temp), end(temp), 0x01);
Using an explicit algorithm like std::fill
makes your code more readable, because it rises the "semantic level" of the source code (you just read "fill" instead of reading a "raw" for
loop, the meaning of which you have to interpret).