void-pointers

Passing Void type parameter in C

Hello there I am working on an assignment in C where I need to pass in an unknown type of parameter into a function. For example suppose I have the following: int changeCount(void* element) { element.Count = element.Count++; return 1; } The reason why variable element is void is because there are 3 types of possibilities. ...

casting via void* instead of using reinterpret_cast

Hi, 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<T2*>(pv); instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can't find an explanation why is this better than the dire...

void has unknown size in Visual C++

In Visual Studio C++ version 9 (and probably other versions too), the following code: int a = sizeof(void); void const *b = static_cast<void const *>("hello world"); b += 6; Generates these errors: error C2070: 'void': illegal sizeof operand error C2036: 'const void *' : unknown size This code works under GCC, which treats sizeof(v...

Byte precision pointer arithmetic in C when sizeof(char) != 1

How can one portably perform pointer arithmetic with single byte precision? Keep in mind that: char is not 1 byte on all platforms sizeof(void) == 1 is only available as an extension in GCC While some platforms may have pointer deref pointer alignment restrictions, arithmetic may still require a finer granularity than the size of the ...

Contructing python function callable from C , with input parameter having *output* sematics

The use case is the following: Given a (fixed, not changeable) DLL implemented in C Wanted: a wrapper to this DLL implemented in python (chosen method: ctypes) Some of the functions in the DLL need synchronization primitives. To aim for maximum flexibility, the designers of the DLL completely rely on client-provided callbacks. More p...

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 stuck into C, I'm aware that I have to make a compromise and I'd like it to be the right one...

Struct instantiation from void pointer buffer

Here's some C++ code that just looks funny to me, but I know it works. There is a struct defined, and in the program we allocate memory using a void pointer. Then the struct is created using the allocated buffer. Here's some code typedef struct{ char buffer[1024]; } MyStruct int main() { MyStruct* mystruct_ptr = 0; void* ...

Is casting an integer value to a void* an often used paradigm in callbacks?

Rather than sending an actual pointer to a value, the value is cast to a pointer. I found these examples in the GUI interface code of a GTK program. g_signal_connect (pastebutton[pane], "clicked", G_CALLBACK(on_paste_button_pressed), (void*)((long)pane<<4)); In the above example, I...

C++: casting to void* and back

* ---Edit - now the whole sourse* When I debug it on the end, "get" and "value" have different values! Probably, I convert to void* and back to User the wrong way? #include <db_cxx.h> #include <stdio.h> struct User{ User(){} int name; int town; User(int a){}; inline int get_index(int a){ return town; } //for another stuff }; int m...

Pointers to void pointers in C - can I use void** for rudimentary polymorphism?

I can understand how a void** might look in memory, but I'm wondering if I'm using it quite right. Are there any fundamental flaws in what I describe below? For example, although I can say "it works for me", am I creating bad / unportable code in some way? So I have an Asteroids clone. There are three entities that can fire bullets, the...

Ncurses User Pointer

I'm trying to learn ncurses, and I'm reading the terrific guide here, but the example at user pointers does not compile. I get this error when I try to compile. menu.cpp: In function 'int main()': menu.cpp:44: error: invalid conversion from 'void (*)(char*)' to 'void*' menu.cpp:44: error: initializing argument 2 of 'int set_item_use...

GLib atoms and memory chunks

The following code snippet is from The Official GNOME 2 Developer's Guide: GMemChunk my_chunk; my_chunk = g_mem_chunk_new("My Chunk", 42, 42*16, G_ALLOC_AND_FREE); gchar *data[50000]; gint i; /* allocate 40,000 atoms */ for(i = 0; i < 40000; i++) { data...

multiple inheritance: unexpected result after cast from void * to 2nd base class

My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting these void* in case of classes with multiple base classes fails and even crashes my program af...

A few questions about my modular code using void* as dynamic data type in C

Hi, A few days ago I posted this question and everyone suggested me to use void*, which I did. I think some of them also pointed a few things that I would need to take care of but I'm not sure what exactly were they. However, I'm having a few problems with this... I'm not going to post all my code where cause it's quite large, instead,...

What is the correct way to cast when using ATL and IUnknownPtr?

During the modification of an existing ATL COM object I came across an article from the "The Old New Thing" blog called "The ways people mess up IUnknown::QueryInterface" and there was a discussion in the comments section that started when one of the respondents (Norman Diamond) pointed out that that in one of the article's examples that...

Equivalent to window.setTimeout() for C++

In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ; which will asynchronously invoke func after 1000 ms. I want to do something similar in C++ (without multithreading), so I put together a sample loop like: #include <stdio.h> struct Callback { // The _time_ this function will be execut...

Problem using void pointer as a function argument

Hi, I can't understand this result... The code: void foo(void * key, size_t key_sz) { HashItem *item = malloc(sizeof(HashItem)); printf("[%d]\n", (int)key); ... item->key = malloc(key_sz); memcpy(item->key, key, key_sz); } void bar(int num) { foo(&num, sizeof(int)); } And I do this call: bar(900011009); ...

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? ...

Void pointers in C

I have written this qsort: void qsort(void *a[],int low,int high, int (*compare)(void*,void*)); When I call this on char *strarr[5]; It says invalid conversion from char** to void**. Why this is wrong? This is the code: #include<cstdlib> #include<cstdio> #include<iostream> using namespace std; inline void strswap(void *a,void *...

Issue with dynamic array Queue data structure with void pointer

Hi, Maybe there's no way to solve this the way I'd like it but I don't know everything so I better ask... I've implemented a simple Queue with a dynamic array so the user can initialize with whatever number of items it wants. I'm also trying to use a void pointer as to allow any data type, but that's the problem. Here's my code: type...