zoukankan      html  css  js  c++  java
  • #pragma pack(n)

    #pragma pack(n)

    9.91 #pragma pack(n)

    This pragma aligns members of a structure to the minimum of n and their natural alignment. Packed objects are read and written using unaligned accesses.

    Note

    This pragma is a GNU compiler extension that the ARM compiler supports.

    Syntax

    #pragma pack(n)
    Where:
    n
    is the alignment in bytes, valid alignment values being 1, 2, 4 and 8.

    Default

    The default is #pragma pack(8).

    Errors

    Taking the address of a field in a #pragma packed struct does not yield a __packed pointer, so the compiler does not produce an error if you assign this address to a non-__packed pointer. However, the field might not be properly aligned for its type, and dereferencing such an unaligned pointer results in undefined behavior.

    Examples

    This example demonstrates how pack(2) aligns integer variable b to a 2-byte boundary.
    typedef struct
    { 
        char a;
        int b;
    } S;
    #pragma pack(2)
    typedef struct
    { 
        char a;
        int b;
    } SP;
    S var = { 0x11, 0x44444444 };
    SP pvar = { 0x11, 0x44444444 };
    
    The layout of S is:
    Figure 9-1 Nonpacked structure S

    The layout of SP is:
    Figure 9-2 Packed structure SP

    Note

    In this layout, x denotes one byte of padding.
    SP is a 6-byte structure. There is no padding after b.
  • 相关阅读:
    SpringCloud--gateway网关基础入门
    SpringCloud--gateway网关基本介绍
    编程的命名规则
    [java基础知识]java安装步骤
    [java基础]java跨平台的基础知识
    [java基础]计算机基础知识
    外观模式
    适配器模式
    设计模式UML类图基础
    拾取坐标
  • 原文地址:https://www.cnblogs.com/qiyuexin/p/12784269.html
Copyright © 2011-2022 走看看