[c++11]结构体字节对齐

[c++11]结构体字节对齐,第1张

概述//测试c++中结构体的字节对齐#include <iostream>#include <cmath>using namespace std;void vsVersion();//example 1struct Node1{ char a; int b; double c;};//example 2struct Node2 { char a;
//测试c++中结构体的字节对齐#include <iostream>#include <cmath>using namespace std;voID vsversion();//example 1struct Node1{    char a;    int b;    double c;};//example 2struct Node2 {    char a;    int b;    char c;};//example 3struct Node3{};//example 4struct Node4{    char a;    int b;    static double c;};//example 5struct InsIDe {    int a;    double b;    float c;};struct Node5 {    char e[2];    int f;    short h;    struct InsIDe insIDe;};//example 6struct Node6 {    char a;    int b;    float c;    double d;    int* p;    char* pc;    short e;};int main(){    Node1 n1;    Node2 n2;    Node3 n3;    Node4 n4;    Node5 n5;    Node6 n6;    vsversion();//宏定义:https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?redirectedfrom=MSDN&vIEw=vs-2019#ifdef _WIN64    cout << "--------x64----------------" << endl;    cout << "char:" << sizeof(char) << endl;    cout << "short:" << sizeof(short) << endl;    cout << "int:" << sizeof(int) << endl;    cout << "double:" << sizeof(double) << endl;    cout << "float:" << sizeof(float) << endl;    cout << "char *:" << sizeof(char *) << endl;    cout << "Node1 :" << sizeof(n1) << endl;    cout << "Node2 :" << sizeof(n2) << endl;    cout << "Node3 :" << sizeof(n3) << endl;    cout << "Node4 :" << sizeof(n4) << endl;    cout << "Node5 :" << sizeof(n5) << endl;    cout << "Node6 :" << sizeof(n6) << endl;#else    cout << "--------x86----------------" << endl;    cout << "char:" << sizeof(char) << endl;    cout << "short:" << sizeof(short) << endl;    cout << "int:" << sizeof(int) << endl;    cout << "double:" << sizeof(double) << endl;    cout << "float:" << sizeof(float) << endl;    cout << "char *:" << sizeof(char*) << endl;    cout << "Node1 :" << sizeof(n1) << endl;    cout << "Node2 :" << sizeof(n2) << endl;    cout << "Node3 :" << sizeof(n3) << endl;    cout << "Node4 :" << sizeof(n4) << endl;    cout << "Node5 :" << sizeof(n5) << endl;    cout << "Node6 :" << sizeof(n6) << endl;#endif        cout << "hello world" << endl;    return 0;}//确定IDE版本voID vsversion(){    if (_MSC_VER == 1921 or _MSC_VER == 1922 or _MSC_VER == 1923)    {        cout << "vs2019" << endl;    }    else if (_MSC_VER == 1910 or _MSC_VER == 1911 or _MSC_VER == 1912 or _MSC_VER == 1913 or _MSC_VER == 1914 or _MSC_VER == 1915 or _MSC_VER == 1916)    {        cout << "vs2017" << endl;    }    else    {        cout << "others" << endl;    }}

总结

以上是内存溢出为你收集整理的[c++11]结构体字节对齐全部内容,希望文章能够帮你解决[c++11]结构体字节对齐所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1210867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存