C基本知识
2023-03-15 22:49:16 时间
1 C基本数据类型
C基本的数据类型说明:
![数据类型](https://img2022.cnblogs.com/blog/1078885/202207/1078885-20220731213807059-884133572.png)
2 字节序
![字节序_1](https://img2022.cnblogs.com/blog/1078885/202207/1078885-20220731213811520-1956586447.png)
测试代码:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for (i = 0; i < len; i++) {
printf(" %.2x", start[i]);
}
printf("
");
}
int main(void)
{
int x = 0x01234567;
show_bytes((byte_pointer)&x, sizeof(x));
return 0;
}
编译运行测试:
$ gcc endian.c
$ ./a.out
67 45 23 01
可以看出,当前我运行的机器是小端序。
3 结构体
参考:C语言技术天花板【结构体】一节课掌握_哔哩哔哩_bilibili
结构体中成员排列的规则:
-
结构体成员的内部偏移量(内部地址),要被这个成员的数据类型的大小整除。
struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte][0x00] */ short s; /* [Byte][Byte] */ };
/* 结构体的内存大小 */ sizeof(struct test) = 8; /* 结构体中的内存分布,可看出成员c后面填充了1Byte的0x00 */ struct test test1 = {0}; test1.a = 0x12345678; test1.c = 0xbb; test1.s = 0xccdd; (gdb) x /8bx &test1 0x7fffffffe110: 0x78 0x56 0x34 0x12 0xbb 0x00 0xdd 0xcc
-
整个结构体的大小,必须是最大成员的size的整数倍,否则就需要在末尾填充空白字节。
struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte] */ };
/* 结构体的内存大小 */ sizeof(struct test) = 8; /* 结构体中的内存分布,可看出成员c后面填充了3Byte的0x00 */ (gdb) x /8bx &test1 0x7fffffffe110: 0x78 0x56 0x34 0x12 0xbb 0x00 0x00 0x00
-
对于结构体中的结构体,按照结构体展开之后的内存对齐来处理。
struct test1 { int a; /* [Byte][Byte][Byte][Byte] */ short s; /* [Byte][Byte][0x00][0x00] */ }; struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte][0x00][0x00][0x00]*/ struct test1 s; };
/* 结构体的内存大小为16,根据规则1,成员c后面补充3Bytes的0x0,根据规则2,s.s成员后面补充2Bytes的0x0 */ sizeof(struct test) = 16; /* 结构体中的内存分布 */ struct test test1 = {0}; test1.a = 0x12345678; test1.c = 0xff; test1.s.a = 0xaabbccdd; test1.s.s = 0x9988; (gdb) x /16bx &test1 0x7fffffffe100: 0x78 0x56 0x34 0x12 0xff 0x00 0x00 0x00 0x7fffffffe108: 0xdd 0xcc 0xbb 0xaa 0x88 0x99 0x00 0x00
-
认为指定特殊的对齐规则,使用
#pragma pack(n)
指定每个成员的其实地址,按照n来对齐,覆盖第一条规则。如果整个n比第一条规则对齐还要大,那么就取小的。#pragma pack(2) struct test1 { int a; /* [Byte][Byte][Byte][Byte] */ short s; /* [Byte][Byte] */ }; struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte][0x00] */ struct test1 s; };
/* 结构体的内存大小为12,根据规则4,成员c后面补充1Bytes的0x0 */ sizeof(struct test) = 12; /* 结构体中的内存分布 */ struct test test1 = {0}; test1.a = 0x12345678; test1.c = 0xff; test1.s.a = 0xaabbccdd; test1.s.s = 0x9988; (gdb) x /12bx &test1 0x7fffffffe110: 0x78 0x56 0x34 0x12 0xff 0x00 0xdd 0xcc 0x7fffffffe118: 0xbb 0xaa 0x88 0x99
#pragma pack(8) struct test1 { int a; /* [Byte][Byte][Byte][Byte] */ short s; /* [Byte][Byte] */ }; struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte][0x00] */ struct test1 s; }; /* 规则1生效 */ sizeof(struct test) = 16;
-
不使用任何对齐,直接存放数据的方式,使用
#pragma pack(1)
。#pragma pack(1) struct test1 { int a; /* [Byte][Byte][Byte][Byte] */ short s; /* [Byte][Byte] */ }; struct test { int a; /* [Byte][Byte][Byte][Byte] */ char c; /* [Byte] */ struct test1 s; };
/* 结构体的内存大小为11,各个成员之间不会填充0x0 */ sizeof(struct test) = 11; /* 结构体中的内存分布 */ struct test test1 = {0}; test1.a = 0x12345678; test1.c = 0xff; test1.s.a = 0xaabbccdd; test1.s.s = 0x9988; (gdb) x /11bx &test1 0x7fffffffe110: 0x78 0x56 0x34 0x12 0xff 0xdd 0xcc 0xbb 0x7fffffffe118: 0xaa 0x88 0x99
相关文章
- 从本体论开始说起——运营商关系图谱的构建及应用
- 如何成为一名数据科学家?
- 从未见过的堂兄杀了人,你的DNA是关键证据
- 20个安全可靠的免费数据源,各领域数据任你挑
- 20个安全可靠的免费数据源,各领域数据任你挑
- 阿里云李飞飞:All in Cloud时代,云原生数据库优势明显
- 基于Hadoop生态系统的一高性能数据存储格式CarbonData(性能篇)
- 大数据告诉你:10年漫威,到底有多少角色
- TigerGraph:实时图数据库助力金融风控升级
- Splunk利用Splunk Connected Experiences和Splunk Business Flow 扩大数据访问
- 大数据开发常见的9种数据分析手段
- 以免在景区看人,我爬了5W条全国景点门票数据...
- 【实战解析】基于HBase的大数据存储在京东的应用场景
- 数据科学家告诉你哪些计算机科学书籍是你应该看的
- Kafka作为大数据的核心技术,你了解多少?
- Spring Boot 整合 Redis 实现缓存操作
- 大数据学习必须掌握的五大核心技术有哪些?
- 基于Antlr在Apache Flink中实现监控规则DSL化的探索实践
- 甲骨文再次被Gartner评为分析型数据管理解决方案魔力象限领导者
- 爬取吴亦凡微博102118条转发数据,扒一扒流量的真假