提交 5d06581f 编写于 作者: F feilong

add bit filed

上级 99cbca6f
......@@ -6,5 +6,8 @@
"C语言"
],
"children": [],
"export": []
"export": [
"pack01.json",
"pack02.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "pack01.md"
}
\ No newline at end of file
# 结构体位对齐
# 结构体位对齐(1)
假设机器字是8 bytes,下面代码的占据的内存大小是多大?
```c
......@@ -14,13 +14,14 @@ struct Test{
## template
```c
#include <stdio.h>
struct Test{
char *p;
char c;
int x;
};
int main(int argc, char**){
print("%d", sizeof(struct Test))
printf("%d", sizeof(struct Test));
return 0;
}
```
......
{
"type": "code_options",
"author": "幻灰龙",
"source": "pack02.md"
}
\ No newline at end of file
# 结构体位对齐(2)
结构体需要与其最宽的成员为基准对齐,假设机器字是8 bytes,下面代码的占据的内存大小是多大?
```c
struct foo3 {
char *p;
char c;
};
```
## template
```c
#include <stdio.h>
struct foo3 {
char *p;
char c;
};
int main(int argc, char**){
printf("%d", sizeof(struct foo3));
return 0;
}
```
## 答案
```c
16 bytes
```
## 选项
### 选项1
```c
9 bytes
```
### 选项2
```c
12 bytes
```
### 选项3
```c
8 bytes
```
\ No newline at end of file
#include <stdio.h>
struct foo5 {
int flip : 1;
int nybble : 4;
int septet : 7;
};
int main(int argc, char **argv) {
printf("%lu", sizeof(struct foo5));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "bit_field.md"
}
\ No newline at end of file
# 十六进制转换
利用位域,你能声明比字符宽度更小的成员,低至1位。假设机器字是8 bytes,下面的结构体大小是?
```c
#include <stdio.h>
struct foo5 {
int flip : 1;
int nybble : 4;
int septet : 7;
};
int main(int argc, char **argv) {
printf("%lu", sizeof(struct foo5));
return 0;
}
```
## template
```c
#include <stdio.h>
struct foo5 {
int flip : 1;
int nybble : 4;
int septet : 7;
};
int main(int argc, char**){
printf("%lu", sizeof(struct foo5));
return 0;
}
```
## 答案
```c
4
```
## 选项
### 选项1
```c
16
```
### 选项2
```c
32
```
### 选项3
```c
8
```
\ No newline at end of file
......@@ -6,5 +6,7 @@
"C语言"
],
"children": [],
"export": []
"export": [
"bit_filed.json"
]
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册