这是为什么铸造空指针是否有效? [英] why is this casting to void pointer valid?

查看:124
本文介绍了这是为什么铸造空指针是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试从一本书的程序。该程序似乎工作,但我不明白的一条线,我下面的评论。

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
/ *计算连续的素数(效率很低)。返回
第N个质数,其中N是看重* ARG指出。 * /
无效* compute_prime(无效* ARG)
{
INT候选人= 2;
INT N = *((INT *)ARG);
而(1){
INT因素;
INT is_prime = 1;
/ *通过连续师测试素性。 * /
为(系数= 2;因子所述候选++因子)
如果(候选%的因素== 0){
is_prime = 0;
打破;
}
/ *这是我们正在寻找的素数? * /
如果(is_prime){
如果(--N == 0)
/ *返回所需的素数作为线程返回值。 * /
回报(无效*)候选人; //这是为什么铸造有效吗? (候选甚至不是一个指针)
}
++候选人;}
返回NULL;
}
诠释的main()
{
线程的pthread_t;
INT which_prime = 5000;
INT素;
/ *启动计算线程,直到第5000素数。 * /
在pthread_create(安培;螺纹,NULL,&安培; compute_prime,&安培; which_prime);
/ *做一些其他的工作,在这里... * /
/ *等待质数的线程来完成,并得到结果。 * /
在pthread_join(螺纹,(无效*)及素数);
/ *打印它计算的最大素数。 * /
的printf(%的DTH素数为%d \\ n,which_prime,素数);
返回0;
}


解决方案

这是无效的。它只是发生,如果工作的sizeof(int)的==的sizeof(无效*),这恰好在许多系统上。

A 无效* 只能保证能够举行指针的数据对象。

下面是一个Ç常见问题解答关于这个问题的。


  

如何转换成整数,并从指针? 我暂时
  东西的整数为指针
,反之亦然?


  
  

指针到整数和整数到指针的转换是
  实现定义(参见问题11.33),且不再有
  该指针可以被转换为整数和背面的任何保证,
  不改变


  
  

强制指针为整数或整数进三分球,从来没有
  已经很好的做法


I'm debugging a program from a book. The program appears to work but I do not understand one line which I comment below.

#include <pthread.h>
#include <stdio.h>
/* Compute successive prime numbers (very inefficiently). Return the
Nth prime number, where N is the value pointed to by *ARG. */
void* compute_prime (void* arg)
{
int candidate = 2;
int n = *((int*) arg);
while (1) {
int factor;
int is_prime = 1;
/* Test primality by successive division. */
for (factor = 2; factor < candidate; ++factor)
if (candidate % factor == 0) {
is_prime = 0;
break;
}
/* Is this the prime number we’re looking for? */
if (is_prime) {
if (--n == 0)
/* Return the desired prime number as the thread return value. */
return (void*) candidate;    // why is this casting valid? (candidate is not even a pointer)
}
++candidate;

}
return NULL;
}
int main ()
{
pthread_t thread;
int which_prime = 5000;
int prime;
/* Start the computing thread, up to the 5,000th prime number. */
pthread_create (&thread, NULL, &compute_prime, &which_prime);
/* Do some other work here... */
/* Wait for the prime number thread to complete, and get the result. */
pthread_join (thread, (void*) &prime);
/* Print the largest prime it computed. */
printf("The %dth prime number is %d.\n", which_prime, prime);
return 0;
}

解决方案

It's not valid. It simply happens to work if sizeof(int) == sizeof(void *), which happens on many systems.

A void * is only guaranteed to be able to hold pointers to data objects.

Here is a C FAQ on the subject.

How are integers converted to and from pointers? Can I temporarily stuff an integer into a pointer, or vice versa?

Pointer-to-integer and integer-to-pointer conversions are implementation-defined (see question 11.33), and there is no longer any guarantee that pointers can be converted to integers and back, without change

Forcing pointers into integers, or integers into pointers, has never been good practice

这篇关于这是为什么铸造空指针是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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