Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What <code>(void *)100</code> means is take the integer value 100 and treat it as a pointer to some unspecified type of memory (i.e., <code>(void *)</code>). In that case, that means push the integer value 100 on the stack as an argument to <code>pthread_create</code>. Presumably, <code>ChildThread</code> casts the <code>void *</code> passed to it back to an <code>int</code>, then uses it as a number.</p> <p>Fundamentally pointers are really just memory addresses. A memory address is just a number that describes a location in memory, so casting an <code>int</code> to a pointer of any type is legal. There are a few cases where casting an <code>int</code> to a pointer is absolutely the right thing to do and required, however, they tend to be rare. For example, if you are writing code for an embedded controller, and want write a driver for a memory mapped I/O device, then you might cast the device's base address as a pointer to an int or struct and then do normal C accesses through the pointer to access the device. Another example where casting <code>int</code>s to pointers, would be to implement the low-level virtual memory management routines to parcel out physical memory for an operating system.</p> <p>The code you present is not uncommon and will work, assuming that the size of a pointer is at least big enough to hold the integer you are trying to pass. Most systems that implement <code>pthread_create</code> would probably have a 32-bit or 64-bit pointer, so your example is pretty likely to work. IMHO, it is a bit of an abuse, because 100 probably does not refer to a memory location in this case, and C does not guarantee that a <code>void *</code> is big enough to hold an <code>int</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload