C 中结构体的编译定义是什么样的?

发布于 2024-09-09 03:17:14 字数 534 浏览 2 评论 0原文

可能的重复:
编译后的 C++ 类是什么样子?

大家好,

 bash$cat struct.c
struct test
{
int i;
float f;
};

bash$gcc -c struct.c

目标文件struct.o是elf格式。我想了解这个目标文件包含什么。源代码只是一个结构体的定义。这里没有任何可执行文件,因此文本中应该没有任何内容,而且实际上也没有数据。 那么 struct 的定义到底去哪里了呢?

我尝试使用;

readelf -a struct.o
objdump -s struct.o

但不太明白这一点。

谢谢,

贾格拉蒂

Possible Duplicate:
How does a compiled C++ class look like?

Hi all,

 bash$cat struct.c
struct test
{
int i;
float f;
};

bash$gcc -c struct.c

The object file struct.o is of elf format. I am trying to understand what does this object file contain. The source code is just a definition of a struct. There is nothing executable here so there should be nothing in text, and there is no data really either.
So where does the definition of struct go really?

I tried using;

readelf -a struct.o
objdump -s struct.o

but don't quite understand this.

Thanks,

Jagrati

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星光不落少年眉 2024-09-16 03:17:14

那么struct的定义在哪里呢
真的去吗?

结构体定义通常位于/dev/null。 C 没有任何内省功能,因此运行时不需要结构定义。在编译期间,对结构体字段的调用将转换为数字偏移量,例如。 x->f 将被编译为相当于 *((void*)x + sizeof(int))。这就是为什么每次使用 struct 时都需要包含标头。

So where does the definition of struct
go really?

Struct definition usually goes to /dev/null. C does not have any introspection features, so struct definition is not needed at run time. During compilation, calls to struct fields are converted to numeric offsets, eg. x->f would be compiled to equivalent of *((void*)x + sizeof(int)). That's why you need to include headers every time you use struct.

明媚殇 2024-09-16 03:17:14

没有什么。它不存在。你没有创造任何东西,也没有使用任何东西。

结构体的定义在编译时使用。该定义通常放置在未编译的头文件中。当使用结构时,就会生成一些代码。该定义会影响编译器在该点生成的内容。

除其他原因外,这就是为什么针对一个版本的库进行编译然后在运行时使用另一版本可能会导致程序崩溃的原因。

There is nothing. It does not exist. You have created nothing and used nothing.

The definition of the struct is used at compile time. That definition would normally be placed in a non-compiled header file. It is when a struct is used that some code is generated. The definition affects what the compiler produces at that point.

This, among other reasons, is why compiling against one version of a library and then using another version at runtime can crash programs.

嘿看小鸭子会跑 2024-09-16 03:17:14

结构体不是被编译的,而是被声明的。不过函数会被编译。

structs are not compiled, they are declared. Functions get compiled though.

不喜欢何必死缠烂打 2024-09-16 03:17:14

我不是专家,我实际上无法回答这个问题......但我想到了这一点。
内存就是内存:如果你用1个字节作为整数或字符,它仍然是一个字节。结果仅取决于编译器。
那么,为什么结构体不能相同呢?我的意思是,编译器可能会计算要分配的内存(因为您的计算机可能会分配内存的字数,而不是字节,如果您的结构是 1 字节长,可能会添加 3 字节,从而允许分配 4 字节字),并且那么 struct 只是您访问数据时的“参考”。
我认为实际上没有必要在下面有一些东西:编译器知道在编译时,如果您引用结构体的字段“名称”,它应该将 is 视为长度为 X 的字符数组 。

正如我所说,我不是此类内部的专家,但据我所知,不需要在“真实代码”中转换结构......这只是编译器的一个注释,可以被破坏 编译完成后。

I'm not an expert and I can't actually answer the question... But I thought of this.
Memory is memory: if you use 1 byte as integer or char, it is still one byte. The results depends only on the compiler.
So, why can't be the same for structs? I mean, the compiler probably will calculate the memory to allocate (as your computer probably will allocate WORDS of memory, not bytes, if your struct is 1 byte long, probably 3 bytes will be added allowing the allocation of 4 bytes word), and then struct will just be a "reference" for you when accessing data.
I think that there is no need to actually HAVE something underneath: it's sufficient for the compiler to know that, in compile time, if you refer to field "name" of your struct, it shall treat is as an array of chars of length X.

As I said, I'm not expert in such internals, but as I see it, there is no need for a struct to be converted in "real code"... It's just an annotation for the compiler, which can be destroyed after the compilation is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文