I have a problem with my code. When I run the code the short “opcode” has the wrong value 52496. So I debug the code step by step… and when I am doing this “opcode” has the correct value 4624! Can someone give me a hint?
void packet_get()
{
boost::shared_ptr<boost::array<unsigned char, 2>> opc(new boost::array<unsigned char, 2>);
recv_two_bytes(opc);
unsigned short opcode;
unsigned char * test[2];
test[0] = &opc->at(0); // *test[0] == 0x12
test[1] = &opc->at(1); // *test[1] == 0x10
opcode = 0;
int i = 0;
for(i = 0; i <= 1; i++)
{
opcode = (opcode<<8) | *(test[i]);
}
// opcode should now be short 4624
}
Usually, when the behavior of the program is different between normal and debug runs, it is due to an undefined behavior. One such common mistake is uninitialized variables.
When you execute a program, it is given a stack that is most likely uninitialized. In debug mode, it is possible for the debugger to initialize this stack. Therefore, an uninitialized variable can easily have different values in debug and normal execution (even 0 in debug mode, which most of the times is what you actually wanted to give the variable but forgot).
It seems like you have some error like that in your
recv_two_bytesfunction. Enabling all warnings on your compiler will help pin down the problem if it is more trivial.Be on the look out for other errors such as indexing out of array also.