void-pointers

Avoid incompatible pointer warning when dealing with double-indirection

Assuming this program: #include <stdio.h> #include <string.h> static void ring_pool_alloc(void **p, size_t n) { static unsigned char pool[256], i = 0; *p = &pool[i]; i += n; } int main(void) { char *str; ring_pool_alloc(&str, 7); strcpy(str, "foobar"); printf("%s\n", str); return 0; } ... is it pos...

How to get rid of void-pointers.

I inherited a big application that was originally written in C (but in the mean time a lot of C++ was also added to it). Because of historical reasons, the application contains a lot of void-pointers. Before you start to choke, let me explain why this was done. The application contains many different data structures, but they are stor...

Converting a void* to a std::string

After perusing the web and messing around myself, I can't seem to convert a void*'s target (which is a string) to a std::string. I've tried using sprintf(buffer, "%p", *((int *)point)); as recommended by this page to get to a C string, but to no avail. And sadly, yes, I have to use a void*, as that's what SDL uses in their USEREVENT stru...

What's the best way to make a bitwise function work for any type of integer input c++?

So I need to read flags in bits and set flags in bits. These bits are in various sizes of integer: int16, int32, int64, etc. I would like to have a function that does something like this: static integertype function(integertype data, char startbit, char endbit); I don't want to code what will be the same code to isolate bits from for...

[pointer] What does the expression "BIO *client = (BIO *)arg" mean?

Hi experts, Here is the context of the code: void THREAD_CC server_thread(void *arg) { BIO *client = (BIO *)arg; ... } Does the expression (BIO *)arg transform the void pointer arg into a pointer that points to BIO? I'm not sure if I got this right or not. Any help would be much appreciated! Z.Zen ...

How to convert a void* to a type that can be used in C#? Interoperability between C DLL and C#

Hi, I am a C/C++ programmer, but I was asked to update a program that was written in C# to communicate with a device. My knowledge of C# is very basic. The previous version was totally written in C#, but now the API that in fact access the device was changed to C. I found out that I can import the C function APIs by using: [DllImport(...

How to make a void** point to a function?

I have code that looks like this: extern "C" __declspec(dllexport) myInterface(int id, void** pFunction) { ... } I need to make the void** pFunction argument point to a function so that the caller can use this function via the pFunction pointer. This function gets called through a DLL, I don't want to do it this way but for a lot ...

Error Performing Pointer Arithmetic on void * in MSVC

Error 1 error C2036: 'const void *' : unknown size file.cpp 111 I don't follow. GCC never complains about void * pointer arithmetic, even on -ansi -pedantic -Wall. What's the problem? Here's the code- struct MyStruct { const void *buf; // Pointer to buffer const void *bufpos; // Pointer to current posi...

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 to return char * instead? ...

Interesting problem on pointers..Please help

#include<iostream> #include<conio.h> using namespace std; int main() { int x = 65; int *ptr = &x; char * a= (char *)ptr; cout<<(int)*(a); getch();return 0; } Sixeof(ptr) and Sizeof(a) display 4 Sizeof(int) displays 4 and sizeof(char) displays 1 So 65 is stored in 4 byte...

Generic editable functions in C using void*

Hi folks, I fall in some problem. I need to write some function like memcpy(void*, const void*), which its signature should be: void arrayCopy(void *dest, int dIndex, const void *src, int sIndex, int len) I noticed that, in many implementation of memcpy, we cast void* to char*, but I think this is not the case of me, as the arrayCop...

Porting Issue: Pointer with offset in VC++

Ok, this compiles fine in GCC under Linux. char * _v3_get_msg_string(void *offset, uint16_t *len) {/*{{{*/ char *s; memcpy(len, offset, 2); *len = ntohs(*len); s = malloc(*len+1); memset(s, 0, *len+1); memcpy(s, offset+2, *len); s[*len] = '\0'; *len+=2; return s; }/*}}}*/ However, I'm having a prob...

Test for void pointer in C++ before deleting

I have an array in C++: Player ** playerArray; which is initialized in the constructor of the class it is in. In the destructor I have: delete playerArray; except when testing the program through Valgrind it says that there are some calls to delete to a void pointer: operator delete(void*) I want to test whether the playerArr...

Relax void * casting in C++

In C, it's not an error to cast pointers to and from void *. A major obstacle in porting to C++ is the need to cast pointers when returning from functions dealing with generic pointers such as malloc, and functions declared in my own code such as void *block_get(Blkno const blkno);. My code however is intended to be compiled by C and C...

sizeof void pointer

why is sizeof void pointer 2 ? ...

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 changed: tmp = malloc(sizeof(char*) * mtmp); --> tmp = new char*[mtmp]; and free(tm...

void * assignment problem

I want to take some fields from packet struct using pointer arithmetic.But what is wrong with the code below ? In first condition i think if i go 4 byte(2 short field) from beginning of packet i get tLow .But it does not give expected value.Additionally second case i want to get data field by going 12 byte from beginning of packet .What ...

Casting from void* to an object array in c++

Hello I'm having problems getting this to work, class A { public: A(int n) { a = n; } int getA() { return a; } private: int a; }; int main(){ A* a[3]; A* b[3]; for (int i = 0; i < 3; ++i) { a[i] = new A(i + 1); } void * pointer = a; b = (A* [])pointer; // DOESNT...

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 because the memory was freed, how can I debug when it gets freed? I hope my questions ar...

in c can I do arithmetic on void * pointers

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 topic EDIT: My little test app runs fine on gcc 4.1.1 and treats void * as char *. But ...