惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - 鬼谷子com

2026年国内主流AI Coding Plan套餐全对比|开发者避坑指南 图解 | 你管这破玩意儿叫TCP?(转载) C++ static_cast、dynamic_cast、const_cast和reinterpret_cast(四种类型转换运算符) Qt内部的d指针和q指针手把手教你实现 C++中虚函数、虚继承内存模型 c++11新特性实战(二):智能指针 windows下使用mingw和msvc静态编译Qt5.15.xx Qt moc元对象编译器的原理和场景(反射) c++11新特性实战 (一):多线程操作 抽象工厂模式(c++实现) 迭代器模式(c++实现) 中介者模式(c++实现) 享元模式(c++实现) 代理模式(c++实现) 状态模式(c++实现) - 鬼谷子com 建造者模式(c++实现) 职责链模式(c++实现) 命令模式(c++实现) 模板方法模式(c++实现)
c++结构体内存对齐
鬼谷子com · 2020-12-09 · via 博客园 - 鬼谷子com

c++结构体内存对齐

基本概念:

各成员变量存放的起始地址相对于结构的起始地址的偏移量必须为该变量的类型所占用的字节数的倍数, 各成员变量在存放的时候根据在结构中出现的顺序依次申请空间 同时按照上面的对齐方式调整位置。 空缺的字节自动填充, 同时为了确保结构的大小为结构的字节边界数(即该结构中占用最大的空间的类型的字节数)的倍数,所以在为最后一个成员变量申请空间后 还会根据需要自动填充空缺的字节;

举例说明:

#include <iostream>

using namespace std;

#pragma pack(8)
struct Test1
{
    char a; //0 - 1
    short b;//2 - 3
    int c;// 4 - 7
    float d;//8 - 11
    double e;//12 - 23
};

#pragma pack(4)
struct Test2
{
    char a; //0 - 1
    short b;//2 - 3
    int c;// 4 - 7
    float d;//8 - 11
    double e;//12 - 19
};

#pragma pack(8)
struct Test3
{
    double e;//0-7
    float d;//8-13
    short b;//14-15
    int c;// 16-19
    char a; //20-23
};

int main()
{
    std::cout << "size test1= " << sizeof(Test1) << std::endl;
    std::cout << "size test2= " << sizeof(Test2) << std::endl;
    std::cout << "size test3= " << sizeof(Test3) << std::endl;
    return 0;
}