简体   繁体   English

类型转换整数指向c中的整数

[英]Typecasting integer to pointer to integer in c

I saw somewhere in a pthreads program something like this... 我在pthreads程序中看到过这样的东西...

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

void *fxn(void * t)
{
//some code
}

int main()
{
//some code
pthread_create(&thread,NULL,fxn,(int *)5);
//some code
}

Here, what is (int *)5 doing? 这里,(int *)5在做什么? and why is it not generating a warning/error? 为什么它不会产生警告/错误? Also, how is it different from pthread_create(&thread,NULL,fxn,&x); 另外,它与pthread_create(&thread,NULL,fxn,&x)有什么不同; where x=5 is of type int ? 其中x = 5是int类型?

Edit: 编辑:

This works fine: 这很好用:

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

void * fxn(void *t)
{
    pthread_exit(t);
}
int main()
{
    pthread_t th;
    void *ret;
    int i=5;
    pthread_create(&th,NULL,fxn,&i);
    pthread_join(th,&ret);
    printf("%d\n",*(int *)ret);
}

but, 但,

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

void * fxn(void *t)
{
    pthread_exit(t);
}
int main()
{
    pthread_t th;
    void *ret;
    int i=5;
    pthread_create(&th,NULL,fxn,(int *)5);
    pthread_join(th,&ret);
    printf("%d\n",*(int *)ret);
}

gives segmentation fault,core dumped...Why? 给出分段错误,核心倾倒......为什么?

Technically, (int *)5 interprets 5 as an address. 从技术上讲, (int *)55解释为地址。 The result is typically a pointer to the address 0x0005 in your address space. 结果通常是指向地址空间中地址0x0005的指针。

In this case, that's almost certainly not the real intention. 在这种情况下,这几乎肯定不是真正的意图。 (Such an address is almost always invalid if you're not doing low-level system stuff, at which point you probably don't have pthread_create available anyway.) (如果你没有做低级系统的话,这样的地址几乎总是无效的,此时你可能还没有pthread_create可用。)

Here, since pthread_create takes a void * as some "extra data" to pass to the thread's startup function (in this case fxn ), whatever data you want to pass via that argument must be converted to a pointer. 这里,由于pthread_createvoid *作为一些“额外数据”传递给线程的启动函数(在本例中为fxn ),因此您希望通过该参数传递的任何数据都必须转换为指针。

The code in fxn will typically include an expression like (int)t to convert the pointer back into an integer. fxn的代码通常包含一个类似于(int)t的表达式,将指针转换回整数。 That's basically the only useful thing that can be done with it -- you're the only one that knows your pointer isn't really a pointer, and if anything (like, say, the last line of your main function...) ever tries to actually use it as a pointer, it's quite likely to trigger a segfault. 这基本上是可以用它完成的唯一有用的东西 - 你是唯一一个知道你的指针不是真正的指针,如果有的话(比如说,你的main函数的最后一行......)曾试图将它实际用作指针,它很可能会引发段错误。

You don't get an error because by casting you tell the compiler that you know what you are doing. 您没有收到错误,因为通过强制转换告诉编译器您知道自己在做什么。

The cast (int*)5 converts 5 to an int pointer pointing to some( very likely )invalid address. cast (int*)5(int*)5转换为指向某个(非常可能)无效地址的int指针。 To get that value in the thread you have to cast void pointer back to an integer (int)t . 要在线程中获取该值,必须将void指针强制转换为整数(int)t Do not do this. 不要这样做。

Passing &x , gets you a pointer to a variable x , which you can use in the thread function. 传递&x ,获取指向变量x的指针,您可以在线程函数中使用它。 Similar to this: 与此类似:

int x = 123 ;
int* px = &x ;
pthread_create(&thread,NULL,fxn,px);

And in the thread: 并在线程中:

int* pt = t ;
printf( "%d" , *pt ) ;

(int*)5 means casting 5 to a pointer to an int. (int*)5表示将5转换为指向int的指针。

This makes possible to pass a pointer which points to 5 in memory to the function pthread_create . 这使得可以将指向内存中的5的指针传递给函数pthread_create

For example: 例如:

(int) 5; //cast to int
(int *) 5; //cast to pointer to int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM