通过 void * 投射枚举

发布于 2024-11-24 13:50:58 字数 608 浏览 2 评论 0原文

我正在尝试将一些旧的 32 位代码转换为 64 位。我收到的最常见的警告之一是:

warning:cast ofpointer to integer of different size

当函数调用 pthread_create 接受 void * 用于将数据传递给新线程。调用线程在 void * 中放入一个枚举(因此 64 位中的大小不匹配)。这是代码:

typedef enum {
zero,
one,
two
}numbers_e;

some_function(...)
{
    numbers_e mydata=zero;

    ...
    pthread_create(..., (void *)mydata);
    ...
}

我设法克服了这个警告:

pthread_create(..., (void *)0 + my_data);

这个解决方案非常丑陋(我正在考虑是否最好将警告保留在使用它的代码附近的大注释中)。还有其他解决方案吗?

I'm trying to convert some old 32 bit code to 64 bit. One of the more frequent warnings I get is:

warning: cast of pointer to integer of different size

this happens when a function calls pthread_create which accepts a void * for passing data to the new thread. The calling thread puts in that void * an enum (hence the size mismatch in 64bit). Here's the code:

typedef enum {
zero,
one,
two
}numbers_e;

some_function(...)
{
    numbers_e mydata=zero;

    ...
    pthread_create(..., (void *)mydata);
    ...
}

I managed to overcome the warning with this:

pthread_create(..., (void *)0 + my_data);

this solution is very ugly (and I'm pondering if it's better to leave the warning as is with a big remark near the code using it). Is there another solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不弃不离 2024-12-01 13:50:58

你的解决方案不仅丑陋,而且是未定义的行为(UB),可能会在将来给你带来问题:

  • void*指针上的算术是非标准的,必须是一个
    编译器
  • 算术对不指向有效对象的指针的扩展是 UB

要避免第一个,您可以使用 (char*)0 + my_data,但这仍然会留下第二个。

您可以做什么

  • 将您的值转换为uintptr_t。这是一种保证的类型
    void* 兼容(如果存在)。它存在于大多数现代
    平台。优点是你的代码无法编译
    它不存在的平台,明确表明您已经拥有
    那么,改变一些东西。
  • 使用指向数据的指针。这才是真正的解决办法
    pthreads 是

为两者设计的,但是,您必须修改被调用函数的源代码,因此您最好将其更改为第二个,这应该是这样的。

your solution is not only ugly, it is undefined behaviour (UB) that could cause you problems in the future:

  • arithmetic on void* pointers is non standard and must be an
    extension of your compiler
  • arithmetic on pointer that don't point to valid object is UB

To avoid the first you could use (char*)0 + my_data, but this would still leave you with the second.

What you could do

  • cast your value to uintptr_t. this is a type that is guaranteed to
    be compatible with void*, if it exists. It exists on most modern
    platforms. The advatage would be that your code wouldn't compile on
    platforms where it doesn't exist, a clear indication that you'd have
    to change something, then.
  • use a pointer to the data. this is the real solution, the way
    pthreads are designed

For both you'd have to modify the source of the called function, though, so you'd well change it to the second, the way how this should be.

三寸金莲 2024-12-01 13:50:58

是的,传递对象的地址。

pthread_create(..., (void *)&mydata);

只要 mydata 在某处仍然可用,编辑就应该是安全的。

Yeah, pass the address of the object.

pthread_create(..., (void *)&mydata);

That should be safe edit as long as mydata remains available somewhere.

£烟消云散 2024-12-01 13:50:58

将数据作为指针传递几乎不是一个好主意,但如果有很多代码执行此操作,请保持原样。
否则,我会按照 cnicutar 的建议传递地址。
请记住,如果 mydata 范围在线程开始之前结束,则可能会出现损坏问题。

passing a data as a pointer is hardly a good idea, but if there is a lot of code that does it - just leave it as is.
otherwise, I would pass the address as cnicutar suggested.
keep in mind that if mydata scope ends before the thread begins you may have a corruption problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文