问题描述
可能的重复:
这段 C++ 代码是什么意思?
我正在尝试使用 JNA 将 C 结构映射到 Java.我遇到了一些我从未见过的东西.
I'm trying to map a C structure to Java using JNA. I came across something that I've never seen.
struct
定义如下:
struct op
{
unsigned op_type:9; //---> what does this mean?
unsigned op_opt:1;
unsigned op_latefree:1;
unsigned op_latefreed:1;
unsigned op_attached:1;
unsigned op_spare:3;
U8 op_flags;
U8 op_private;
};
您可以看到定义了一些变量,例如 unsigned op_attached:1
,但我不确定这意味着什么.这会影响要为此特定变量分配的字节数吗?
You can see some variable being defined like unsigned op_attached:1
and I'm unsure what would that mean. Would that effect the number of bytes to be allocated for this particular variable?
推荐答案
此构造指定每个字段的长度(以位为单位).
This construct specifies the length in bits for each field.
这样做的好处是你可以控制sizeof(op)
,如果你小心的话.结构的大小将是内部字段大小的总和.
The advantage of this is that you can control the sizeof(op)
, if you're careful. the size of the structure will be the sum of the sizes of the fields inside.
在您的情况下,op 的大小为 32 位(即 sizeof(op)
为 4).
In your case, size of op is 32 bits (that is, sizeof(op)
is 4).
对于每组未签名的 xxx:yy,大小总是向上取整到下一个 8 的倍数;构造.
The size always gets rounded up to the next multiple of 8 for every group of unsigned xxx:yy; construct.
这意味着:
struct A
{
unsigned a: 4; // 4 bits
unsigned b: 4; // +4 bits, same group, (4+4 is rounded to 8 bits)
unsigned char c; // +8 bits
};
// sizeof(A) = 2 (16 bits)
struct B
{
unsigned a: 4; // 4 bits
unsigned b: 1; // +1 bit, same group, (4+1 is rounded to 8 bits)
unsigned char c; // +8 bits
unsigned d: 7; // + 7 bits
};
// sizeof(B) = 3 (4+1 rounded to 8 + 8 + 7 = 23, rounded to 24)
我不确定我是否记得正确,但我想我没记错.
I'm not sure I remember this correctly, but I think I got it right.
这篇关于结构或联合中的“无符号临时:3"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!