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

查看:194
本文介绍了c++ - 学内存对齐遇到的一个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

#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了.

解决方案

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

这篇关于c++ - 学内存对齐遇到的一个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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