Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intptr_t instead of void*?

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html)

For what I've already read:

  • int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do
  • pointer arithmetics on both void* and intptr_t require casts, so none gets advantage here
  • void* means less explicit casts when storing pointers, intptr_t means less casts when storing integer values
  • intptr_t requires C99

What else should I take into consideration?

like image 984
Gepard Avatar asked Feb 29 '12 02:02

Gepard


People also ask

Can you free a void * in C?

yes it is safe. Show activity on this post. In one case, malloc and free match, and in the other the allocated memory is returned. There are also secondary resources, and the constructor and destructor match.

What does void * func () mean?

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.

What is void * in C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is Intptr_t type?

intptr_t is a signed integer memsize-type that can safely store a pointer regardless of the platform capacity. The type intptr_t is similar to the types ptrdiff_t and INT_PTR. The size of the type depends upon the data model.


1 Answers

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*?

No.

intptr_t is not guaranteed to exist. First, as you note, it was introduced in C99. Second, implementations are not required to have an integer type big enough to hold converted pointer values without loss of information.

Converting an int to intptr_t and back is unlikely to lose information but there's no actual guarantee that intptr_t is wider than int.

If you want to store pointer values, store them in pointer objects. That's what pointer objects are for.

Any pointer to an object or incomplete type can be converted to void* and back again without loss of information. There is no such guarantee for pointers to functions -- but any pointer-to-function type can be converted to any other pointer-to-function-type and back without loss of information. (I'm referring to the C standard; I think POSIX provides some additional guarantees.)

If you want to store either an integer or a pointer value in the same object, the first thing you should do is re-think your design. If you've already done so, and concluded that you really do want to do this, consider using a union (and keeping careful track of what kind of value you've stored most recently).

There are APIs that use a void* argument to allow arbitrary data to be passed; see, for example, the POSIX pthread_create() function. This can be abused by casting an integer value to void* but it's safer to pass the address of an integer object.

like image 58
Keith Thompson Avatar answered Oct 02 '22 06:10

Keith Thompson