The programming language is C++ and the bug only happens when in Release, in Debug everything is fine.When trying to do a sum of the elements in a char array in a loop or one after the other, the result is not the expected one. This problem happens when the sum is made in a char with a succession of "+" and the target char is 0 at the beginning.ex.:#include <iostream>int main(){ unsigned char byContent[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF }; unsigned char *data = new unsigned char[2048]; memcpy(data, byContent, 16); unsigned int *actualData = (unsigned int*) data; unsigned int uiCheckSum = 0; unsigned int cnt = 0; //the 4 is 16/4, the 16 is the size given to memcpy for (cnt=0; cnt<4; cnt++, actualData++) { uiCheckSum += *actualData; } unsigned char checkSum = 0; unsigned char *totalCheckSum = (unsigned char*)&uiCheckSum; /*for (cnt=0; cnt<4; cnt++) { //printf("losing time"); checkSum = (unsigned char)(checkSum + totalCheckSum[cnt]); }*/ //printf("totalCheckSum[0] = %i\n", totalCheckSum[0]); //printf("totalCheckSum[1] = %i\n", totalCheckSum[1]); //printf("totalCheckSum[2] = %i\n", totalCheckSum[2]); //printf("totalCheckSum[3] = %i\n", totalCheckSum[3]); checkSum = (unsigned char)(checkSum + totalCheckSum[0]); checkSum = (unsigned char)(checkSum + totalCheckSum[1]); printf("checkSum = %i and should be equal to 52 (24 + 28)\n", checkSum); checkSum = (unsigned char)(checkSum + totalCheckSum[2]); checkSum = (unsigned char)(checkSum + totalCheckSum[3]); printf("checkSum = %i and should be equal to 120", checkSum); while(1) {}}The first summation seems to modify the value of the next char in the array. If checkSum is not 0 at the beginning, everything is fine and if the printf() are not commented, it works too because there is no optimization.
Visual Studio/Silverlight/Tooling version
What category (if any) best represents this feedback?
Steps to reproduce
Product Language
Operating System
Operating System Language
Actual results
Expected results
Please wait...