Click here to Skip to main content
14,207,187 members

C / C++ / MFC

 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 14:01
memberBram van Kampen12-Jan-19 14:01 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 16:50
memberHS_C_Student12-Jan-19 16:50 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan12-Jan-19 22:51
protectorRichard MacCutchan12-Jan-19 22:51 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student13-Jan-19 7:10
memberHS_C_Student13-Jan-19 7:10 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan13-Jan-19 22:12
protectorRichard MacCutchan13-Jan-19 22:12 
AnswerDemonstrating the relevant case and potential best practice Pin
HS_C_Student13-Jan-19 6:46
memberHS_C_Student13-Jan-19 6:46 
GeneralRe: Demonstrating the relevant case and potential best practice Pin
Richard MacCutchan13-Jan-19 22:15
protectorRichard MacCutchan13-Jan-19 22:15 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Stefan_Lang16-Jan-19 23:13
memberStefan_Lang16-Jan-19 23:13 
I haven't read all of the responses in detail, so excuse me if some of the below has already be mentioned.

1. If the function argument is defined as unsigned, a signed int will be silently transformed into an unsigned leading to potentially inappropriate behavior from the viewpoint of the caller (already mentioned). The main issue here is the silent transformation that may at best be indicated as a warning at compile time. For that reason, it might be worth considering to change the argument type to signed. Some of the big guys in C++ programming think that the use of unsigned is often not worth the hassle and may convey a false sense of security. Point in case: if you have a variable of type array index, it's true that a negative value doesn't make sense. But defining it unsigned still doesn't make it range-safe as you still need to check the upper bound. Being unsigned safes you one check, but introduces the new problem of silent signed to unsigned conversions. In the end you gain nothing.

2. The test <0 always returns true on an unsigned variable. Therefore there is no point to make it. If you want to make a two-sided bounds check on an unsigned, checking just the upper bound actually treats both, because negative values will be converted to very large positive values!

3. regarding #3: don't worry about possible future extensions! Make your code look reasonable and meaningful from the point of view of the requirements that you have now! Chances are, that any future requirement will look different than you're anticipating. Furthermore, if anything problematic of a scope you're describing here is going to happen, a lot more than your function will be affected, and it's quite possible someone will come up with some generic workaround that can cope with this problem; however, if you make your code try to anticipate that change, the generic code may not work on it, because it doesn't look as expected! Better just design your code in the most reasonable way now!

4. You can avoid a lot of these problems if you simply use std::array
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student17-Jan-19 13:05
memberHS_C_Student17-Jan-19 13:05 
Questionacademic question - usage of class variable Pin
Vaclav_10-Jan-19 5:20
memberVaclav_10-Jan-19 5:20 
AnswerRe: academic question - usage of class variable Pin
Richard MacCutchan10-Jan-19 6:27
protectorRichard MacCutchan10-Jan-19 6:27 
AnswerRe: academic question - usage of class variable Pin
CPallini10-Jan-19 21:28
mveCPallini10-Jan-19 21:28 
GeneralRe: academic question - usage of class variable Pin
Vaclav_11-Jan-19 3:25
memberVaclav_11-Jan-19 3:25 
GeneralRe: academic question - usage of class variable Pin
CPallini11-Jan-19 5:21
mveCPallini11-Jan-19 5:21 
GeneralRe: academic question - usage of class variable Pin
David Crow11-Jan-19 5:44
memberDavid Crow11-Jan-19 5:44 
AnswerRe: academic question - usage of class variable Pin
Stefan_Lang16-Jan-19 22:26
memberStefan_Lang16-Jan-19 22:26 
QuestionChanging CMainFrame Minimize ICON Pin
ForNow9-Jan-19 14:02
memberForNow9-Jan-19 14:02 
QuestionRe: Changing CMainFrame Minimize ICON Pin
David Crow10-Jan-19 4:07
memberDavid Crow10-Jan-19 4:07 
AnswerRe: Changing CMainFrame Minimize ICON Pin
ForNow10-Jan-19 4:39
memberForNow10-Jan-19 4:39 
GeneralRe: Changing CMainFrame Minimize ICON Pin
David Crow10-Jan-19 4:42
memberDavid Crow10-Jan-19 4:42 
GeneralRe: Changing CMainFrame Minimize ICON Pin
ForNow10-Jan-19 5:09
memberForNow10-Jan-19 5:09 
QuestionUsage of bitset ? Pin
Vaclav_9-Jan-19 5:12
memberVaclav_9-Jan-19 5:12 
AnswerRe: Usage of bitset ? Pin
Daniel Pfeffer9-Jan-19 5:46
professionalDaniel Pfeffer9-Jan-19 5:46 
AnswerRe: Usage of bitset ? Pin
k50549-Jan-19 6:09
memberk50549-Jan-19 6:09 
GeneralRe: Usage of bitset ? Pin
Daniel Pfeffer9-Jan-19 21:38
professionalDaniel Pfeffer9-Jan-19 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190612.1 | Last Updated 15 Jun 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid