静态变量获取技术 [英] Static Variable Fetching Techique
问题描述
要从结构/类中提取普通成员变量的值,编译器通过获取元素与结构/类的起始地址的偏移量,然后将基地址添加到偏移量中,来到达变量. br/>
编译器如何获取静态变量?
示例代码:
在下面显示的两种结构中,编译器将如何获取变量?
To withdraw a normal member variable''s value from a structure/class the complier reaches the variable by getting the offset of the element from the beginning address of the structure/class and then adding the base address to the offset.
How is a static variable fetched by the compiler?
Sample code:
In both the structures shown below how will the compiler fetch the variables?
struct cde
{
char c;
int d;
};
struct xyz
{
static char x;
int y;
static int z;
};
char xyz::x = 'Y';
int xyz::z = 47;
main()
{
cde C;
xyz X;
C.c = 'D';
C.d = 4;
X.y = 7;
printf("%c %d\n",C.c, C.d);
printf("%c %d %f\n",X.x, X.y, X.z);
}
推荐答案
我为您提供了提示.您说了
i have hint for you. You stated
然后将基地址添加到偏移量中.
then adding the base address to the offset.
编译器如何知道基地址在哪里?提示是,它知道静态变量在同一机制下的位置.
How does the compiler know where the base address is? The hint is, it knows where the static variables are by the same mechanism.
这篇关于静态变量获取技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!