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
18
votes
11 answers

Genericity vs type-safety? Using void* in C

Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get…
Joe
  • 46,419
  • 33
  • 155
  • 245
18
votes
2 answers

void* vs. char* pointer arithmetic

I'm looking through my textbook and I'm a little confused about some of the code in there. In one part, they are performing pointer arithmetic in the following way: void* bp; ... bp = (void*)((char*)(bp)+16); ... But later on, they do the…
de1337ed
  • 3,113
  • 12
  • 37
  • 55
17
votes
6 answers

array of type void

plain C have nice feature - void type pointers, which can be used as pointer to any data type. But, assume I have following struct: struct token { int type; void *value; }; where value field may point to char array, or to int, or something…
S.J.
  • 1,213
  • 2
  • 12
  • 15
17
votes
5 answers

How to make compiler not show int to void pointer cast warnings

I have some code that does lots of casting from int to void* and vice-versa (i don't care if it's ugly. I like having generic stuff) Example: typedef struct _List { long size; long mSize; // Max size void** elementArray; }List; List…
Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30
16
votes
6 answers

Can I do arithmetic on void * pointers in C?

is this valid void *p = &X; /* some thing */ p += 12; and if so what does p now point to? I have (third party) code that does this (and compiles cleanly) and my guess is that the void * was treated as a char *. My trusty K&R is silent(ish) on the…
pm100
  • 48,078
  • 23
  • 82
  • 145
16
votes
2 answers

Objective-C variable... pointing to itself?

I spotted this construct in some of Apple's example code for dealing with key-value observing. When adding an observer, you can add a context (in the form of a void* variable) that can uniquely identify the KVO call - particularly useful if you want…
jstm88
  • 3,335
  • 4
  • 38
  • 55
16
votes
2 answers

How to safely store an id object in a C++ void* member under ARC when no other references hold on to the object?

I'm working with Box2D (C++) and I create an Objective-C object and assign it to a Box2D body's userData property, which is of type void*. Now in some cases the void* userData may be the only active reference to that ObjC object. Therefore, because…
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
16
votes
6 answers

Does a pointer point to the LSB or MSB?

if I have the following code: int i = 5; void * ptr = &i; printf("%p", ptr); Will I get the LSB address of i, or the MSB? Will it act differently between platforms? Is there a difference here between C and C++?
elyashiv
  • 3,623
  • 2
  • 29
  • 52
15
votes
1 answer

Is interpreting a pointer to first member as the class itself well defined?

I have some code that look like this: template struct memory_block { // Very not copiable, this class cannot move memory_block(memory_block const&) = delete; memory_block(memory_block const&&) = delete; …
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
15
votes
3 answers

c++ convert string into void pointer

I use a library that have a callback function where one of the parameters is of type void *. (I suppose to let to send value of any type.) I need to pass a string (std::string or a char[] is the same). How can I do this?
ghiboz
  • 7,863
  • 21
  • 85
  • 131
14
votes
2 answers

Why operator void*() conversion function added to the C++ stream classes?

There is a conversion function operator void*() const in C++ stream classes. so that all stream objects can be implicitly converted to void*. During the interaction with programmers on SO they suggest me to don't use void* unless you've a good…
Destructor
  • 14,123
  • 11
  • 61
  • 126
14
votes
2 answers

Cast a Swift struct to UnsafeMutablePointer

Is there a way to cast a Swift struct's address to a void UnsafeMutablePointer? I tried this without success: struct TheStruct { var a:Int = 0 } var myStruct = TheStruct() var address = UnsafeMutablePointer(&myStruct) Thanks! EDIT: the…
popisar
  • 379
  • 2
  • 3
  • 15
14
votes
4 answers

C: Extrapolating type from void pointer

Say a function takes a void pointer as an argument, like so: int func(void *p); How can we determine or guess the type of what p is pointing to?
Yktula
  • 14,179
  • 14
  • 48
  • 71
14
votes
5 answers

If I have a void pointer, how do I put an int into it?

I have an array of arbitrary values, so I have defined it as an array of void pointers, so I can point to any kind of information (like int, character arrays, etc). However, how do I actually assign an int to it? Take for example these…
pbean
  • 727
  • 1
  • 10
  • 20
14
votes
1 answer

How to cast blocks to and from void *

So, I'm trying to pass a block as an NSAlert contextInfo parameter. [myAlert beginSheetModalForWindow: theWindow modalDelegate: myAlert didEndSelector: @selector(alertDidEnd:returnCode:contextInfo:) …
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88