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
23
votes
1 answer

Type punning with void * without breaking the strict aliasing rule in C99

I recently came across the strict aliasing rule, but I'm having trouble understanding how to use void * to perform type punning without breaking the rule. I know this breaks the rule: int x = 0xDEADBEEF; short *y = (short *)&x; *y = 42; int z =…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
22
votes
3 answers

void pointers: difference between C and C++

I'm trying to understand the differences between C and C++ with regards to void pointers. the following compiles in C but not C++ (all compilations done with gcc/g++ -ansi -pedantic -Wall): int* p = malloc(sizeof(int)); Because malloc returns…
zaharpopov
  • 16,882
  • 23
  • 75
  • 93
21
votes
6 answers

C++. Error: void is not a pointer-to-object type

I have a C++ program: struct arguments { int a, b, c; arguments(): a(3), b(6), c(9) {} }; class test_class{ public: void *member_func(void *args){ arguments vars = (arguments *) (*args); //error: void is not a …
Matt Munson
  • 2,903
  • 5
  • 33
  • 52
21
votes
2 answers

Is there a way to cast shared_ptr to shared_ptr?

I want to keep the smart behavior of std::shared_ptr. So is there a way to cast a shared void pointer to another type while without confusing the reference counting? I can't get the raw pointer and create a new shared pointer from it.
danijar
  • 32,406
  • 45
  • 166
  • 297
21
votes
3 answers

Is ((void *) -1) a valid address?

Verbatim from Linux' man shmat: RETURN VALUE [...] on error (void *) -1 is returned, and errno is set to indicate the cause of the error. (POSIX tells the same using a slightly different wording.) Is there any mandatory rule or definition…
alk
  • 69,737
  • 10
  • 105
  • 255
20
votes
7 answers

How to make generic function using void * in c?

I have an incr function to increment the value by 1 I want to make it generic,because I don't want to make different functions for the same functionality. Suppose I want to increment int,float,char by 1 void incr(void *vp) { (*vp)++; } But…
Omkant
  • 9,018
  • 8
  • 39
  • 59
19
votes
5 answers

Mental model for void* and void**?

Note: I'm a experienced C++ programmer, so I don't need any pointer basics. It's just that I never worked with void** and have kind of a hard time getting my mental model adjusted to void* vs. void**. I am hoping someone can explain this in a good…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
18
votes
4 answers

Why is `boost::any` better than `void*`?

What inherent advantages do boost::any and boost::any_cast offer over using void* and dynamic_cast?
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
18
votes
2 answers

Is comparing two void pointers to different objects defined in C++?

Inspired by this answer about dynamic cast to void*: ... bool eqdc(B* b1, B *b2) { return dynamic_cast(b1) == dynamic_cast(b2); } ... int main() { DD *dd = new DD(); D1 *d1 = dynamic_cast(dd); D2 *d2 =…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
18
votes
6 answers

Is every null pointer constant a null pointer?

From the C17 draft (6.3.2.3 ¶3): An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.67) If a null pointer constant is converted to a pointer type, the resulting pointer,…
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
18
votes
4 answers

%p format specifier needs explicit cast to void* for all types but char* in printf

I've read a lot of answers about the %p format specifier usage in C language here in Stack Overflow, but none seems to give an explanation as to why explicit cast to void* is needed for all types but char*. I'm of course aware about the fact that…
programmersn
  • 582
  • 1
  • 3
  • 17
18
votes
6 answers

Explicit void pointer as function parameter

I have a function: int foo(void * ptr) { // ... } Can I syntactically (not with compiler warnings, etc.) in C++11/14 disable passing there pointers other than void * itself? For example, now it can be called like: foo(new int(42)); I need to…
vladon
  • 8,158
  • 2
  • 47
  • 91
18
votes
5 answers

C++, does bool conversion always fall back to implicit conversion to void*?

Question: Does implicit bool conversions always fall back to attempting implicit conversion to void*? (If such a conversion function exists for the type). If so, why? Consider the following short program: #include class Foo{ public: …
jensa
  • 2,792
  • 2
  • 21
  • 36
18
votes
4 answers

Casting void pointers

I've seen a lot of the following in older C code: type_t *x = (type_t *) malloc(...); What's the point of casting the pointer returned from malloc() since it's void *? Is it because older C compilers didn't support void pointers and malloc() used…
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
18
votes
2 answers

can void* be used to store function pointers?

void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? int send(); void* p = send; Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store…
Venkatesh
  • 1,537
  • 15
  • 28
1 2
3
90 91