学内存对齐遇到的一个问题?

发布于 2022-09-05 07:50:55 字数 5155 浏览 31 评论 0

#include "stdafx.h"
#include <stdio.h>
#pragma pack(3)

typedef struct
{
    
    char a;
    char b;
    char c;
    char d;
    char e;
    int x;
    char y;
}Hoge;

int main()
{
    Hoge h;
    printf("%d\n", sizeof(h));
    printf("%x\n", &(h.a));
    printf("%x\n", &(h.b));
    printf("%x\n", &(h.c));
    printf("%x\n", &(h.d));
    printf("%x\n", &(h.e));
    printf("%x\n", &(h.x));
    printf("%x\n", &(h.y));
    return 0;
}

clipboard.png

我这里已经设置内存对齐为 3 字节, 而类中最大对齐字节是int的4字节, 有 n = min(3, 4) = 3;

整体对齐规则:最后整个类的大小应该是n的整数倍

那么最后sizeof(h)理论上应该是3的倍数啊, 但是vs却告诉我是16.

Edit:

1>------ Build started: Project: 两数相除, Configuration: Debug x64 ------
1>stdafx.cpp
1>Source.cpp
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(3): error C2220: warning treated as error - no 'object' file generated
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(3): warning C4086: expected pragma parameter to be '1', '2', '4', '8', or '16'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(13): warning C4820: '<unnamed-tag>': '3' bytes padding added after data member '<unnamed-tag>::e'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(15): warning C4820: '<unnamed-tag>': '3' bytes padding added after data member '<unnamed-tag>::y'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(20): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type '::size_t'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(20): note: consider using '%zd' in the format string
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(21): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(21): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(22): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(22): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(23): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(23): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(24): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(24): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(25): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(25): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(26): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'int *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(26): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'int *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(27): warning C4477: 'printf' : format string '%x' requires an argument of type 'unsigned int', but variadic argument 1 has type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(27): warning C4313: 'printf': '%x' in format string conflicts with argument 1 of type 'char *'
1>c:\users\hp\documents\visual studio 2017\projects\两数相除\两数相除\source.cpp(30): warning C4710: 'int printf(const char *const ,...)': function not inlined
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\stdio.h(946): note: see declaration of 'printf'
1>Done building project "两数相除.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我之前在vs中开了warning all. 现在我查了下vs也的确有了warning, 不过我漏看了, 以后我准备都开treat warnings as errors了.

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

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

发布评论

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

评论(1

人心善变 2022-09-12 07:50:56

pack的有效参数只能是1、2、4、8、16,不可以是3。你仔细看一下,程序应该没编译通过。
另外你用的是CLion?用的什么编译器?
能贴一下编译输出的提示吗?

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