学内存对齐遇到的一个问题?
#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;
}
我这里已经设置内存对齐为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pack的有效参数只能是1、2、4、8、16,不可以是3。你仔细看一下,程序应该没编译通过。
另外你用的是CLion?用的什么编译器?
能贴一下编译输出的提示吗?