- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在处理一些使用外部库的代码,您可以在其中通过 void*
值将值传递给回调。
不幸的是,之前编写此代码的人决定通过将整数转换为空指针 ((void*)val
) 来将整数传递给这些回调。
我现在正在努力清理这个烂摊子,我正在尝试确定将整数转换为 void*
的“正确”方法。不幸的是,修复 void 指针的使用有点超出了我在这里能够做的返工的范围。
现在,我正在执行两次转换以从/转换为 void 指针:
static_cast<int>(reinterpret_cast<intptr_t>(void_p))
和
reinterpret_cast<void *>(static_cast<intptr_t>(dat_val))
由于我在 64 位机器上,直接转换 ((int)void_p
) 会导致错误:
error: cast from 'void*' to 'int' loses precision [-fpermissive]
原始实现确实与 -fpermissive
一起工作,但我正试图摆脱它以解决可维护性和与错误相关的问题,所以我试图“正确地”执行此操作,例如C++ 转换。
直接转换为 int ( static_cast<int>(void_p)
) 失败 ( error: invalid static_cast from type 'void*' to type 'int'
)。我对 reinterpret_cast
的理解是,它基本上只是导致编译器将相关值的地址视为转换为数据类型,而不实际发出任何机器代码,因此将 int
直接转换为 void*
是一个坏主意,因为void*
比 int
大(分别为 4/8 字节)。
我认为在这里使用 intptr_t
是正确的中间值,因为它保证足够大以包含 void*
的整数值,一旦我有一个整数值,我就可以截断它而不会导致编译器提示。
考虑到我不得不通过 void 指针推送数据,这是正确的,甚至是明智的方法吗?
最佳答案
I think using
intptr_t
is the correct intermediate here, since it's guaranteed to be large enough to contain the integral value of thevoid*
, and once I have an integer value I can then truncate it without causing the compiler to complain.
是的,正如您提到的那样,这是正确的中间类型。到现在为止,如果您的实现没有提供它,那么您可能遇到的问题不仅仅是缺少 typedef。
Is this the correct, or even a sane approach given I'm stuck having to push data through a void pointer?
是的,考虑到限制,它非常理智。
您可能会考虑检查值是否适合,而不是在 Debug模式下从 void*
中解压缩它时简单地截断它,或者甚至使用 intptr
代替该整数的所有进一步处理int
以避免截断。
您还可以考虑通过该参数将指针推送到实际的 int
而不是 int
本身。请注意,这虽然效率较低,但会让您面临终生问题。
关于c++ - 在 C++ 中正确地将 `void*` 转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768714/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!