使用offsetof对结构体指针偏移操作

题目来自于COMP20003 Tutorial 2:

Program m ing Challenge 2.2 The technology stack at Hidebound Inc. uses a subset of C w hich doesn't have the '.' or '->'
operators, as the higher-ups heard shortcuts like this w ere useful in an activity called "code golfing" and, misunderstanding w hat
that meant, w anted to discourage all recreational activities on company time. The change improved compile times and required
resources slightly so the developer in charge of that performance w as happy to force the change on the other programmers in
the company. In this challenge, you'll need to replace a piece of code w hich does this using both the simple '->' and '.' operators
w ith a piece of code that instead changes the value in the struct by using value casting and pointer addition instead.
This challenge is intended to highlight that '.' and '->' are merely shortcuts to other dereference operations and though you w ill
eventually find your code is less messy w hen using them, understanding exactly w hat you are doing w ill reduce the number of
errors you make and allow you to examine code closely w hen you have something complicated that isn't doing exactly w hat you
think it should be. You may find reading through the (2nd) extra w orkshop material document on the LMS under the Resources
section is particularly useful for this task.
As a hint, you may find the offsetof macro useful (you can find this using the man pages). For an extra challenge, try only using
the sizeof macro, the address of operator (&) and the dereference operator (*). Note also that for the latter, a process know n as
"packing" may sometimes add holes to structs w hich are unused, though that has been carefully avoided in the struct defined
here.

 1 /*
 2 This program was written by Richard Chad Sparrow
 3 as a test case for AB-testing the hazard management
 4 system.
 5 */
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 #include <stddef.h>
 9 struct hazard {
10     char *description;
11     void *extraData;
12     int extraDataType;
13     int id;
14     char severityClass;
15 };
16 void printHazard(struct hazard *hazard);
17 int main(int argc, char **argv){
18     struct hazard hazard1;
19     struct hazard hazard2;
20     struct hazard *lastHazard;
21     /* Hazard data setup. */
22     hazard1.description = "Brake service required.";
23     hazard1.extraData = NULL;
24     hazard1.extraDataType = 0;
25     hazard1.id = 1;
26     hazard1.severityClass = 'A';
27     hazard2.description = "Unknown issue in fluid level.";
28     hazard2.extraData = NULL;
29     hazard2.extraDataType = 0;
30     hazard2.id = 2;
31     hazard2.severityClass = 'U';
32     lastHazard = &hazard2;
33     printf("Hazards after setup:\n");
34     printHazard(&hazard1);
35     printHazard(&hazard2);
36     /*
37     The brake service hazard has been present for multiple
38     services, so needs to be updated to severity class 'B'.
39     */
40     /* Original: hazard1.severityClass = 'B'; */
41     /* CHANGE THE CODE HERE: */
42     hazard1.severityClass = 'B';
43     printf("Hazard 1 after class B severity update:\n");
44     printHazard(&hazard1);
45     /*
46     The next hazard to be evaluted has been evaluated and
47     its severity class has been found to be quite serious,
48     class 'D'. As part of this issue, the id has also been
49     increased to 3 and the hazard description has been
50     changed to "Fluid leak in tank 4".
51     */
52     /* Original: lastHazard->severityClass = 'D'; */
53     /* CHANGE THE CODE HERE: */
54     lastHazard->severityClass = 'D';
55     
56     /* Original: lastHazard->description = "Fluid leak in tank 4"; */
57     /* CHANGE THE CODE HERE: */
58     lastHazard->description = "Fluid leak in tank 4";
59     printf("Hazard 2 after description and D-class update:\n");
60     printHazard(&hazard2);
61     return 0;
62 }
63 void printHazard(struct hazard *hazard){
64     printf("Hazard %d: %s [Class %c, extraDataType: %d]\n",
65     hazard->id, hazard->description, hazard->severityClass,
66     hazard->extraDataType);
67 }

即:不使用.和->替换目标代码,提示使用offsetof函数。

关于offsetof函数:http://man7.org/linux/man-pages/man3/offsetof.3.html

第一条:

1 hazard1.severityClass = 'B';

替换为:

1     //*(char *)((void *)(&hazard1) + offsetof(struct hazard, severityClass)) = 'B';
2     *(char *)((void *)(&hazard1) + sizeof(char *) + sizeof(void *) + sizeof(int) + sizeof(int)) = 'B';

为何是(void *)(&hazard1)?

&hazard1代表了该结构体变量和其首成员的地址,直接+1或者(struct hazard *)(&hazard1)+1则直接跳出了该结构体变量的范围(如数组int a[10]:*(a+1)是a[1]一样),使用(void *)让其以字节为单位进行偏移(也可用(char *)),这样就不会跳出该结构体变量了。 源自Psrion对我提出问题的回答https://q.cnblogs.com/q/111494/

也可使用sizeof根据成员在结构体中定义的顺序进行偏移。

最后一条:

1 lastHazard->description = "Fluid leak in tank 4";

替换为:

1     //*(char **)((void *)(lastHazard) + offsetof(struct hazard, description)) = "Fluid leak in tank 4";
2     //*(char **)((void *)(lastHazard)) = "Fluid leak in tank 4";
3     *(char **)(lastHazard) = "Fluid leak in tank 4";

lastHazard为结构体指针,故不用&,description为结构体中第一个成员,即结构体变量地址同时也是该成员的地址。

答案:

 1 /*
 2 This program was written by Richard Chad Sparrow
 3 as a test case for AB-testing the hazard management
 4 system.
 5 */
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 #include <stddef.h>
 9 struct hazard {
10     char *description;
11     void *extraData;
12     int extraDataType;
13     int id;
14     char severityClass;
15 };
16 void printHazard(struct hazard *hazard);
17 int main(int argc, char **argv){
18     struct hazard hazard1;
19     struct hazard hazard2;
20     struct hazard *lastHazard;
21     /* Hazard data setup. */
22     hazard1.description = "Brake service required.";
23     hazard1.extraData = NULL;
24     hazard1.extraDataType = 0;
25     hazard1.id = 1;
26     hazard1.severityClass = 'A';
27     hazard2.description = "Unknown issue in fluid level.";
28     hazard2.extraData = NULL;
29     hazard2.extraDataType = 0;
30     hazard2.id = 2;
31     hazard2.severityClass = 'U';
32     lastHazard = &hazard2;
33     printf("Hazards after setup:\n");
34     printHazard(&hazard1);
35     printHazard(&hazard2);
36     /*
37     The brake service hazard has been present for multiple
38     services, so needs to be updated to severity class 'B'.
39     */
40     /* Original: hazard1.severityClass = 'B'; */
41     /* CHANGE THE CODE HERE: 
42     //hazard1.severityClass = 'B';*/
43     
44     //*(char *)((void *)(&hazard1) + offsetof(struct hazard, severityClass)) = 'B';
45     *(char *)((void *)(&hazard1) + sizeof(char *) + sizeof(void *) + sizeof(int) + sizeof(int)) = 'B';
46     
47     printf("Hazard 1 after class B severity update:\n");
48     printHazard(&hazard1);
49     /*
50     The next hazard to be evaluted has been evaluated and
51     its severity class has been found to be quite serious,
52     class 'D'. As part of this issue, the id has also been
53     increased to 3 and the hazard description has been
54     changed to "Fluid leak in tank 4".
55     */
56     /* Original: lastHazard->severityClass = 'D'; */
57     /* CHANGE THE CODE HERE: 
58     lastHazard->severityClass = 'D';*/
59     
60     *(char *)((void *)(lastHazard) + offsetof(struct hazard, severityClass)) = 'D';
61     
62     /* Original: lastHazard->description = "Fluid leak in tank 4"; */
63     /* CHANGE THE CODE HERE: 
64     lastHazard->description = "Fluid leak in tank 4";*/
65     
66     //*(char **)((void *)(lastHazard) + offsetof(struct hazard, description)) = "Fluid leak in tank 4";
67     //*(char **)((void *)(lastHazard)) = "Fluid leak in tank 4";
68     *(char **)(lastHazard) = "Fluid leak in tank 4";
69     
70     printf("Hazard 2 after description and D-class update:\n");
71     printHazard(&hazard2);
72     return 0;
73 }
74 void printHazard(struct hazard *hazard){
75     printf("Hazard %d: %s [Class %c, extraDataType: %d]\n",
76     hazard->id, hazard->description, hazard->severityClass,
77     hazard->extraDataType);
78 }

 

转载于:https://www.cnblogs.com/Will-zyq/p/10049052.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/426340.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

项目管理(7):备战pmp

1.2制定项目初步范围说明书 项目范围说明书&#xff08;初步&#xff09; 目的&#xff1a; 登记项目及其产品和服务的特征与边界&#xff0c;以及验收与范围控制的方法 内容&#xff1a; 项目与产品的目标 产品或服务的要求与特性 产品验收标准 项目边界 项目要求与可…

旅馆客未满 计算机术语,前厅测试题

《前厅服务》单元测试一一、单项选择题(每题1分&#xff0c;共50分)1、按照人类生产、生活、社会实践活动领域的不同&#xff0c;道德可划分为三个组成部分&#xff0c;其中不包括()。A、职业道德B、家庭美德C、社会良知D、社会公德2、职业道德是人们在长期的职业活动中形成的(…

2018 java蓝桥杯校赛题目

1.【问题描述】 100 可以表示为带分数的形式&#xff1a;100 3 69258 / 714还可以表示为&#xff1a;100 82 3546 / 197注意特征&#xff1a;带分数中&#xff0c;数字1~9分别出现且只出现一次&#xff08;不包含0&#xff09;。类似这样的带分数&#xff0c;100 有 11 种表…

计算机操作系统(7):进程的基本概念

2.1 进程的基本概念 程序的顺序执行及其特征程序的并发执行及其特征进程的特征与状态进程控制块 2.1.1 程序的顺序执行及其特征 顺序执行包含两层含义&#xff1a; 在单道批处理系统中&#xff0c;对于多个用户程序来说&#xff0c;所有程序是依次执行的。(外部顺序性) 对…

无法打开计算机上的event log服务,Win7系统下启用Windows event log服务发生4201错误的正确解决方法...

Windows event log服务会把程序与系统发送的错误消息记录在日志中&#xff0c;其中还包含了部分有用的诊断信息&#xff0c;近期有Win7用户在启用windows event log服务的时候&#xff0c;发生4201错误提示框。那么该如何解决该问题&#xff1f;下面装机之家分享一下Win7系统下…

广义线性模型的计算机应用技术学院,SPSS数据分析—广义线性模型

我们前面介绍的一般线性模型、Logistic回归模型、对数线性模型、Poisson回归模型等&#xff0c;实际上均属于广义线性模型的范畴&#xff0c;广义线性模型包含的范围非常广泛&#xff0c;原因在于其对于因变量、因变量的概率分布等条件的限制放宽&#xff0c;使其应用范围加大。…

没有时间

请理解时间是运动的表现 转载于:https://www.cnblogs.com/eiya/p/10053311.html

中国第一个计算机病毒什么时候,新中国成立以来的第一例电脑病毒小球病毒是在1988年发现...

扩展阅读&#xff1a;1988年&#xff0c;随着软件交流&#xff0c;石头和小球病毒跟随软盘悄悄地通过香港和美国进入了中国内地&#xff0c;并在人们的懵懂之间在大型企业和研究所间广为传播。小球病毒是国内发现的第一个计算机病毒。发作条件是当系统时钟处于半点或整点&#…

计算机操作系统(8):进程的控制

2.2 进程控制 进程控制是进程管理中最基本的功能。 进程控制包括&#xff1a; ◆ 创建进程 ◆ 终止进程 ◆ 进程状态转换 进程控制是由OS的内核完成的。 2.2.1 进程的创建 2&#xff0e;进程的创建 调用进程创建原语Create&#xff08;&#xff09;&#xff0c;按下述…

Linux起不来,如何修复

用于系统启动不了的情况&#xff1a; 配置文件修改错误。 grub挂的了。 忘记Linux root密码&#xff0c;修改root密码 rd.break mount -o remount,rw /sysroot chroot /sysroot 这样既可对系统进行修复啦&#xff01;比如修改/etc/fstab文件。转载于:https://www.cnblogs.com/y…

计算机操作系统(9):深入理解B/S与C/S架构

深入理解B/S与C/S架构 阅读目录 C/S架构简要介绍 什么是B/S架构 B/S架构的几种形式 发展前景 一、C/S架构简要介绍 在了解什么是B/S架构之前&#xff0c;我们有必要了解一下什么是C/S架构&#xff1a; C/S架构是第一种比较早的软件架构&#xff0c;主要用于局域网内。也叫…

计算机信息管理相关课题论文,计算机信息管理课题论文[].doc

计算机信息管理课题论文[]目 录一、 引言………………………………………………………………… 2二、方案目标………………………………………………………………2三、安全需求………………………………………………………………3四、风险分析……………………………………………

硬件设计--DC/DC电源芯片详解

本文参考&#xff1a;http://www.elecfans.com/article/83/116/2018/20180207631874.html https://blog.csdn.net/wangdapao12138/article/details/79763343 第一次写博客&#xff0c;不喜勿喷&#xff0c;谢谢&#xff01;&#xff01;&#xff01; DC/DC电源指直流转换为直流…

计算机操作系统(10):集群和分布式

简单说&#xff0c;分布式是以缩短单个任务的执行时间来提升效率的&#xff0c;而集群则是通过提高单位时间内执行的任务数来提升效率。 例如&#xff1a; 如果一个任务由10个子任务组成&#xff0c;每个子任务单独执行需1小时&#xff0c;则在一台服务器上执行改任务需10小时…

Unity预计算全局实时GI(gi params)

Unity提供许多进阶的光照贴图控制方法&#xff0c;这些设定被放在一个光照参数集(LightmapParameters)的资源里&#xff0c;好让设定能在不同的项目场景或是透过版本控制和团队一起共享。 要建立一个Lightmap Parameters资源&#xff0c;先找到Project窗口从Create下拉选单建立…

计算机工程与应用单像素成像,2011计算机工程与应用基于压缩感知理论的单像素成像系统研究_白凌云.pdf...

2011计算机工程与应用基于压缩感知理论的单像素成像系统研究_白凌云.pdf116 2011 &#xff0c;47 (33) Computer Engineering and Applications 计算机工程与应用基于压缩感知理论的单像素成像系统研究1 1 2白凌云&#xff0c;梁志毅 &#xff0c;徐志军1 1 2BAI Lingyun &…

计算机操作系统(11):负载均衡

负载均衡是高可用网络基础架构的的一个关键组成部分&#xff0c;有了负载均衡&#xff0c;我们通常可以将我们的应用服务器部署多台&#xff0c;然后通过负载均衡将用户的请求分发到不同的服务器用来提高网站、应用、数据库或其他服务的性能以及可靠性。 为什么要引入负载均衡…

[bzoj3994] [SDOI2015]约数个数和

Description 设d(x)为x的约数个数&#xff0c;给定N、M&#xff0c;求 \(\sum_{i1}^N\sum_{j1}^Md(ij)\) Input 输入文件包含多组测试数据。 第一行&#xff0c;一个整数T&#xff0c;表示测试数据的组数。 接下来的T行&#xff0c;每行两个整数N、M。 Output T行&#xff0c;每…

计算机配置对电子竞技的影响,配置高并不是唯一优点 看看电竞硬件还要啥?...

01宁美国度星河 战列舰XS现在有很多游戏品牌依靠品牌的力量为自己背书&#xff0c;以至于很多游戏玩家很难找的合适自己的高性能电脑&#xff0c;电竞体验也就无从谈起&#xff0c;但是今年的新品里还是有一些意外之喜&#xff0c;在整机性能上表现不俗。比如电脑发烧友们向来有…

玩转oracle 11g(10):初步了解Rman备份

备份与恢复的定义及分类 备份的定义及分类&#xff1a; 备份就是把数据库复制到转储设备的过程。其中&#xff0c;转储设备是指用于放置数据库副本的磁带或磁盘。通常也将存放于转储设备中的数据库的副本称为原数据库的备份或转储。备份是一份数据副本&#xff0c;从不同的角度…