Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does it mean to convert int to void* or vice versa?

Tags:

c

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 */

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

like image 980
Mathai Avatar asked Dec 23 '11 16:12

Mathai


People also ask

Can void be converted to int?

You're return ing the value of int sum by setting a void * address to it. In this case, the address is not valid. But, if you keep that in mind and get the value of sum by casting a void * to int it will work.

Can you convert an integer to character and vice versa?

We can use the built-in function chr() to convert an integer to its character representation in Python.

How do you assign an int to a void?

Now, we want to assign the void pointer to integer pointer, in order to do this, we need to apply the cast operator, i.e., (int *) to the void pointer variable. This cast operator tells the compiler which type of value void pointer is holding.


3 Answers

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:

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.

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

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:

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.

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

like image 199
bobbymcr Avatar answered Oct 09 '22 05:10

bobbymcr


Both void* pointer (or any pointer for that matter) and int are, roughly speaking, numbers. They may be of different bitsize, but it is unlikely that pointer is smaller than int, so that makes the operation reversible. Of course, it's illegal and you should never dereference the pointer that has no valid location to point to.

like image 32
Michael Krelin - hacker Avatar answered Oct 09 '22 05:10

Michael Krelin - hacker


The C standard specifies that it must be possible to convert a void pointer to an integral type such that converting the integral type back to a void pointer will yield the same pointer. I'm not sure if it requires that converting a null pointer to an integer yield the value zero, or if only numeric literal zeroes are recognized as special. On many implementations, the integer value represents a hardware address, but the standard makes no such guarantee. It would be entirely possible that on hardware which included a special trap for hardware address 0x12345678, converting a pointer to an integer would subtract 0x12345678 from the hardware address, and converting an integer to a pointer would add 0x12345678 back in (thus an integer value of zero would represent a null pointer).

In many cases, particularly when developing for embedded controllers, the compiler vendor will explicitly specify what hardware address will be accessed when converting a particular integer value to a pointer type. On processors with a single linear address space, converting an integer value 0x12345678 to a pointer would generally yield a pointer which refers to address 0x12345678; the hardware reference manual would indicate if there was anything special about that location. On processors with more 'interesting' address spaces, it may be necessary to use something other than the hardware address as the pointer. For example, on antique IBM PC computers, the display buffer was mapped at hardware address 0xB8000, but in most compilers, the address would be expressed as (short far*)0xB8000000.

like image 20
supercat Avatar answered Oct 09 '22 05:10

supercat