博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用offsetof对结构体指针偏移操作
阅读量:5126 次
发布时间:2019-06-13

本文共 7874 字,大约阅读时间需要 26 分钟。

题目来自于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 
7 #include
8 #include
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 multiple38 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 and47 its severity class has been found to be quite serious,48 class 'D'. As part of this issue, the id has also been49 increased to 3 and the hazard description has been50 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 
7 #include
8 #include
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 multiple38 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 and51 its severity class has been found to be quite serious,52 class 'D'. As part of this issue, the id has also been53 increased to 3 and the hazard description has been54 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

你可能感兴趣的文章
后RCNN时代的物体检测及实例分割进展
查看>>
File类的创建,删除文件
查看>>
Cookie/Session机制详解
查看>>
Struts随笔
查看>>
php保留小数方式
查看>>
软件工程第3次作业 | 提问回顾与个人总结
查看>>
读锁跟写锁的区别
查看>>
铁大通。
查看>>
【22】java初始化(静态变量)
查看>>
【38】通过继承扩展接口
查看>>
Never Wait for Weights(带权并查集+路径压缩)
查看>>
hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
查看>>
Custom VirtualPathProvider
查看>>
2018年总结和明年计划
查看>>
selenuim+java
查看>>
Gym - 100203H Highways 最小生成树
查看>>
redhat基本操作
查看>>
.net Basic
查看>>
C++宏的学习笔记
查看>>
Android APK反编译详解(附图)
查看>>