结构体中定义的 char 占用多少字节?

发布于 2024-12-27 09:00:38 字数 815 浏览 0 评论 0原文

当我在结构体中定义字符类型时,它似乎占用了超过1个字节;事实上它似乎需要 4 个字节。

下面是我的程序:

#include <stdio.h>

int main(void)
{
    struct book{
        char name;
        float price;
        int pages;
    };
    struct book b1={'B',130.00,550};
    printf("\nAddress of structure:%u",&b1);
    printf("\nAddress of character name:%u",&b1.name);
    printf("\nAddress of float price:%u",&b1.price);
    printf("\nAddress of integer pages:%u",&b1.pages);
    printf("\n\n");
    return 0;
}

当我运行上面的程序时,我得到以下输出:

    Address of structure:557762432
    Address of character name:557762432
    Address of float price:557762436
    Address of integer pages:557762440

为什么我看到变量“name”和变量“price”的地址之间存在 4 个字节的差异?

运行该程序的系统是运行 Fedora-14 的 x86_64 位架构。

When I define a character type in a structure, it seems to take more than 1 byte; in fact it seems to take 4 bytes.

Below is my program:

#include <stdio.h>

int main(void)
{
    struct book{
        char name;
        float price;
        int pages;
    };
    struct book b1={'B',130.00,550};
    printf("\nAddress of structure:%u",&b1);
    printf("\nAddress of character name:%u",&b1.name);
    printf("\nAddress of float price:%u",&b1.price);
    printf("\nAddress of integer pages:%u",&b1.pages);
    printf("\n\n");
    return 0;
}

When I run the above program, I get the output below:

    Address of structure:557762432
    Address of character name:557762432
    Address of float price:557762436
    Address of integer pages:557762440

Why is it that I see the difference of 4 bytes between address of variable "name" and variable "price"?

The system on which this program is being run is x86_64 bit arch running Fedora-14.

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

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

发布评论

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

评论(4

不疑不惑不回忆 2025-01-03 09:00:38

C 标准允许实现向结构添加额外的填充位,以便根据实现的要求使其与字节边界对齐,从而更快地访问结构。

这称为结构填充

鉴于上述情况,结构的大小可能与各个成员的大小之和不同。您应该始终使用 sizeof 来确定结构的大小。

此外,上面提到的原因是您没有看到结构成员放置在您期望的内存地址处。

The C standard allows implementations to add additional padding bits to structures so as to access the structure faster by making it aligned to byte boundaries as required by that implementation.

This is known as Structure Padding.

Given the above the size of a structure may not be the same as the sum of the sizes of the individual members. You should always use sizeof to determine the size of structure.

Also, the above mentioned is the reason that you do not see the structures members placed at memory addresses you expect them to be at.

眼泪也成诗 2025-01-03 09:00:38

您正在遇到对齐规则。这是一个非常特定于编译器和系统的事情,但默认情况下(也就是说,除非您在代码中使用编译器标志或特殊对齐请求另行指定)x64 Linux 上的 GCC 将结构体的每个字段对齐为其大小的倍数。因此,单字符字节无需特别担心对齐问题。但是,int 或 float 始终放置在 4 字节边界上,而 double 始终放置在 8 字节边界上。这就是你在这里看到的。

正如 Ethan 在上面的评论中指出的那样,某些处理器甚至不会访问未以某种方式对齐的内存对象,而如果未对齐,英特尔处理器访问内存的速度会慢得多。

You are running into alignment rules. This is a very compiler- and system-specific thing, but by default (that is, unless you specify otherwise using compiler flags or special alignment requests in the code) GCC on x64 Linux aligns each field of a struct on a multiple of its size. So, a one-character byte has nothing in particular to worry about for alignment. However, an int or float is always placed on a 4-byte boundary, and a double is always placed on an 8-byte boundary. That's what you are seeing here.

As Ethan notes above in the comments, some processors won't even access memory objects that aren't aligned a certain way, and Intel processors will access memory much more slowly if it isn't aligned.

不再让梦枯萎 2025-01-03 09:00:38

严格来说,char 始终占用 1 个字节,但后面可能有 0 个或更多字节填充。填充量取决于编译器、CPU、结构中 char 后面的类型对齐方式以及任何有效的打包编译指示。如果 N 字节类型必须是 N 字节对齐的(例如在 SPARC 上),则:

struct s16 { char c; double d; };
struct s8  { char c; long l;   };
struct s4  { char c; short s;  };
struct s2  { char c; char b;   };

在 SPARC 上的上述结构中,单个 char 后跟 7、3、1 和 0 字节机器,以及 x86_64 机器上。

Strictly, the char always occupies one byte, but it may be followed by 0 or more bytes padding. How much padding depends on the compiler, and on the CPU, and on the alignment of the type following the char in a structure, and any packing pragmas that are in effect. If an N-byte type must be N-byte aligned, as on SPARC, for example, then:

struct s16 { char c; double d; };
struct s8  { char c; long l;   };
struct s4  { char c; short s;  };
struct s2  { char c; char b;   };

The solitary char is followed by 7, 3, 1 and 0 bytes in the structures above on a SPARC machine, and also on an x86_64 machine.

空宴 2025-01-03 09:00:38

C 中的“char”通常恰好占用 8 位(一个字节)。

但是,结构中的字符在单词、双字甚至四字边界上“对齐”:

http:// en.wikipedia.org/wiki/Data_struct_alignment

有编译器指令可用于强制对齐到 4、2 或 1 字节(这可以避免此行为)。

例如,在 Visual C++ 中,您可以说“#pragma pack(1)”。

A "char" in C typically takes exactly 8 bits (one byte).

However, a char in a struct is "aligned" on a word, dword or even quadword boundary:

http://en.wikipedia.org/wiki/Data_structure_alignment

There are compiler directives available to force alignment to 4, 2 or 1 byte (which circumvents this behavior).

For example, in Visual C++, you can say "#pragma pack(1)".

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