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
238
votes
10 answers

Pointer arithmetic for void pointer in C

When a pointer to a particular type (say int, char, float, ..) is incremented, its value is increased by the size of that data type. If a void pointer which points to data of size x is incremented, how does it get to point x bytes ahead? How does…
Siva Sankaran
  • 2,407
  • 2
  • 15
  • 4
202
votes
13 answers

C isn't that hard: void ( *( *f[] ) () ) ()

I just saw a picture today and think I'd appreciate explanations. So here is the picture: Transcription: "C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions…
Motun
  • 2,149
  • 3
  • 16
  • 23
172
votes
7 answers

Objective-C: difference between id and void *

What is the difference between id and void *?
mk12
  • 25,873
  • 32
  • 98
  • 137
138
votes
16 answers

Concept of void pointer in C programming

Is it possible to dereference a void pointer without type-casting in the C programming language? Also, is there any way of generalizing a function which can receive a pointer and store it in a void pointer and by using that void pointer, can we make…
AGeek
  • 5,165
  • 16
  • 56
  • 72
115
votes
4 answers

What is a void pointer in C++?

Possible Duplicate: What is a void pointer and what is a null pointer? I often see code which resembles something like the following: void * foo(int bar); What does this mean? Does it mean that it can return anything? Is this similar to dynamic…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
102
votes
13 answers

Is it safe to delete a void pointer?

Suppose I have the following code: void* my_alloc (size_t size) { return new char [size]; } void my_free (void* ptr) { delete [] ptr; } Is this safe? Or must ptr be cast to char* prior to deletion?
An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
79
votes
17 answers

error: cast from 'void*' to 'int' loses precision

I have a function with prototype void* myFcn(void* arg) which is used as the starting point for a pthread. I need to convert the argument to an int for later use: int x = (int)arg; The compiler (GCC version 4.2.4) returns the error: file.cpp:233:…
Joshua D. Boyd
  • 4,808
  • 3
  • 29
  • 44
55
votes
10 answers

Do all pointers have the same size in C++?

Recently, I came across the following statement: It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes. But then I came across this which states that: While pointers are…
Jason
  • 36,170
  • 5
  • 26
  • 60
55
votes
5 answers

What does "typedef void (*Something)()" mean

I am trying to understand what this means, the code I am looking at has in .h typedef void (*MCB)(); static MCB m_process; in .C MCB Modes::m_process = NULL; And sometimes when I do m_process(); I get segmentations fault, it's probably…
DogDog
  • 4,820
  • 12
  • 44
  • 66
53
votes
10 answers

Why is it impossible to have a reference-to-void?

Why is it impossible to have a reference to void? The only thing I found in the C++ Standard is this line, at 8.3.2.1 A declarator that specifies the type "reference to cv void" is ill-formed. Why is it that way? Why can't I write a "generic"…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
46
votes
9 answers

What is a void pointer and what is a null pointer?

So I was going through some interview questions and I came across one about void and null pointers, which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void…
Shouvik
  • 11,350
  • 16
  • 58
  • 89
46
votes
6 answers

dynamic_cast from "void *"

According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please clarify the issue.
dimba
  • 26,717
  • 34
  • 141
  • 196
42
votes
4 answers

Is there a way to avoid implicit conversion to void*?

I'm using an API that accepts void* in certain functions. I frequently accidentally pass the wrong pointer type to the function, and of course it compiles fine, but doesn't work at runtime. Is there a way to disable implicit conversion to void* for…
AILien
  • 832
  • 4
  • 11
37
votes
3 answers

casting via void* instead of using reinterpret_cast

I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast: T1 * p1=... void *pv=p1; T2 * p2= static_cast(pv); Instead of: T1 * p1=... T2 * p2=…
sinek
  • 2,458
  • 3
  • 33
  • 55
36
votes
2 answers

c: size of void*

I'm a bit confused with a void* pointer in C. Especially after reading this question: Is the sizeof(some pointer) always equal to four?, where one person says there is no guarantee that sizeof(int *) == sizeof(double *) My question is: is there a…
facha
  • 11,862
  • 14
  • 59
  • 82
1
2 3
90 91