I have written a small program to check the page alignment. This program works fine when I hardcode the value of address variable in program itself, but when I try to take them from commandline using argc and argv values, the output goes haphazard, does the value of uint64_t cannot be recovered from commandline using atoi function..?
normal code, one can see the value of address being hardcoded into program itself.
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 int aligned(uint64_t addr, uint64_t pgsize){
5 return ((uint64_t)addr % pgsize == 0);
6 }
7
8 int main(int argc, char*argv[]){
9 uint32_t i;
10 uint64_t addr, size;
11 addr=0x1900000000;
12 size=0x100000000 ;
13
14 for(i=0;i<7;i++)
15 if(aligned(addr,size>>i)){
16 printf("Aligned to %#lx\n",size>>i);
17 }
18 else
19 printf("Not Aligned to %#lx\n",size>>i);
20 return 0;
21 }
output
[souravhimanshu] ./aligned
Aligned to 0
Aligned to 0x80000000
Aligned to 0x40000000
Aligned to 0x20000000
Aligned to 0x10000000
Aligned to 0x8000000
Aligned to 0x4000000
code with command line input
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 int aligned(uint64_t addr, uint64_t pgsize){
5 return ((uint64_t)addr % pgsize == 0);
6 }
7
8 int main(int argc, char*argv[]){
9 uint32_t i;
10 uint64_t addr, size;
11 if(argc<2){
12 printf("usage ./chkalign <address>\n");
13 exit(-1);
14 }
15 addr=atoi(argv[1]);
16 printf("%#lx",addr);
17 //addr=0x1900000000;
18 size=0x100000000 ;
19
20 for(i=0;i<7;i++)
21 if(aligned(addr,size>>i)){
22 printf("Aligned to %#lx\n",size>>i);
23 }
24 else
25 printf("Not Aligned to %#lx\n",size>>i);
26 return 0;
27 }
output (incorrect)
[sourav@himanshu] ./chkalign 0x924000000
0Aligned to 0
Aligned to 0x80000000
Aligned to 0x40000000
Aligned to 0x20000000
Aligned to 0x10000000
Aligned to 0x8000000
Aligned to 0x4000000
as we can see the value of addr when printed after atoi function shows 0.
kindly advise...
strtol()instead? - Sourav Ghoshatoi, so work that problem, and omit all the uninteresting stuff. - Jens Gustedt