结构体内存对齐
时间:2022-07-24
本文章向大家介绍结构体内存对齐,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <stdio.h>
#include <string.h>
#include <malloc.h>
/* So, when you are working with image headers, binary headers, and network packets, and are trying to access the
TCP/ IP header, structure padding has to be avoided. */
int main(int argc, char* argv[])
{
struct gif_hdr
{
char signature[3];
char version[3];
int width;
int height;
char colormap;
char bgcolor;
char ratio;
};
3+3+2+4+4+1+1+1+1 = 20
struct gif_hdr v1;
struct gif_hdr *dsptr;
printf("Size of structure data = %dn", sizeof(struct gif_hdr));
dsptr = (struct gif_hdr*)malloc(sizeof(struct gif_hdr));
printf("&(dsptr->signature[0]) = %pn", &(dsptr->signature[0]));
printf("&(dsptr->version[0]) = %pn", &(dsptr->version[0]));
printf("&(dsptr->width) = %pn", &(dsptr->width));
printf("&&(dsptr->height) = %pn", &(dsptr->height));
printf("&(dsptr->colormap) = %pn", &(dsptr->colormap));
printf("&(dsptr->bgcolor) = %pn", &(dsptr->bgcolor));
printf("&(dsptr->ratio) = %pnn", &(dsptr->ratio));
printf("Offset of signature = %dn", &(dsptr->signature[0]) - &(dsptr->signature[0]) );
printf("Offset of version = %dn", &(dsptr->version[0]) - &(dsptr->signature[0]) );
printf("Offset of width = %dn", (char*)&(dsptr->width) - &(dsptr->signature[0]));
printf("Offset of height = %dn", (char*)&(dsptr->height) - &(dsptr->signature[0]));
printf("Offset of colormap = %dn", &(dsptr->colormap) - &(dsptr->signature[0]));
printf("Offset of bgcolor = %dn",&(dsptr->bgcolor) - &(dsptr->signature[0]));
printf("Offset of ratio = %dn", &(dsptr->ratio) - &(dsptr->signature[0]));
return 0;
}
# struct.exe
Size of structure data = 20
&(dsptr->signature[0]) = 00491878
&(dsptr->version[0]) = 0049187B
&(dsptr->width) = 00491880
&(dsptr->height) = 00491884
&(dsptr->colormap) = 00491888
&(dsptr->bgcolor) = 00491889
&(dsptr->ratio) = 0049188A
Offset of signature = 0
Offset of version = 3
Offset of width = 8
Offset of height = 12
Offset of colormap = 16
Offset of bgcolor = 17
Offset of ratio = 18
- 列表(List) 的增删改查及其他方法
- Python之递归函数
- RFID Hacking②:PM3入门指南
- HDU 1575 Tr A(矩阵快速幂)
- Python之内置函数
- Python中的logger和handler到底是个什么鬼
- 1570. [POJ3461]乌力波
- biztalk rosettanet 自定义 pip code
- Python之线程
- 3555: [Ctsc2014]企鹅QQ
- 【实战】RFID Hacking(1):看我如何突破门禁潜入FreeBuf大本营
- P2885 [USACO07NOV]电话线Telephone Wire
- 实战-Fluxion与wifi热点伪造、钓鱼、中间人攻击、wifi破解
- 【下载】PyTorch实现的神经网络翻译框架——机器翻译工具包 nmtpytorch
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ROS2机器人笔记20-07-24
- Postgresql 渗透利用总结
- Spring 基于注解(annotation)的配置之@Required注解
- 由一个系统激活工具引起的一次简单测试
- Golang channel 快速入门
- 潘石屹用Python解决100个问题 | 素数
- Spring 自动装配模式之构造函数装配方式
- 安全狗 {safedog} 最新版注入bypass
- C语言定时关机小程序
- 深入k8s:Pod对象中重要概念及用法
- Golang语言排序的几种方式
- 性能分析(1)- Java 进程导致 CPU 使用率升高,问题怎么定位?
- 安全服务之安全基线及加固(三)Apache篇
- 使用docsify来管理文献
- Cypress系列(41)- Cypress 的测试报告