2

Why is casting from void* to int and vice versa allowed in C? Where is this used other than pthread?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • 1
    It's just because it's handy in pthread_create. & `void*` & `int` have usually same size - 4 bytes. But you should not assume this. Ideally, you should pass integer pointer to it, instead of value. But for numeric constants, we use this hack to pass the value themselves. – anishsane Apr 23 '15 at 07:28
  • See also [When is an integer<->pointer cast actually correct?](https://stackoverflow.com/questions/7146813). – Christian Aichinger Apr 23 '15 at 07:30
  • 2
    @anishane: `"void* & int have usually same size - 4 bytes"` - that's a very dangerous assumption - it used to be true in the good old 32 bit days, but these days most desktop and server operating systems are 64 bit (LP64 = 4 byte int and 8 byte pointer). – Paul R Apr 23 '15 at 07:30
  • One reason that casting from `int` to pointer is allowed in C is so that code can access [memory mapped I/O](http://en.wikipedia.org/wiki/Memory-mapped_I/O#Example). – user3386109 Apr 23 '15 at 08:00

2 Answers2

5

It is allowed, but the behavior is implementation-defined.

Sometimes, under certain situation (on particular platform/ architecture), this provision may be useful for quick-and-dirty hacks/tricks involving memory addresses by using operators that needs int operands. [Ex: XOR operation]. Can be useful for memory optimization for limited-memory embedded devices.

To quote the standard regarding the functionality,

The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to be consistent with the addressing structure of the execution environment.

Related Reading: From c11, chapter 6.3.2.3,

Paragraph 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.

and paragraph 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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

The following Standard references define those conversion. As you can see they are implementation defined. You should only convert if you know what you are doing and know your platform.

6.3.2.3 Pointers

  1. 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.

  2. 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

2501
  • 25,460
  • 4
  • 47
  • 87