如果我们开始在一些地址,我们如何确定变量占据整个程序的地址吗? [英] If we start at some address, how can we determine the addresses that variables occupy throughout a program?
问题描述
假设内存位置地址100开始,考虑:
Assuming that the memory locations starts at address 100, consider:
int a;
struct{
char b;
struct{
short *c[20];
char d;
}e;
}f;
double g;
char *h;
我知道 A
需要解决100-103,但我无法确定当你有一个结构会发生什么。我知道的起始地址的结构是基于最大字段对齐,和整体结构的尺寸是最大的领域的倍数,但我无法区分两者时,两个结构是如上嵌套。此外,如果我们有一个指针或数字在数组作为短* C [20]
我们如何确定通过该声明采取的内存?如果有人能在每行,我将不胜AP preciate它解释地址布局。越是这样,我就AP preciate为什么内存是这样分配的解释。
I know a
takes address 100-103, but I'm having trouble determining what happens when you have a structure. I know the start address for a structure is aligned based on the largest field, and the size of the overall structure is a multiple of the largest field, but I'm having trouble distinguishing between the two when the two structures are nested as above. Also, if we have a pointer, or an array of numbers as in short *c[20]
how do we determine the memory taken by this declaration? If someone can explain the address layout at each line I would greatly appreciate it. More so, I would appreciate an explanation of why the memory is assigned in this way.
感谢您的时间。
推荐答案
有没有真正的规则。它是由编译器。关于你保证全部是乙
地址是电子
以下地址和地址 C
是 D
。并且每个的第一要素结构
的地址是一样的结构
的地址。另一方面,没有保证无论为任何结构以外的元素。编译器可分配 A
,˚F
,先按g
和 ^ h
在它为所欲为的任何方式。
There's no real rule. It's up to the compiler. About all you're guaranteed is that the address of b
is below the address of e
, and the address of c
is below the address of d
. And that the addresses of the first elements of each struct
are the same as the address of the struct
. On the other hand, there are no guarantees whatever for the elements outside of any struct. The compiler may allocate a
, f
, g
and h
in any way it pleases.
这篇关于如果我们开始在一些地址,我们如何确定变量占据整个程序的地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!