简体   繁体   English

内存中的结构布局数组

[英]Array of structure Layout in Memory

 int a[2];

This in memory actually looks like: 这实际上在内存中看起来像:

 //Assuming int is 2 bytes 

 add=2000, a[0]=124    

 add=2002, a[1]=534 

How does this actually look like in memory 实际在内存中看起来如何

 struct l {

 struct l    * n;

 long int pad[7];

 };          

 struct l container;

I am unable to visualize. 我无法想象。 Please help! 请帮忙!

BTW this is taken from section 3.3.2 of What Every Programmer Should Know About Memory 顺便说一句,这摘自每个程序员应了解的内存的 3.3.2节

The layout of struct l would be as follows. struct l的布局如下。 As the book says it will occupy 32 bytes. 如书所述,它将占用32个字节。

   addr   ref
   -----------------
   2000:  n
   2004:  pad[0]
   2008:  pad[1]
     ...
   2028:  pad[6]

On a 32-bit system struct l* , a pointer to a structure would occupy 4 bytes. 在32位系统struct l* ,指向结构的指针将占用4个字节。 A variable of type long int would occupy the same amount of memory. long int类型的变量将占用相同的内存量。

Assuming a pointer in your architecture is 4 bytes and a long int in your architecture is 4 bytes: 假设架构中的指针为4个字节,架构中的long int为4个字节:

struct l {
   struct l    * n;
   long int pad[7];
};

struct l someName;

layout will look like: 布局将如下所示:

add=2000, someName.n
add=2004, someName.pad[0]
add=2008, someName.pad[1]
...
add=2028, someName.pad[6]

this just means every time you allocate memory for struct l, you will need 4 byte (pointer) + 4 byte (let's say long int is 4 byte) * 7. 这仅意味着每次为struct l分配内存时,您将需要4字节(指针)+ 4字节(假设long int是4字节)* 7。

so using your system, it should be: add=2000 *n add=2004 pad[0] add=2008 pad[1] ... 因此使用您的系统应该是:add = 2000 * n add = 2004 pad [0] add = 2008 pad [1] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM