問題描述
可能的重復(fù):
這段 C++ 代碼是什么意思?
我正在嘗試使用 JNA 將 C 結(jié)構(gòu)映射到 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
,但我不確定這意味著什么.這會(huì)影響要為此特定變量分配的字節(jié)數(shù)嗎?
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?
推薦答案
此構(gòu)造指定每個(gè)字段的長度(以位為單位).
This construct specifies the length in bits for each field.
這樣做的好處是你可以控制sizeof(op)
,如果你小心的話.結(jié)構(gòu)的大小將是內(nèi)部字段大小的總和.
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).
對(duì)于每組未簽名的 xxx:yy,大小總是向上取整到下一個(gè) 8 的倍數(shù);構(gòu)造.
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)
我不確定我是否記得正確,但我想我沒記錯(cuò).
I'm not sure I remember this correctly, but I think I got it right.
這篇關(guān)于結(jié)構(gòu)或聯(lián)合中的“無符號(hào)臨時(shí):3"是什么意思?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!