Structures, unions, enumerations, and bitfields in ARM C and C++
Structures, unions, enumerations, and bitfields in ARM C and C++
| Non-Confidential | ARM DUI0375E | |||
|
||||
| Home > C and C++ Implementation Details > Structures, unions, enumerations, and bitfields in ARM C and C++ | ||||
10.4 Structures, unions, enumerations, and bitfields in ARM C and C++
Describes the implementation of the structured data types union, enum, and struct. It also discusses structure padding and bitfield implementation.
Unions
union is accessed using a member of a different type, the resulting value can be predicted from the representation of the original type. No error is given.Enumerations
enum is implemented in the smallest integral type that contains the range of the enum.--enum_is_int, if an enum contains only positive enumerator values, the storage type of the enum is the first unsigned type from the following list, according to the range of the enumerators in the enum. In other modes, and in cases where an enum contains any negative enumerator values, the storage type of the enum is the first of the following, according to the range of the enumerators in the enum:unsigned charif not using--enum_is_intsigned charif not using--enum_is_intunsigned shortif not using--enum_is_intsigned shortif not using--enum_is_intsigned intunsigned intexcept C with--strictsigned long longexcept C with--strictunsigned long longexcept C with--strict.
Note
In ARM Compiler 4.1 and later, the storage type of the enum being the first unsigned type from the list applies irrespective of mode.
enum in this way can reduce data size. The command-line option --enum_is_int forces the underlying type of enum to at least as wide as int.Note
--enum_is_int option, and that share interfaces or data structures.ints. That is, they must be in the range -2147483648 to +2147483647, inclusive. A warning is issued for out-of-range enumerator values:#66: enumeration value is out of "int" range
unsigned int, long long, or unsigned long long.armcc --diag_error=66 ...
Structures
- all C structures
- all C++ structures and classes not using virtual functions or base classes.
sizeof() function returns the size of the structure including padding.
according to how the structure is defined:
- Structures that are defined as
staticorexternare padded with zeros. - Structures on the stack or heap, such as those defined with
malloc()orauto, are
padded with whatever is previously stored in those memory locations. You cannot
usememcmp()to compare padded structures defined in this
way.
--remarks option to view the messages that aregenerated when the compiler inserts padding in a
struct.struct
{
int x;
} X = { };
--cpp and--c90 options, an error is generated.Packed structures
__packed qualifier. Alternatively, you can use #pragma pack(n) to make sure that any structures with unaligned data are packed. There is no command-line option to change the default packing of structures.Bitfields
- Little-endian
-
Lowest addressed means least significant.
- Big-endian
-
Lowest addressed means most significant.
Note
int, signed int, and unsigned int. For non-int bitfields, the compiler displays an error.signed or unsigned qualifiers, is treated as unsigned. For example, int x:10 allocates an unsigned integer of 10 bits.struct X
{
int x:10;
int y:20;
};
x. At the second declaration, the compiler finds the existing integer container with a sufficient number of unallocated bits, and allocates y in the same container as x.z overflows the container if an additional bitfield is declared for the structure:struct X
{
int x:10;
int y:20;
int z:5;
};
z.struct X
{
int x:10;
char y:2;
};
x. These 10 bits occupy the first byte and two bits of the second byte of the integer container. At the second declaration, the compiler checks for a container of type char. There is no suitable container, so the compiler allocates a new correctly aligned char container.char is 1, the compiler searches for the first byte that contains a sufficient number of unallocated bits to completely contain the bitfield. In the example structure, the second byte of the int container has two bits allocated to x, and six bits unallocated. The compiler allocates a char container starting at the second byte of the previous int container, skips the first two bits that are allocated to x, and allocates two bits to y.y is declared char y:8, the compiler pads the second byte and allocates a new char container to the third byte, because the bitfield cannot overflow its container. The following figure shows the bitfield allocation for the following example structure:struct X
{
int x:10;
char y:8;
};
Note
container types. For example, adding an
int bitfield to the example structure gives:struct X
{
int x:10;
char y:8;
int z:5;
}
int container starting at the same location as the int x:10 container and allocates a byte-aligned char and 5-bit bitfield, as follows:
bitfield of size zero. A bitfield of zero size fills the container up to the end
if the container is not empty. A subsequent bitfield declaration starts a new
empty container.
Note
container with unspecified values when a bitfield is written. This does not
affect normal usage of bitfields.
Bitfields in packed structures
packed structures, have an alignment of 1. Therefore the maximum bit padding
inserted to align a packed bitfield container is 7 bits.
8*sizeof(container-type)-1 bits.ensure arrays of the structure will have their elements correctly aligned.
the bitfield that declared it. Non-packed bitfield containers are the size of
their type.
struct A { int z:17; }; // sizeof(A) = 4, alignment = 4
struct A { __packed int z:17; }; // sizeof(A) = 3, alignment = 1
__packed struct A { int z:17; }; // sizeof(A) = 3, alignment = 1
struct A { char y:1; int z:31; }; // sizeof(A) = 4, alignment = 4
struct A { char y:1; __packed int z:31; }; // sizeof(A) = 4, alignment = 1
__packed struct A { char y:1; int z:31; }; // sizeof(A) = 4, alignment = 1
struct A { char y:1; int z:32; }; // sizeof(A) = 8, alignment = 4
struct A { char y:1; __packed int z:32; }; // sizeof(A) = 5, alignment = 1
__packed struct A { char y:1; int z:32; }; // sizeof(A) = 5, alignment = 1
struct A { int x; char y:1; int z:31; }; // sizeof(A) = 8, alignment = 4
struct A { int x; char y:1; __packed int z:31; }; // sizeof(A) = 8, alignment = 4
__packed struct A { int x; char y:1; int z:31; }; // sizeof(A) = 8, alignment = 1
struct A { int x; char y:1; int z:32; }; // sizeof(A) = 12, alignment = 4 [1]
struct A { int x; char y:1; __packed int z:32; }; // sizeof(A) = 12, alignment = 4 [2]
__packed struct A { int x; char y:1; int z:32; }; // sizeof(A) = 9, alignment = 1
struct example1
{
int a : 8; /* 4-byte container at offset 0 */
__packed int b : 8; /* 1-byte container at offset 1 */
__packed int c : 24; /* 3-byte container at offset 2 */
}; /* Total size 8 (3 bytes tail padding) */;
struct example2
{
__packed int a : 8; /* 1-byte container at offset 0 */
__packed int b : 8; /* 1-byte container at offset 1 */
int c : 8; /* 4-byte container at offset 0 */
}; /* Total size 4 (No tail padding) */
struct example3
{
int a : 8; /* 4-byte container at offset 0 */
__packed int b : 32; /* 4-byte container at offset 1 */
__packed int c : 32; /* 4-byte container at offset 5 */
int d : 16; /* 4-byte container at offset 8 */
int e : 16; /* 4-byte container at offset 12 */
int f : 16; /* In previous container */
}; /* Total size 16 (No tail padding) */
| Non-Confidential | ARM DUI0375E | |
| Copyright 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. |
| Home > C and C++ Implementation Details > Structures, unions, enumerations, and bitfields in ARM C and C++ |
struct X
{
int x:10;
char y:8;
int z:5;
}
Structures, unions, enumerations, and bitfields in ARM C and C++的更多相关文章
- delphi 保存网页MHT
delphi 保存网页MHT uses ADODB_TLB, CDO_TLB, ComObj,MSHTML;{$R *.dfm}{能把网页如 WWW.QQ.COM保存为一个单文件 .MHT但不能把 ...
- Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册
Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册 ctypes 是一个Python 标准库中的一个库.为了实现调用 DLL,或者共享库等C数据类型而设计.它可以把这 ...
- [转]Keyword Reference (F#)
Visual F# Development Portal http://msdn.microsoft.com/en-us/library/vstudio/ff730280.aspx 本文转自:http ...
- c++学习书籍推荐《The C++ Programming Language第四版》下载
百度云及其他网盘下载地址:点我 作者简介 Bjarne Stroustrup is the designer and original implementer of C++, the author o ...
- Delphi 如何在程序中执行动态生成的Delphi代码
如何在程序中执行动态生成的Delphi代码 经常发现有人提这类问题,或者提问内容最后归结成这种问题 前些阵子有位高手写了一个“执行动态生成的代码”,这是真正的高手,我没那种功力,我只会投机取巧. 这里 ...
- Swift声明参考
一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...
- Core Java Volume I — 1.2. The Java "White Paper" Buzzwords
1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...
- The Swift Programming Language-官方教程精译Swift(4)字符串和字符
String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...
- LibVLC video controls
原文 http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__video.html VLC 3.0.0-git ...
- 《C++程序设计语言(英文第四版)》【PDF】下载
<C++程序设计语言(英文第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382177 内容简介 本书是C++领域经典的参 ...
随机推荐
- python selenium.remote远程连接时间达10分钟
问题: 在机器A搭建了selenium-grid的环境,hub以4444端口号启动,并在4444下注册了子node,端口4445,浏览器配置chrome 使用代码进行远程连接,并创建会话: 传入的se ...
- Vue3 中的 v-bind 指令:你不知道的那些工作原理
前言 v-bind指令想必大家都不陌生,并且都知道他支持各种写法,比如<div v-bind:title="title">.<div :title="t ...
- LLM推理 - Nvidia TensorRT-LLM 与 Triton Inference Server
1. LLM部署-TensorRT-LLM与Triton 随着LLM越来越热门,LLM的推理服务也得到越来越多的关注与探索.在推理框架方面,tensorrt-llm是非常主流的开源框架,在Nvidia ...
- Prometheus监控系统(一)Prometheus介绍
1. Prometheus简介 Prometheus受启发于Google的Brogmon监控系统(类似kubernetes是从Google的Brog系统演变而来).于2012年以开源形式发布,在201 ...
- Linux 使用 Swap分区
Linux 使用 Swap分区 背景 买的云服务器在使用的时候,资源经常不够,因此需要使用swap分区. Swap分区在系统的物理内存不够用的时候,把硬盘内存中的一部分空间释放出来,以供当前运行的程序 ...
- 全国产RK3568J + FPGA的PCIe、FSPI通信实测数据分享!
测试数据汇总 案例 时钟频率 理论速率 测试结果 FSPI通信案例 150MHz 71.53MB/s 读速率:67.452MB/s 写速率:52.638MB/s PCIe通信案例 100MHz 803 ...
- 3562-Linux系统使用手册
- 我跟你说@RefreshScope跟Spring事件监听一起用有坑!
本文记录一下我在 Spring 自带的事件监听类添加 @RefreshScope 注解时遇到的坑,原本这两个东西单独使用是各自安好,但当大家将它们组合在一起时,会发现我们的事件监听代码被重复执行.希望 ...
- CSP2023
坐标HA 背景 NOIP都打完了,CSP-S都没写游记,所以来补一篇(逃-- 正文 Day 7*-1 考前一周停课集训,被whk老师怒斥不务正业,悲QWQ. Day 0 周五得到年级第一zyx的鼓励, ...
- 跟着ChatGPT学习设计模式 - 工厂模式
1. 前言 在工作过程中,越发觉得设计模式的重要性.经常会有人说工作5年的人,大学生随便培训1-2月也能做同样的工作,没错,大学生的确可以做. 但其写的代码,可维护性.可扩展性.添加新功能时方便还是简 ...