简体   繁体   English

如何确定结构的内存布局?

[英]How do I determine the memory layout of a structure?

Suppose I have the following structure (in C): 假设我有以下结构(在C中):

struct my_struct {
  int foo;
  float bar;
  char *baz;
};

If I now have a variable, say 如果我现在有一个变量,比方说

struct my_struct a_struct;

How can I find out how the fields of that structure are going to be laid out in memory? 我怎样才能知道该结构的字段将如何在内存中布局? In other words, I need to know what the address of a_struct.foo , of a_struct.bar and a_struct.baz are going to be. 换句话说,我需要知道a_struct.fooa_struct.bara_struct.baz的地址是什么。 And I cannot do that programatically, because I am actually cross-compiling to another platform. 我不能以编程方式做到这一点,因为我实际上正在交叉编译到另一个平台。

CLARIFICATION 澄清

Thanks the answers so far, but I cannot do this programatically (ie with the offsetof macro, or with a small test program) because I am cross-compiling and I need to know how the fields are going to be aligned on the target platform. 感谢到目前为止的答案,但我不能以编程方式(即使用offsetof宏,或使用小型测试程序)执行此操作,因为我正在交叉编译,我需要知道字段将如何在目标平台上对齐。 I know this is implementation-dependent, that's the whole point of my question. 我知道这是依赖于实现的,这是我的问题的全部要点。 I am using GCC to compile, targeting an ARM architecture. 我正在使用GCC进行编译,目标是ARM架构。

What I need in the end is to be able to dump the memory from the target platform and parse it with other tools, such as Python's struct library. 我最终需要的是能够从目标平台转储内存并使用其他工具解析它,例如Python的struct库。 But for that I need to know how the fields were laid out. 但为此,我需要知道这些领域是如何布局的。

In general, this is implementation specific. 通常,这是特定于实现的。 It depends on things like the compiler, compiler settings, the platform you are compiling on, word-size, etc. Here's a previous SO thread on the topic: C struct memory layout? 这取决于编译器,编译器设置,您正在编译的平台,字大小等等。这里是关于主题的前一个SO线程: C struct memory layout?

If you are cross-compiling, I'd imagine the specific layout will be different depending on which platform you compile for. 如果您是交叉编译,我想具体的布局将根据您编译的平台而有所不同。 I'd consult references for your compiler and platform. 我会参考您的编译器和平台的参考资料。

在矮人包中有一个名为pahole(Poke-A-Hole)的程序,它将生成一个报告,显示程序的结构以及显示填充位置和大小的标记。

One hacky way to see the memory view of what's inside it would be to cast a struct pointer to a char pointer, then print out all the chars, something like: 一种hacky方式来查看内部内容的内存视图是将结构指针强制转换为char指针,然后打印出所有字符,例如:

struct my_struct s;
s.foo = MAX_INT;
s.bar = 1.0;
s.baz = "hello";

for (int i = 0; i < sizeof(s); i++) {
  char *c = ((char*)&s) + i;
  printf("byte %d: 0x%02x\n", i, *c);
}

That doesn't explicitly show you the boundaries, you'd have to infer that from the dump. 这没有明确地向您显示边界,您必须从转储中推断出。

Comments made by others about packing still apply; 其他人对包装的评论仍然适用; you'll also want to use explicitly sized types like uint32 instead of unsigned int 您还需要使用显式大小的类型,如uint32而不是unsigned int

I think you have two options. 我认为你有两种选择。

The first one is to use __attribute__((packed)) after the struct declaration. 第一个是在struct声明之后使用__attribute__((packed)) This will ensure that each member will be allocated exactly the amount of memory that its type requires. 这将确保为每个成员分配其类型所需的内存量。

The other one is to examine your structure and use the alignment rules (n-byte basic type variable has to be n-byte aligned) to figure out the layout. 另一个是检查你的结构并使用对齐规则(n字节基本类型变量必须是n字节对齐)来计算布局。

In your example, in either case each member variable will take 4 bytes and the structure will occupe 12 bytes in memory. 在您的示例中,在任何一种情况下,每个成员变量将占用4个字节,结构将在内存中占用12个字节。

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

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