这是什么意思转换INT为void *,反之亦然? [英] what does it mean to convert int to void* or vice versa?

查看:172
本文介绍了这是什么意思转换INT为void *,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思为整数值从一个记忆点转换为无效* 或反之亦然?
我的理解是无效* 是一个地址的未指定长度的内存块。结果
这似乎有点像橘子与苹果的比较

What does it mean to convert an integer value to a void* or viceversa from a memory point of view? My understanding is void* is an address to a block of memory of unspecified length.
This seems to be something like comparing apple with oranges.

int myval = 5;
void* ptr = (void*)myval;
printf("%d",(int)ptr);

我意识到,我应该给这何处使用的确切内容。

I realized that I should have given the exact context where this is used.

int main(int argc, char* argv[]) {
long       thread;  /* Use long in case of a 64-bit system */
pthread_t* thread_handles; 

/* Get number of threads from command line */
if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);  
if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]);

thread_handles = malloc (thread_count*sizeof(pthread_t)); 

for (thread = 0; thread < thread_count; thread++)  
  pthread_create(&thread_handles[thread], NULL, Hello, (void*) thread);  

printf("Hello from the main thread\n");

for (thread = 0; thread < thread_count; thread++) 
  pthread_join(thread_handles[thread], NULL); 

free(thread_handles);
return 0;
}  /* main */

/*-------------------------------------------------------------------*/
void *Hello(void* rank) {
long my_rank = (long) rank;  /* Use long in case of 64-bit system */ 

printf("Hello from thread %ld of %d\n", my_rank, thread_count);

return NULL;
}  /* Hello */

这code是彼得Pachecho的书上的并行编程。

This code is from Peter Pachecho's book on Parallel programming.

推荐答案

铸造 INT 无效* 比较无意义的,不应该做的,你将试图在非指针转换为指针。引述 C99标准,部分6.3.2.3项目5:

Casting int to void * is rather meaningless and should not be done as you would be attempting to cast a non-pointer to a pointer. Quoting the C99 standard, section 6.3.2.3 item 5:

的整数可以被转换为任何指针类型。除previously
  指定,其结果是实现定义,可能不
  正确对齐,可能不会指向引用的实体
  类型,可能会重新presentation一个陷阱。

An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

您的可能的投为int * 无效* (任何指针转换为无效* ,你可以像所有指针的基地型想到的)。

You could cast int * to void * (any pointer is convertible to void * which you can think of like the "base type" of all pointers).

铸造无效* INT 是不可移植的,可能是根据所使用的平台上完全错误(例如, 无效* 64也许位宽和 INT 可能只有32位)。再次引用了C99标准,部分6.3.2.3项目6:

Casting void * to int is not portable and may be completely wrong depending on the platform you use (e.g. void * maybe 64 bits wide and int may only be 32 bits). Quoting the C99 standard again, section 6.3.2.3 item 6:

不限指针类型可被转换成一个整数类型。除
  previously指定,其结果是实现定义。如果
  结果不能重新在整型psented $ P $,该行为是
  未定义。结果不必是任何值的范围
  整数类型。

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

要解决这个问题,一些平台提供 uintptr_t形式,它允许你把一个指针作为适当宽度的数值。

To solve this, some platforms provide uintptr_t which allows you to treat a pointer as a numeric value of the proper width.

这篇关于这是什么意思转换INT为void *,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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