全局变量以及全局的函数名在编译时就已经分配地址了,而且这个地址是固定的线性地址,不管程序运行多少次,运行多少个实例,它的地址始终是确定的,而且是唯一的线性地址(线性地址通过分页机制转化成物理地址)
//hello.c
#include<stdio.h>
const char gstr[]="Hello world";
int main()
{
const char str[]="Hello world";
printf("str=%X ",str);
printf("gstr=%X/n",gstr);
return 0;
}
编译 gcc hello.c –o hello
测试脚本hello.sh
#!/bin/bash
total=1000
str_num=0
gstr_num=0
while [ $total -gt 0 ]
do
str1=`./hello | cut -d ' ' -f1`
gstr1=`./hello | cut -d ' ' -f2`
str2=`./hello | cut -d ' ' -f1`
gstr2=`./hello | cut -d ' ' -f2`
if [ $str1 != $str2 ]
then
str_num=$(($str_num + 1))
fi
if [ $gstr1 !
全局变量与局部变量的地址
最新推荐文章于 2024-05-03 12:00:43 发布