数据结构填充

发布于 2024-11-08 04:19:30 字数 106 浏览 0 评论 0 原文

什么是 C++ 中的数据结构填充以及如何检查填充字节的字节数?

class a { public: int x; int y; int z; };

What is data structure padding in c++ and how do i check the number of bytes padded bytes?

class a { public: int x; int y; int z; };

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

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

发布评论

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

评论(4

☆獨立☆ 2024-11-15 04:19:32

哈哈,只需创建 2 个相同的结构,将其中之一打包
例如

struct foo {
  int  a;
  char b;
  int  c;
} 

struct bar {
  int  a;
  char b;
  int  c;
} __attribute__((__packed__));

sizeof(foo) - sizeof(bar)
会给你填充量。或者你也可以像 Duck 建议的那样手动计算。

Lol just create 2 identical structs, make one of them packed
e.g.

struct foo {
  int  a;
  char b;
  int  c;
} 

struct bar {
  int  a;
  char b;
  int  c;
} __attribute__((__packed__));

sizeof(foo) - sizeof(bar)
will give you the amount of padding. Or you could also calculate manually like Duck suggested.

永言不败 2024-11-15 04:19:31

处理器要求某些类型的数据具有特定的对齐方式。例如,处理器可能要求 int 位于 4 字节边界上。因此,例如,int 可以从内存位置 0x4000 开始,但不能从 0x4001 开始。因此,如果您定义了一个类:

class a
{
public:
    char c;
    int i;
};

编译器必须在 ci 之间插入填充,以便 i 可以在 4 字节边界上开始。

Processors require that certain types of data have particular alignments. For example, a processor might require that an int be on a 4-byte boundary. So, for example, an int could start at memory location 0x4000 but it could not start at 0x4001. So if you defined a class:

class a
{
public:
    char c;
    int i;
};

the compiler would have to insert padding between c and i so that i could start on a 4-byte boundary.

情绪操控生活 2024-11-15 04:19:31
struct A
{
    char c;
    int i;
};

int main(int argc, char *argv[])
{
    A a;

    cout << "sizeof struct = " << sizeof(A) << endl;

    cout << "sizeof items  = " << sizeof(a.c) + sizeof(a.i) << endl;

    return 0;
}
struct A
{
    char c;
    int i;
};

int main(int argc, char *argv[])
{
    A a;

    cout << "sizeof struct = " << sizeof(A) << endl;

    cout << "sizeof items  = " << sizeof(a.c) + sizeof(a.i) << endl;

    return 0;
}
清醇 2024-11-15 04:19:31

出于性能原因进行填充 - 请参阅这篇文章数据结构对齐更多信息。

要查看编​​译器是否填充您的数据结构,您可以编写一个简单的程序:

#include <iostream>

class a {
public:
    int x;
    int y;
    int z;
};

int main()
{
    std::cout << sizeof(a) << std::endl; // print the total size in bytes required per class instance

    a anInstance;
    std::cout << &anInstance.x << std::endl; // print the address of the x member
    std::cout << &anInstance.y << std::endl; // as above but for y
    std::cout << &anInstance.z << std::endl; // etc
}

我添加了公共声明以避免编译器错误 - 它不会影响大小或填充。

编辑:在我的 MacBook Air 上运行它会给出以下输出:
12
0x7fff5fbff650
0x7fff5fbff654
0x7fff5fbff658

这表明在我的机器上总大小是12字节,每个成员相隔4字节。每个 int 为 4 个字节(可以通过 sizeof(int) 确认)。没有填充。

与班上的不同成员一起尝试此操作,例如:

class b {
    public:
        char      w;
        char      x[6];
        int       y;
        long long z;
};

padding is done for performance reasons - see this article Data Structure Alignment for more info.

To see whether the compiler pads your data structure you could write a simple program:

#include <iostream>

class a {
public:
    int x;
    int y;
    int z;
};

int main()
{
    std::cout << sizeof(a) << std::endl; // print the total size in bytes required per class instance

    a anInstance;
    std::cout << &anInstance.x << std::endl; // print the address of the x member
    std::cout << &anInstance.y << std::endl; // as above but for y
    std::cout << &anInstance.z << std::endl; // etc
}

I added the public declaration to avoid compiler errors - It will not affect the size or padding.

Edit: Running this on my macbook air gives the following output:
12
0x7fff5fbff650
0x7fff5fbff654
0x7fff5fbff658

This shows that on my machine the total size is 12 bytes, and each member is 4 bytes apart. The ints are 4 bytes each (which can be confirmed with sizeof(int)). There is no padding.

Try this with different members in your class, for example:

class b {
    public:
        char      w;
        char      x[6];
        int       y;
        long long z;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文