void-pointers

C# and void pointers

你说的曾经没有我的故事 提交于 2019-12-01 16:39: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 SupportsWhatever(ref h); It should also be noted that I need to marshal information back and forth

How to access members through a void pointer

回眸只為那壹抹淺笑 提交于 2019-12-01 11:36:48
问题 Started by trying to write a small program to translate basic arithmetic into English, I end up building a binary tree(which is inevitably very unbalanced) to represent the order of evaluations. First, I wrote struct expr; typedef struct{ unsigned char entity_flag; /*positive when the oprd struct represents an entity ---a single digit or a parenthesized block*/ char oprt; expr * l_oprd;// these two point to the children nodes expr * r_oprd; } expr; However, to efficiently represent single

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

有些话、适合烂在心里 提交于 2019-12-01 06:12:38
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 that the parameter I am receiving will always be a pointer to s . Can I do something like IS_MY_STRUCT

Why can't static_cast a double void pointer?

会有一股神秘感。 提交于 2019-12-01 05:16:05
Consider the following piece of code: void **v_dptr(nullptr); int **i_dptr = static_cast<int**>(v_dptr); The above example produces the following compile error: static_cast from 'void **' to 'int **' is not allowed LIVE DEMO I know that the proper way to cast a void pointer to any other pointer type is to use static_cast . However, you can't static_cast a double void pointer to another double pointer of other type. Q: Why we can't static_cast a double void pointer? What's the proper way to cast a double void pointer? When you have a void* and cast it to an int* , there may or may not be some

static_cast'd pointer value

孤街醉人 提交于 2019-12-01 03:33:26
In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a , and there is an object b of type T (ignoring cv

Why can't static_cast a double void pointer?

好久不见. 提交于 2019-12-01 02:20:50
问题 Consider the following piece of code: void **v_dptr(nullptr); int **i_dptr = static_cast<int**>(v_dptr); The above example produces the following compile error: static_cast from 'void **' to 'int **' is not allowed LIVE DEMO I know that the proper way to cast a void pointer to any other pointer type is to use static_cast . However, you can't static_cast a double void pointer to another double pointer of other type. Q: Why we can't static_cast a double void pointer? What's the proper way to

static_cast'd pointer value

喜夏-厌秋 提交于 2019-12-01 00:54:51
问题 In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the

Using void pointer to an array

半腔热情 提交于 2019-11-30 21:55:44
I was just trying to use a void pointer to an integer array ,I tried to see if i can print the array back by casting it back into int. But it is giving me some random value. Can you tell me where i am going wrong? #include<stdio.h> #include<stdlib.h> int main(){ int a[5]; int x; int j; a[0]=1; a[1]=2; a[2]=3; a[3]=4; void *arr=a; for(j=0;j<4;j++){ x = *(int *)(arr+j); printf("%d",x); } return 0; } Output is this: 133554432131072512 Why is it not pinting elements of array a[] i.e 1,2,3,4 ? You need to cast arr before adding j . Here is a minimal fix: x = *(((int *)arr)+j); but I think it's

Confusion in ternary operator and typecasting

爷,独闯天下 提交于 2019-11-30 20:20:31
I gone through this question - why the result of : 1 ? (int *)0 : (void *)0 differs to the result of : 1 ? (int *)0 : (void *)1 How it is differ ? It should be 0 or (int*)0 . How to check the result ? Where we can use such type of expression ? effeffe The only difference is in types: the first one returns an int * , the second one returns a void * . From C11 standard, §6.5.15 Conditional operator , ¶6: If both the second and third operands are pointers or one is a null pointer constant and the other is a pointer, the result type is a pointer to a type qualified with all the type qualifiers of

Dereferencing void pointers

青春壹個敷衍的年華 提交于 2019-11-30 18:35:23
In the hope of gaining a better understanding of the answers given in this post, can someone please explain to me if the following circular buffer implementation is possible, and if not, why not. #define CB_TYPE_CHAR 0 #define CB_TYPE_FLOAT 1 ... typedef struct CBUFF { uint16 total; /* Total number of array elements */ uint16 size; /* Size of each array element */ uint16 type; /* Array element type */ uint16 used; /* Number of array elements in use */ uint16 start; /* Array index of first unread element */ void *elements; /* Pointer to array of elements */ } CBUFF; ... void cbRead(CBUFF