void-pointers

Casting void pointers, depending on data (C++)

喜夏-厌秋 提交于 2019-12-23 05:47:11
问题 Basically what I want to do is, depending on the some variable, to cast a void pointer into a different datatype. For example (the 'cast' variable is just something in order to get my point across): void* ptr = some data; int temp = some data; int i = 0; ... if(temp == 32) cast = (uint32*) else if(temp == 16) cast = (uint16*) else cast = (uint8*) i = someArray[*((cast)ptr)]; Is there anything in C++ that can do something like this (since you can't actually assign a variable to be just (uint32

Equivalent to window.setTimeout() for C++

久未见 提交于 2019-12-22 04:55:33
问题 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 executed. double execTime ; // The function to execute after execTime has passed void* func ; } ; // Sample function to execute void go() { puts( "GO" ) ; } // Global program-wide

Replacing realloc (C --> C++)

六眼飞鱼酱① 提交于 2019-12-22 03:42:44
问题 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(tmp) --> delete [] tmp; However, what do I do with realloc in the following function: char* space_getRndPlanet (void) { int i,j; char **tmp; int ntmp; int mtmp; char

Error Performing Pointer Arithmetic on void * in MSVC

一曲冷凌霜 提交于 2019-12-21 05:18:29
问题 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 position in buffer }; ... size_t someSize_t, anotherSize_t; MyStruct *myStruct = (MyStruct *) userdata; ... if ( (myStruct->bufpos + someSize_t) > (myStruct->buf + anotherSize_t) ) { // Error on

How to use void* as a single variable holder? (Ex. void* raw=SomeClass() )

北城以北 提交于 2019-12-20 06:38:54
问题 I am trying to make void* to hold a value (to avoid default constructor calling). I want to:- copy K to void* e.g. K k1; --> void* raw=k1; copy void* to K e.g. void* raw; --> K k2=raw; try not to break destructor and causes memory leak don't use any dynamic allocation (heap, performance reason) Here is what I tried:- class K{ public: std::string yes="yes" ; }; int main() { //objective: k1->raw->k2 , then delete "raw" void* raw[sizeof(K)]; //<--- try to avoid heap allocation K k1; static_cast

Recasting *void function arguments

好久不见. 提交于 2019-12-20 04:07:29
问题 I posted a question here earlier that I think I can answer if someone can help me with the following: I have a function double func(void* data) I want to pass in an object or struct. (In my case an armadillo matrix or even just and std::vector). How do I pass a pointer to an object as an argument to func() and then, once inside func(), how do I recast the void pointer into its original type? Edit : Here's what ended up working, where mat is the Armadillo matrix class: mat A(2,2); A << 1 << 2

Bubble sort universal implementation in C

耗尽温柔 提交于 2019-12-20 02:26:22
问题 I'm trying to make an universal bubble sort function. It allow to user to write its own compare and swap function. I implemented a swap and compare function for int type, but when I run the code for next array: {3, 5, 8, 9, 1, 2, 4, 7, 6, 0} , I get: 0 0 84214528 2312 1 2 4 7 6 0. Why this is happening? #include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 int compInt(void *a, void *b) // FUNCTION FOR COMPARE INT { if (*(int*)(a) > *(int*)(b)) { return false; } // IF FIRST INT

C# and void pointers

风流意气都作罢 提交于 2019-12-19 18:47:41
问题 I am writing my first C# application, but as luck would have it I must use void pointers (working with a DLL which returns handles). From what I read there are a few options: Unsafe code, for example see http://www.c-sharpcorner.com/UploadFile/gregory_popek/WritingUnsafeCode11102005040251AM/WritingUnsafeCode.aspx In the C# program writing in my function declerations IntPtr instead of void*. e.g.: public static extern char SupportsWhatever(IntPtr h); Using ref, e.g.: public static extern char

Explicit cast required to pointer to void pointer [duplicate]

徘徊边缘 提交于 2019-12-19 11:15:32
问题 This question already has answers here : incompatible pointer type in C (3 answers) Closed 5 years ago . I have the following function signature int foo(void **) and am trying to give it a pointer to a char pointer, i.e. char ** . My compiler is complaining with the following warning argument of type "char **" is incompatible with parameter of type "void **" Is this to be expected? I know passing a char * to a function expecting a void * requires no explicit cast but is this true for pointers

How to check if a void* pointer can be safely cast to something else?

若如初见. 提交于 2019-12-19 09:09:49
问题 Let's say I have this function, which is part of some gui toolkit: typedef struct _My_Struct My_Struct; /* struct ... */ void paint_handler( void* data ) { if ( IS_MY_STRUCT(data) ) /* <-- can I do something like this? */ { My_Struct* str = (My_Struct*) data; } } /* in main() */ My_Struct s; signal_connect( SIGNAL_PAINT, &paint_handler, (void*) &s ); /* sent s as a void* */ Since the paint_handler will also be called by the GUI toolkit's main loop with other arguments, I cannot always be sure