结构和位域 [英] Structures and bit fields

查看:67
本文介绍了结构和位域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct abc

{

int d;

int m;

int y;

}日期;

如果我使用位字段并假设33位同时使用date.d和date.m

和date.m取16比用于存储1位date.d和date.m的新32位内存块而不是编译器将分配剩余内存到date.y



< b>我尝试了什么:



i不知道wheter编译器是否分配内存

struct abc
{
int d;
int m;
int y;
}date;
if i use bit fields and suppose 33 bits are taking both date.d and date.m together
and date.m taking 16 bit than a new block of 32 bit memory created for storing 1 bit of date.d and date.m than did compiler will allocates remaing memory to date.y

What I have tried:

i have no idea wheter compiler allocates memory or not

推荐答案

在结构中使用位字段时,可以指定每个字段的宽度,但编译器可以根据需要随意将它们排列在存储单元中。不同的编译器会有不同的行为,但大多数会尝试合理,因此机器寄存器和I / O设备寄存器等内容或多或少地直观地映射。
When you use bit fields in a struct, you get to specify the width of each field, but the compiler is free to arrange them in "storage units" as it sees fit. Different compilers will behave differently, but most will try to be "reasonable", so that things like machine registers and I/O device registers are mapped more or less intuitively.


这篇关于结构和位域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆