Click here to Skip to main content
15,883,961 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: error: expected identifier before string constant Pin
Stefan_Lang17-Jan-19 1:30
Stefan_Lang17-Jan-19 1:30 
AnswerRe: error: expected identifier before string constant Pin
k505413-Jan-19 8:29
mvek505413-Jan-19 8:29 
AnswerRe: error: expected identifier before string constant Pin
Graham Breach13-Jan-19 23:21
Graham Breach13-Jan-19 23:21 
GeneralRe: error: expected identifier before string constant Pin
Vaclav_14-Jan-19 3:33
Vaclav_14-Jan-19 3:33 
GeneralRe: error: expected identifier before string constant Pin
Graham Breach14-Jan-19 4:33
Graham Breach14-Jan-19 4:33 
AnswerRe: error: expected identifier before string constant Pin
Stefan_Lang16-Jan-19 22:38
Stefan_Lang16-Jan-19 22:38 
GeneralRe: error: expected identifier before string constant Pin
Vaclav_19-Jan-19 18:31
Vaclav_19-Jan-19 18:31 
Question(C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 3:27
HS_C_Student11-Jan-19 3:27 
Suppose I have a function that takes an array pointer and an index declared as type unsigned int and the function will access the array at that index.

AFAIK "Index < 0" should never evaluate to be true regardless of what the caller passed because the unsigned type will control how the bits at the address of Index are interpreted and there will be no interpretation of a bit as an indication of sign.

So implicitly an unsigned int is not capable of an access violation Below / before an array's first element given the pointer arithmetic. All that being said it does not seem at all necessary to test it.

There are other reasons to use the test however.
#1 it may improve readability as someone who reads the code sees that a full boundary check is being done
#2 it may make the code symmetric with other code that tests upper and lower boundaries of any other type of variable
#3 if the function is later modified and the type is accidentally changed to int (for example the types are changed when porting to a different machine architecture) or the code is copy and pasted to a different context where Index is not unsigned, omitting the bounds check introduces the possibility of an access violation
#4 I think it's possible, perhaps likely, that an intelligent compiler will optimize it out completely, even if it didn't we're not talking about a grave loss of efficiency.

So although I feel stupid writing what seems to be redundant and unnecessary code and concern myself with conciseness, for a robust application it seems worthwhile.
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
CPallini11-Jan-19 5:25
mveCPallini11-Jan-19 5:25 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 14:29
Bram van Kampen12-Jan-19 14:29 
SuggestionRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
David Crow11-Jan-19 5:40
David Crow11-Jan-19 5:40 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 7:17
HS_C_Student11-Jan-19 7:17 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
David Crow11-Jan-19 7:33
David Crow11-Jan-19 7:33 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Joe Woodbury11-Jan-19 11:54
professionalJoe Woodbury11-Jan-19 11:54 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 12:18
HS_C_Student11-Jan-19 12:18 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
leon de boer12-Jan-19 3:36
leon de boer12-Jan-19 3:36 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 4:59
HS_C_Student12-Jan-19 4:59 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 14:01
Bram 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
HS_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
mveRichard 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
HS_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
mveRichard MacCutchan13-Jan-19 22:12 
AnswerDemonstrating the relevant case and potential best practice Pin
HS_C_Student13-Jan-19 6:46
HS_C_Student13-Jan-19 6:46 
GeneralRe: Demonstrating the relevant case and potential best practice Pin
Richard MacCutchan13-Jan-19 22:15
mveRichard 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
Stefan_Lang16-Jan-19 23:13 

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.