结构声明占用内存吗? [英] does structure declaration occupies memory?

查看:56
本文介绍了结构声明占用内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct books
{
    char name[100];
    float price;
    int pages;
};

声明一个结构体在不创建结构体object的情况下,该结构体是否会为其DATA MEMBERS占用内存空间?

Declaring a structure Without creating an object of a structure, does the structure occupies space in memory for its DATA MEMBERS?

推荐答案

结构的定义通常不是 C 中二进制文件的一部分.它只存在于您的源代码中.

The definition of a struct is normally not part of the binary in C. It only exists in your source code.

当编译器看到对您的结构的引用时(通常用于为此结构的实例分配或释放空间,通过对象变量访问其字段等),它会参考您的结构定义以了解正确的内容数字是针对该数据类型的(它主要是想计算数据类型大小和字段偏移量).

When the compiler sees references to your struct (typically for allocation or deallocation of space for an instance of this struct, access to its fields through an object variable, etc), it consults your struct definition in order to understand what the correct numbers are for that data type (it mainly wants to calculate data type sizes and field offsets).

当所有这些都完成后,结构体定义本身就会被遗忘,只有数字被保留在程序中,无论它们在哪里被实际使用.

When all this is done, the struct definition itself is forgotten and only the numbers are kept in the program, wherever they were actually used.

因此,如果您根本不引用您的结构,则不应存在它的任何痕迹.

Therefore, if you don't reference your struct at all, then no traces of it should be present.

这篇关于结构声明占用内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆