14,303,528 members

C / C++ / MFC

 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" David Crow11-Jan-19 5:40 David Crow 11-Jan-19 5:40
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student11-Jan-19 7:17 HS_C_Student 11-Jan-19 7:17
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" David Crow11-Jan-19 7:33 David Crow 11-Jan-19 7:33
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" Joe Woodbury11-Jan-19 11:54 Joe Woodbury 11-Jan-19 11:54
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student11-Jan-19 12:18 HS_C_Student 11-Jan-19 12:18
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" leon de boer12-Jan-19 3:36 leon de boer 12-Jan-19 3:36
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student12-Jan-19 4:59 HS_C_Student 12-Jan-19 4:59
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" Bram van Kampen12-Jan-19 14:01 Bram van Kampen 12-Jan-19 14:01
 Well, I hope this is a question by a student. It would prove that there is at least One student still studying the 'C' language in this world. It also gives me a dark impression of your teacher. (unless if you missed lessons, or was not paying attention) The situation is clearly not understood by you in terms of Bits and Bytes. Signed or Unsigned is all a matter of interpretation, and, something called 2 compliment arithmetic. One Byte of 8 bits can only have 256 possible values, 0 to 255! (Hope you know and understand that much) The question is, how do we interpret this. We can take this as an unsigned range of 0 to 255, or, as a range of -127 to + 127. In the latter case the most significant bit will represent the sign. 1 for Negative, 0 for positive. The beauty of this scheme is the Two Complement Inversion. Every Number can have it's sign changed by first inverting all bits, and then adding One. Addition and Subtraction using that scheme works correctly all the time. It is so good, the machine is not interested in whether a byte is signed or unsigned! It uses the same mechanism to do arithmetic for both! A Byte of value 0xFF will per definition always be interpreted as 255 by the compiler if it is of unsigned type. It will also always be interpreted by the compiler as -1, if it is of signed type. Actually, an Unsigned 32 bit Int initialised to -1, would have the value 4294967295, the same as an Unsigned Byte initialised to -1 would have the value of 255. The Compiler is friendly! If you initialise a signed byte with 255, it's value will be -1! There is hence absolutely no point in testing an unsigned integer for being negative! It has no capacity of being negative! A half decent compiler will probably optimise the test for this condition out! An Unsigned Integer is always, and, per definition, equal or larger than 0. I hope I managed to explain in the above as to WHY! As to your problem of access violations, After a Brief look at your code: (unsigned int32)-1 means for the compiler: 4294967295. So Your essentially loop says for(i=0; i<4294967295;i++){...} Make your iterator Signed! Tell me how you get on after that! Regards Bram van Kampenmodified 12-Jan-19 20:25pm.
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student12-Jan-19 16:50 HS_C_Student 12-Jan-19 16:50
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" Richard MacCutchan12-Jan-19 22:51 Richard MacCutchan 12-Jan-19 22:51
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student13-Jan-19 7:10 HS_C_Student 13-Jan-19 7:10
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" Richard MacCutchan13-Jan-19 22:12 Richard MacCutchan 13-Jan-19 22:12
 Demonstrating the relevant case and potential best practice HS_C_Student13-Jan-19 6:46 HS_C_Student 13-Jan-19 6:46
 Re: Demonstrating the relevant case and potential best practice Richard MacCutchan13-Jan-19 22:15 Richard MacCutchan 13-Jan-19 22:15
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" Stefan_Lang16-Jan-19 23:13 Stefan_Lang 16-Jan-19 23:13
 Re: (C) Robust code Should an unsigned int array index be tested for "< 0" HS_C_Student17-Jan-19 13:05 HS_C_Student 17-Jan-19 13:05
 academic question - usage of class variable Vaclav_10-Jan-19 5:20 Vaclav_ 10-Jan-19 5:20
 Re: academic question - usage of class variable Richard MacCutchan10-Jan-19 6:27 Richard MacCutchan 10-Jan-19 6:27
 Re: academic question - usage of class variable CPallini10-Jan-19 21:28 CPallini 10-Jan-19 21:28
 Re: academic question - usage of class variable Vaclav_11-Jan-19 3:25 Vaclav_ 11-Jan-19 3:25
 Re: academic question - usage of class variable CPallini11-Jan-19 5:21 CPallini 11-Jan-19 5:21
 Re: academic question - usage of class variable David Crow11-Jan-19 5:44 David Crow 11-Jan-19 5:44
 Re: academic question - usage of class variable Stefan_Lang16-Jan-19 22:26 Stefan_Lang 16-Jan-19 22:26
 Changing CMainFrame Minimize ICON ForNow9-Jan-19 14:02 ForNow 9-Jan-19 14:02
 Re: Changing CMainFrame Minimize ICON David Crow10-Jan-19 4:07 David Crow 10-Jan-19 4:07
 Last Visit: 21-Sep-19 7:11     Last Update: 21-Sep-19 7:11 Refresh « Prev1...41424344454647484950 Next »