Questions tagged [void-pointers]

A void pointer (void *) in C and C++ is a pointer that points to a memory location with no specified type.

A void pointer (void*) in and is a pointer that points to a memory location with no specified type. A void * is typically used to exchange pointers, where some called function may not care about the type of the argument that it receives — for example, functions which operate on raw memory (such as realloc and bzero) and functions which pass along the pointer without needing to access it (for example, GTK+ callbacks).

In C, the use of void * is considered idiomatic as long as no undefined operations (such as dereferencing it or performing pointer arithmetic on it) are taken on the pointer, as C lacks type-safe generics. Unless working with C libraries, C++ users should avoid void * and use templates instead, as compilers can detect usages of templates that are not type-safe.

Note that in standard C, you cannot perform arithmetic on a void *; the GNU C Compiler allows such arithmetic as a (non-portable) extension.

1365 questions
14
votes
1 answer

Delete vs operator delete (and void pointer)

Does delete ptr differ from operator delete(ptr) only in this, that delete calls ptr destructor? Or in other words, does delete ptr first call a destructor of ptr and then operator delete(ptr) to free allocated memory? Then is delete ptr technically…
mip
  • 8,355
  • 6
  • 53
  • 72
13
votes
3 answers

Usage of void pointers across different platforms

I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems? int x=100; int *pi=&x; printf("value of pi is:…
Naseer
  • 4,041
  • 9
  • 36
  • 72
12
votes
1 answer

What does "void *(*)(void *)" mean in C++?

It's the parameter in pthread_create(). I think each part means: void *: The return value is a void pointer. (*): It's a pointer to a function. (void *): It takes an untyped pointer as a parameter. Is that correct?
Marty
  • 5,926
  • 9
  • 53
  • 91
12
votes
3 answers

type casting integer to void*

#include void pass(void* ); int main() { int x; x = 10; pass((void*)x); return 0; } void pass(void* x) { int y = (int)x; printf("%d\n", y); } output: 10 my questions from the above code.. what happens when we…
user2282725
12
votes
4 answers

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast
DangerMouse
  • 704
  • 7
  • 20
12
votes
5 answers

C struct to void* pointer

I have a struct defined as: typedef struct { int type; void* info; } Data; and then i have several other structs that i want to assign to the void* using the following function: Data* insert_data(int t, void* s) { Data * d =…
Nitrate
  • 349
  • 1
  • 5
  • 13
11
votes
0 answers

C: Why is casting from void pointer to function pointer undefined?

Possible Duplicate: Why are function pointers and data pointers incompatible in C/C++? In the man page for dlsym, the following snippet has been provided. double (*cosine)(double); handle = dlopen("libm.so", RTLD_LAZY); /*…
Manav
  • 10,094
  • 6
  • 44
  • 51
11
votes
6 answers

Pointer to void in C++?

I'm reading some code in the Ogre3D implementation and I can't understand what a void * type variable means. What does a pointer to void mean in C++?
tunnuz
  • 23,338
  • 31
  • 90
  • 128
11
votes
8 answers

Replacing realloc (C --> C++)

In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++) However, I still have an issue with a similar function: I…
Biosci3c
  • 772
  • 5
  • 15
  • 35
11
votes
2 answers

Dereferencing void pointers

In the hope of gaining a better understanding of the answers given in this post, can someone please explain to me if the following circular buffer implementation is possible, and if not, why not. #define CB_TYPE_CHAR 0 #define CB_TYPE_FLOAT …
Zack
  • 463
  • 2
  • 7
  • 15
10
votes
1 answer

Why deleting void* is UB rather than compilation error?

Why would deleting an object through void* be undefined behavior, rather than compilation error? void foo(void* p) { delete p; } This code compiles and produces code, albeit with the warning on gcc and clang (surprisingly, ICC doesn't give a…
SergeyA
  • 61,605
  • 5
  • 78
  • 137
10
votes
5 answers

Is a conversion to `void *` required before converting a pointer to `uintptr_t` and vice versa?

Section 7.18.1.4 of the C99 standard states: The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
10
votes
4 answers

C->C++ Automatically cast void pointer into Type pointer in C++ in #define in case of type is not given (C-style) [MSVS]

Hi! I've used the following C macro, But in C++ it can't automatically cast void* to type*. #define MALLOC_SAFE(var, size) { \ var = malloc(size); \ if (!var) goto error; \ } I know, I can do something like this: #define…
zxcat
  • 2,054
  • 3
  • 26
  • 40
10
votes
7 answers

Find out Type of C++ Void Pointer

I have a small question: how do I find out what type a C++ pointer is? I often use a small function in my console programs to gather input, which looks something like this: void query(string what-to-ask, [insert datatype here] * input) I would like…
new123456
  • 873
  • 1
  • 12
  • 21
10
votes
4 answers

In C, Generic Containers or Safe Containers?

In C++ you can have both generic and type safe containers by using templates. However in C, if you want generic containers, you have to (afaik) use void*, which means you lose type safety. To have type safe containers, you would have to reimplement…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208