Strange result
-
Hello dwCopyLen = dwBufSize - dwSkipCount - HVD_HEADER_SIZE; how can dwCopyLen become 821 when: dwBufSize = 815 dwSkipCount = 0 #define HVD_HEADER_SIZE 1+2+4+1 //stx+USHORT+DWORD+char strange huh? If i split it to three lines: dwCopyLen = dwBufSize; dwCopyLen -= HVD_HEADER_SIZE; dwCopyLen -= dwSkipCount; I get the correct result. Why?
-
Hello dwCopyLen = dwBufSize - dwSkipCount - HVD_HEADER_SIZE; how can dwCopyLen become 821 when: dwBufSize = 815 dwSkipCount = 0 #define HVD_HEADER_SIZE 1+2+4+1 //stx+USHORT+DWORD+char strange huh? If i split it to three lines: dwCopyLen = dwBufSize; dwCopyLen -= HVD_HEADER_SIZE; dwCopyLen -= dwSkipCount; I get the correct result. Why?
This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do
#define HVD_HEADER_SIZE (1 + 2 + 4 + 1)
to get the results you are expecting! -
This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do
#define HVD_HEADER_SIZE (1 + 2 + 4 + 1)
to get the results you are expecting!Martyn Pearson wrote: This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting! ofcourse :omg: Why didnt I think of that? :-D Many thanks!
-
Martyn Pearson wrote: This comes down to the order in which the operands are evaluated. Your first line expands to 815 - 0 - 1 + 2 + 4 + 1 i.e. 814 + 2 + 4 + 1 = 821 Do #define HVD_HEADER_SIZE (1 + 2 + 4 + 1) to get the results you are expecting! ofcourse :omg: Why didnt I think of that? :-D Many thanks!
Lesson 1: Don't use macros unless you can't do without. const int HVD_HEADER_SIZE = 1 + sizeof(USHORT) + sizeof(DWORD) + sizeof(char); would have done the job. Lesson 2: If you have to embrace macros, embrace them correctly: braces go 'round the entire expression, and each argument :)
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen