Click here to Skip to main content
14,666,484 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: error: expected identifier before string constant Pin
Graham Breach14-Jan-19 4:33
MemberGraham Breach14-Jan-19 4:33 
AnswerRe: error: expected identifier before string constant Pin
Stefan_Lang16-Jan-19 22:38
mveStefan_Lang16-Jan-19 22:38 
GeneralRe: error: expected identifier before string constant Pin
Vaclav_19-Jan-19 18:31
MemberVaclav_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
MemberHS_C_Student11-Jan-19 3:27 
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
MemberBram 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
MemberDavid 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
MemberHS_C_Student11-Jan-19 7:17 
I'm genuinely surprised and my paranoia has been rewarded. I think I've misunderstood something fundamental. Here is a code sample:

#include <stdio.h>

//void print_iterator(int integer_array[], int index)
void print_iterator(int integer_array[], unsigned int index)
{
	unsigned int i;

	if(index < 0)
	{
		printf("Index violation!\n");
		return;
	}
	else
		printf("Passed index validation: %d, as %u\n", index, index);

	for(i = 0; i < index; i++)
	{
//		printf("%d\t", integer_array[i]);

		if(i == 20)
		{
			printf("Loop overrun, undefined behavior\n");
			return;
		}
	}
}

main()
{
	int ints[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
	print_iterator(ints, -1);
}


If the index argument is declared as an unsigned int, it seems it will ALWAYS pass the bounds check when -1 is passed as the parameter, and produce this output:

Quote:
Passed index validation: -1, as 4294967295
Loop overrun, undefined behavior
Press any key to continue . . .


If it is declared as an int (uncomment the first function line, comment out the second one) it will work properly:

Quote:
Index violation!
Press any key to continue . . .


Anyone know why?
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
David Crow11-Jan-19 7:33
MemberDavid 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
MemberHS_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
Memberleon 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
MemberHS_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
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
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
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
mveRichard 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
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
mveStefan_Lang16-Jan-19 23:13 
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
mveRichard MacCutchan10-Jan-19 6:27 
AnswerRe: academic question - usage of class variable Pin
CPallini10-Jan-19 21:28
mveCPallini10-Jan-19 21:28 

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.