zoukankan      html  css  js  c++  java
  • C struct __attribute__ ((__packed__))

    __packed__ 主要用于C/C++ 结构体中内存是否对齐
    例如

    struct st_packed {
        int s0;
        uint8_t s1;
        int s2;
    } __attribute__ ((__packed__));
    struct st_align {
        int s0;
        uint8_t s1;
        int s2;
    };
    

    sizeof(st_packed) 9
    sizeof(st_align) 12

    比较适合在申请一块内存时候作为头部结构体
    例如

    struct st_proto {
        uint8_t version;
        uint8_t type;
        int size;
        uint8_t data[];
    } __attribute__ ((__packed__));
    struct st_data {
        int flag;
        int res_code;
    } __attribute__ ((__packed__));
    struct st_com {
        st_proto proto;
        st_data data;
    } __attribute__ ((__packed__));
    

    这样的结构体结构紧密,适合做自定义二进制协议,可以直接用于网络发送(需要注意把int等类型等转为网络字节序)

  • 相关阅读:
    雑談
    safari下无法模拟click()的解决方法 ------转载
    用户操作
    居中
    replace 重写
    JS数组添加元素的三种方式
    Asp.Net Core 包
    CSS3 动画 可以参考
    CSS3 动画
    完全居中
  • 原文地址:https://www.cnblogs.com/stdpain/p/12435365.html
Copyright © 2011-2022 走看看