五千年(敝帚自珍)

主题:【贴图】最近看的一些令人蛋疼的好图(四) -- 李根

共:💬29 🌺175
全看分页树展 · 主题 跟帖
家园 写了个短看了下

觉得应该是参数传给printf之前就被做了类型转换成int了(此处应该是把给定参数的最高位重复以达到占满32bit int型),printf里头应该是直接在高位补0?printf这没跟进去看,因为汇编也忘的差不多了,不过重复了好几遍,结果都是稳定的,应该printf内部从int到long int不是越界读的结果。

gcc 4.2.4 x86_64-linux-gnu

int main () {

/*

int i=1;

int j=(1<<31)+255;

char *c_ip = &j;

printf("int len is %d\n",sizeof(i));

printf("long int len is %d\n",sizeof(j));

printf("int pointer len is %d\n",sizeof(&i));

printf("int is %x\n",j);

printf("long int j is %llx\n",j);

printf("long int char pointer is %llx\n",*c_ip);*/

int a=-1;

printf("long int a is %llx\n",a);

int b=255;

printf("long int b is %llx\n",b);

char c=128;

printf("long int c is %llx\n",c);

char d=127;

printf("long int d is %llx\n",d);

char f=128;

printf("long int f is %llx\n",(int)f);

char g=127;

printf("long int f is %llx\n",(int)g);

printf("long int -1 is %llx\n",(long int)-1);

return 0;

}

output:

long int a is ffffffff

long int b is ff

long int c is ffffff80

long int d is 7f

long int f is ffffff80

long int f is 7f

long int -1 is ffffffffffffffff

全看分页树展 · 主题 跟帖


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河