69,380
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b ;
int a ;
printf("%x\n",&a); //只输出a的地址,本人机器上为61FE1C
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b ;
int a ;
printf("%x\n",&b); //只输出b的地址,本人机器上也为61FE1C
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b ;
int a ;
printf("%x\t%x\n",&a,&b); //同时输出a,b的地址,本人机器上为61FE18 61FE1C
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b;
int a;
printf("a = %d\n",a); //输出a = 16
printf("b = %d\n",b); //输出b = 0
printf("%X\n",&a); //a的地址为61FE18
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b;
int a;
printf("a = %d\n",a); //输出a = 0
printf("b = %d\n",b); //输出b = 16
printf("%X\n",&b); //b的地址也为61FE18
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b = 10000;
int a ;
printf("%X\n",&b); //b的地址是 61FE1C
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b = 10000;
int a ;
printf("a = %d\n",a);
printf("%X\n",&b); //b的地址是 61FE18
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b ;
int a ;
printf("%x\n",&a); //只输出a的地址,本人机器上为61FE1C
system("pause");
return 0;
}
這段程序中,b只在定義時出現一次。
這就是你要明白的其中一點:編譯器有優化功能,它掃描了整個程序發現b其實可以不要,於是就很可能被省略掉了。
那麽這個時候,不管你輸出的是a還是b,它的地址分配都是一樣的,即屬於該程序的變量空間只有一個整數。#include <stdio.h>
#include <stdlib.h>
int main()
{
int b = 10000;
int a = 10000;
printf("%X\n",&b); //你确定轮流注释这两句打印语句运行出来的结果一样?a,b同时被使用,编译优化不会舍弃任何一个,内存地址肯定不同
//printf("%X\n",&a); //估计你代码哪里有问题,要不就是进程加载时不是重复使用原来的内存(这很正常)
system("pause");
return 0;
}
[/quote]