如何将整数转换为空指针? [英] How to cast an integer to void pointer?

查看:27
本文介绍了如何将整数转换为空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中使用线程时,我面临警告

警告:从不同大小的整数转换为指针"

代码如下

#include#include#include#include无效*打印(无效*id){int a=10;printf("我的线程id是%ld
",pthread_self());printf("线程 %d 正在执行
",id);返回(空*)42;}int main(){pthread_t th[5];国际t;国际我;内部状态;无效 *ret;for(i=0;i<5;i++){status=pthread_create(&th[i],NULL,print,(void *)i);//在这一行得到警告如果(状态){printf("创建线程时出错
");退出(0);}pthread_join(th[i],&ret);printf("--->%d
",(int *)ret);}pthread_exit(NULL);}

谁能解释一下如何将整数传递给接收 (void * ) 作为参数的函数?

解决方案

如果您需要,这是将整数传递给新 pthread 的好方法.你只需要抑制警告,这样做就可以了:

#include void *threadfunc(void *param){int id = (intptr_t) 参数;...}内部,r;r = pthread_create(&thread, NULL, threadfunc, (void *) (intptr_t) i);

讨论

这可能会冒犯您的敏感性,但它非常短并且没有竞争条件(就像您使用 &i 时那样).仅仅为了得到一堆编号的线程而编写几十行额外的代码是没有意义的.

数据竞争

这是一个糟糕的版本,存在数据竞争:

#include #include #define N 10void *thread_func(void *arg){int *ptr = arg;//当我们到达这里时 *ptr 是否发生了变化?或许!printf("Arg = %d
", *ptr);返回空;}int main(){国际我;pthread_t 线程[N];for (i = 0; i 

现在,当我使用线程消毒器运行它时会发生什么?

(另外,看看它是如何打印5"两次...)

<前>==================警告:ThreadSanitizer:数据竞争(pid=20494)线程 T1 在 0x7ffc95a834ec 处读取大小 4:#0 thread_func/home/depp/test.c:9 (a.out+0x000000000a8c)#1 <null><空>(libtsan.so.0+0x000000023519)主线程先前在 0x7ffc95a834ec 处写入大小为 4:#0 main/home/depp/test.c:17 (a.out+0x000000000b3a)位置是主线程的堆栈.主线程在以下位置创建的线程 T1(tid=20496,正在运行):#0 pthread_create (libtsan.so.0+0x0000000273d4)#1 main/home/depp/test.c:18 (a.out+0x000000000b1c)总结:ThreadSanitizer:数据竞争/home/depp/test.c:9 thread_func==================参数 = 1精氨酸 = 2精氨酸 = 3精氨酸 = 4精氨酸 = 5精氨酸 = 6精氨酸 = 7精氨酸 = 8精氨酸 = 9精氨酸 = 5ThreadSanitizer:报告了 1 个警告

While working with Threads in C, I'm facing the warning

"warning: cast to pointer from integer of different size"

The code is as follows

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<pthread.h>
void *print(void *id)
{
 int a=10;
 printf("My thread id is %ld
",pthread_self());
 printf("Thread %d is executing
",id);
 return (void *) 42;
}

int main()
{
 pthread_t th[5];
 int t;
 int i;
 int status;
 void *ret;
 for(i=0;i<5;i++)
 {
   status=pthread_create(&th[i],NULL,print,(void *)i); //Getting warning at this line
   if(status)
   {
    printf("Error creating threads
");
    exit(0);
   }
   pthread_join(th[i],&ret);
   printf("--->%d
",(int *)ret);
 }
 pthread_exit(NULL);
}

Can anybody explain how to pass an integer to a function which receives (void * ) as a parameter?

解决方案

This is a fine way to pass integers to new pthreads, if that is what you need. You just need to suppress the warning, and this will do it:

#include <stdint.h>

void *threadfunc(void *param)
{
    int id = (intptr_t) param;
    ...
}

int i, r;
r = pthread_create(&thread, NULL, threadfunc, (void *) (intptr_t) i);

Discussion

This may offend your sensibilities, but it's very short and has no race conditions (as you'd have if you used &i). No sense in writing a few dozen lines of extra code just to get a bunch of numbered threads.

Data races

Here is a bad version with a data race:

#include <pthread.h>
#include <stdio.h>

#define N 10

void *thread_func(void *arg)
{
    int *ptr = arg;
    // Has *ptr changed by the time we get here?  Maybe!
    printf("Arg = %d
", *ptr);
    return NULL;
}

int main()
{
    int i;
    pthread_t threads[N];
    for (i = 0; i < N; i++) {
        // NO NO NO NO this is bad!
        pthread_create(&threads[i], NULL, thread_func, &i);
    }
    for (i = 0; i < N; i++) {
        pthread_join(threads[i], NULL);
    }
    return 0;
}

Now, what happens when I run it with the thread sanitizer?

(Also, check out how it prints "5" twice...)

==================
WARNING: ThreadSanitizer: data race (pid=20494)
  Read of size 4 at 0x7ffc95a834ec by thread T1:
    #0 thread_func /home/depp/test.c:9 (a.out+0x000000000a8c)
    #1 <null> <null> (libtsan.so.0+0x000000023519)

  Previous write of size 4 at 0x7ffc95a834ec by main thread:
    #0 main /home/depp/test.c:17 (a.out+0x000000000b3a)

  Location is stack of main thread.

  Thread T1 (tid=20496, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x0000000273d4)
    #1 main /home/depp/test.c:18 (a.out+0x000000000b1c)

SUMMARY: ThreadSanitizer: data race /home/depp/test.c:9 thread_func
==================
Arg = 1
Arg = 2
Arg = 3
Arg = 4
Arg = 5
Arg = 6
Arg = 7
Arg = 8
Arg = 9
Arg = 5
ThreadSanitizer: reported 1 warnings

这篇关于如何将整数转换为空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆