The program I wrote takes two numbers and makes a division and a modulo operation.
The code is
#define ADDRESS_SPACE 4294967295
int main (int argc, char *argv[]) {
long int pagesize = atoi(argv[1]), virtaddr = atoi(argv[2]);
if (virtaddr >= ADDRESS_SPACE) {puts("Address is too large"); return 1;}
printf("%lu\n", virtaddr);
printf("%lu\n", ADDRESS_SPACE);
printf("Page = %lu\nOffset = %lu\n", virtaddr/pagesize, virtaddr%pagesize);
return 0;
}
And doing ./page 1024 9999999999999999999999999999999999999999999999 gives the following output
18446744073709551615
4294967295
Page = 0
Offset = 18446744073709551615
If virtaddr is bigger than ADDRESS_SPACE, why isn’t the if statement working? I know there’s an overflow, but printing the variables doesn’t show any error and they are still numbers (the maximum value a long int can take).
18446744073709551615is the unsigned version of -1.virtaddris signed but you displayed it as unsigned; of course -1 is going to be less than any valid positive number.