Click here to Skip to main content
14,365,911 members

C / C++ / MFC

 
GeneralRe: error: expected identifier before string constant Pin
Richard MacCutchan13-Jan-19 23:29
protectorRichard MacCutchan13-Jan-19 23:29 
GeneralRe: error: expected identifier before string constant Pin
Stefan_Lang17-Jan-19 0:28
memberStefan_Lang17-Jan-19 0:28 
GeneralRe: error: expected identifier before string constant Pin
Richard MacCutchan17-Jan-19 0:56
protectorRichard MacCutchan17-Jan-19 0:56 
GeneralRe: error: expected identifier before string constant Pin
Stefan_Lang17-Jan-19 2:30
memberStefan_Lang17-Jan-19 2:30 
AnswerRe: error: expected identifier before string constant Pin
k505413-Jan-19 9:29
memberk505413-Jan-19 9:29 
AnswerRe: error: expected identifier before string constant Pin
Graham Breach14-Jan-19 0:21
memberGraham Breach14-Jan-19 0:21 
GeneralRe: error: expected identifier before string constant Pin
Vaclav_14-Jan-19 4:33
memberVaclav_14-Jan-19 4:33 
GeneralRe: error: expected identifier before string constant Pin
Graham Breach14-Jan-19 5:33
memberGraham Breach14-Jan-19 5:33 
AnswerRe: error: expected identifier before string constant Pin
Stefan_Lang16-Jan-19 23:38
memberStefan_Lang16-Jan-19 23:38 
GeneralRe: error: expected identifier before string constant Pin
Vaclav_19-Jan-19 19:31
memberVaclav_19-Jan-19 19:31 
Question(C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 4:27
memberHS_C_Student11-Jan-19 4:27 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
CPallini11-Jan-19 6:25
mveCPallini11-Jan-19 6:25 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 15:29
memberBram van Kampen12-Jan-19 15:29 
SuggestionRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
David Crow11-Jan-19 6:40
memberDavid Crow11-Jan-19 6:40 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 8:17
memberHS_C_Student11-Jan-19 8:17 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
David Crow11-Jan-19 8:33
memberDavid Crow11-Jan-19 8:33 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Joe Woodbury11-Jan-19 12:54
professionalJoe Woodbury11-Jan-19 12:54 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student11-Jan-19 13:18
memberHS_C_Student11-Jan-19 13:18 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
leon de boer12-Jan-19 4:36
memberleon de boer12-Jan-19 4:36 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 5:59
memberHS_C_Student12-Jan-19 5:59 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 15:01
memberBram van Kampen12-Jan-19 15:01 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 17:50
memberHS_C_Student12-Jan-19 17:50 
Before I got your reply I did a little more investigating into the issue. I was looking at the bitwise representation which I noticed is 2C as you said.

I wrote this small program to test it out. Here is the output:

//Declare an int and unsigned int, initialize to -1, check bit representation:

//int int_neg_1 = -1;
11111111111111111111111111111111

//unsigned int uint_neg_1 = -1;
11111111111111111111111111111111

//Cast the unsigned int to int, check representation:
(int)uint_neg_1;
11111111111111111111111111111111

//Compare printf as signed and unsigned int for all three:

int_neg_1 : %d: -1, %u: 4294967295
uint_neg_1: %d: -1, %u: 4294967295
(int)uint_neg_1: %d: -1, %u: 4294967295

//This is the thing that caught me off guard.  "uint_neg_1 < 0" does not evaluate to true.

int_neg_1       < 0: True
uint_neg_1      < 0: False

//If however, I cast the unsigned int to "int" it seems to force interpretation as a signed number:
(int)uint_neg_1 < 0: True


So it seems that the bitwise storage and representation for signed and unsigned integers is the same.

The typecast to int changed the evaluation of the expression. I'd like to see / understand the difference in the assembly instructions because I think that is probably where the paths diverge.

Quote:
There is hence absolutely no point in testing an unsigned integer for being negative! It has no capacity of being negative!


That's what I believed, but based on my experiment it's not safe to rely on the "unsigned" type as an indication of being >= 0.

If someone invokes my function with "-1" and it is written without the bounds check, the result is an access violation. As a design practice my code should catch the error of the caller.

I have a bad headache, I apologize if I've made mistakes.

See this post for the use case of an unsigned int < 0 check:

Re: (C) Robust code Should an unsigned int array index be tested for "< 0" - C / C++ / MFC Discussion Boards[^]

/* code sample for demonstration and testing purposes */


#include <stdio.h>

#define print_binary(a) \
	for(j = 31; j >= 0; j--) \
		printf("%c", a & (1 << j) ? '1' : '0');	\
	printf("\n\n");											

void main()
{
	int j;

	int int_neg_1 = -1;
	unsigned int uint_neg_1 = -1;

	printf("int int_neg_1 = -1;\n");
	print_binary(int_neg_1);

	printf("unsigned int uint_neg_1 = -1;\n");
	print_binary(uint_neg_1);

	printf("(int)uint_neg_1;\n");
	print_binary((int)uint_neg_1);


	printf("int_neg_1 : %%d: %d, %%u: %u\n", int_neg_1, int_neg_1);
	printf("uint_neg_1: %%d: %d, %%u: %u\n", uint_neg_1, uint_neg_1);
	printf("(int)uint_neg_1: %%d: %d, %%u: %u\n\n", (int)uint_neg_1, (int)uint_neg_1);


	printf("int_neg_1\t< 0: %s\n", int_neg_1 < 0 ? "True" : "False");
	printf("uint_neg_1\t< 0: %s\n\n", uint_neg_1 < 0 ? "True" : "False");

	printf("(int)uint_neg_1\t< 0: %s\n\n", (int)uint_neg_1 < 0 ? "True" : "False");
}

GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan12-Jan-19 23:51
protectorRichard MacCutchan12-Jan-19 23:51 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student13-Jan-19 8:10
memberHS_C_Student13-Jan-19 8:10 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan13-Jan-19 23:12
protectorRichard MacCutchan13-Jan-19 23:12 

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.