void-pointers

Casting to void* and Back to Original_Data_Type*

眉间皱痕 提交于 2019-12-04 04:15:34
问题 I have seen and used this many times is C++, specially in various thread implementations. What I wonder is if there are any pitfalls/ issues of doing this? Is there any way that we could run in to an error or undefined condition when we are casting to void* and back again? How should we resolve such issues if there are any? Thank you. 回答1: What I wonder is if there are any pitfalls/ issues of doing this? You need to be absolutely sure while casting the the void* back to the particular type,

Disadvantages of using void* pointers in C

a 夏天 提交于 2019-12-03 22:00:24
There are many drawbacks to using void * in C (memory related, type related, efficiency wise ...). In spite of them we use them a lot for the flexibility they provide. List the disadvantages/drawbacks using void * (and preferred solution in C - if possible). EDIT: please go through the follwoing link: http://attractivechaos.wordpress.com/2008/10/02/using-void-in-generic-c-programming-may-be-inefficient/ There are no efficiency issues with void pointers. The only limitations with void pointers are: you cannot dereference void pointer for obvious reasons sizeof(void) is illegal you cannot

Why is `typedef void * COMPLEX` used in this interface?

痴心易碎 提交于 2019-12-03 21:51:00
I have a program and I can't understant how it works. Here is a part of it. I don't understand the line typedef void *COMPLEX , the command this and why the struct COMPLEX_IMPL is being used. #ifndef _COMPLEX_H #define _COMPLEX_H typedef void *COMPLEX; COMPLEX NewCOMPLEX (double a, double b ); void DeleteCOMPLEX(COMPLEX this ); double GetA (COMPLEX this ); double GetB (COMPLEX this ); COMPLEX AddComplex (COMPLEX c1, COMPLEX c2, COMPLEX res); COMPLEX MultComplex (COMPLEX c1, COMPLEX c2, COMPLEX res); #endif /* _COMPLEX_H */ #ifndef _COMPLEX_H #define _COMPLEX_H typedef void *COMPLEX; COMPLEX

If I have a void pointer, how do I put an int into it?

扶醉桌前 提交于 2019-12-03 09:17:09
问题 I have an array of arbitrary values, so I have defined it as an array of void pointers, so I can point to any kind of information (like int , character arrays, etc). However, how do I actually assign an int to it? Take for example these initializations: void* data[10]; int x = 100; My intuition would think this, but this gives a compile error: data[0] = malloc(sizeof(int)); *(data[0]) = x; Also I thought about using &x , but I would take the address of a local variable, which (to my

Objective-C variable… pointing to itself?

喜夏-厌秋 提交于 2019-12-03 08:58:55
问题 I spotted this construct in some of Apple's example code for dealing with key-value observing. When adding an observer, you can add a context (in the form of a void* variable) that can uniquely identify the KVO call - particularly useful if you want multiple KVO calls to trigger the same action, as the single context can avoid using a bunch of chained or statements to check all the possibilities. This is the line that's used to declare the variable used for the context: static void *aContext

Printing a void* variable in C

南笙酒味 提交于 2019-12-03 08:44:31
问题 Hi all I want to do a debug with printf. But I don't know how to print the "out" variable. Before the return, I want to print this value, but its type is void* . int hexstr2raw(char *in, void *out) { char c; uint32_t i = 0; uint8_t *b = (uint8_t*) out; while ((c = in[i]) != '\0') { uint8_t v; if (c >= '0' && c <= '9') { v = c - '0'; } else if (c >= 'A' && c <= 'F') { v = 10 + c - 'A'; } else if (c >= 'a' || c <= 'f') { v = 10 + c - 'a'; } else { return -1; } if (i%2 == 0) { b[i/2] = (v << 4);

(void*) casting- what is this used for?

江枫思渺然 提交于 2019-12-03 07:29:31
I did try searching for this on SO but I think due to the syntax and not knowing exactly what to search I became a little unstuck. I have seen (void*) being used to cast, usually to function calls. What is this used for? void* , usually referred to as a void pointer , is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful when writing functions that handle multiple data types. Void pointers are more useful with C than C++. You

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

Deadly 提交于 2019-12-03 07:20:43
问题 What inherent advantages do boost::any and boost::any_cast offer over using void* and dynamic_cast ? 回答1: The advantage is that boost::any is way more type-safe than void* . E.g. int i = 5; void* p = &i; static_cast<double*>(p); //Compiler doesn't complain. Undefined Behavior. boost::any a; a = i; boost::any_cast<double>(a); //throws, which is good As to your comment, you cannot dynamic_cast from a void* . You can dynamic_cast only from pointers and references to class types which have at

c: size of void*

只谈情不闲聊 提交于 2019-12-03 06:47:56
问题 I'm a bit confused with a void* pointer in C. Especially after reading this question: Is the sizeof(some pointer) always equal to four?, where one person says there is no guarantee that sizeof(int *) == sizeof(double *) My question is: is there a guarantee of sizeof(void*) >= sizeof(any other pointer type)? In other words, can I always assign a some_type* pointer to a void* pointer and then get it back as some_type*? 回答1: Only data pointers. void * can hold any data pointer, but not function

Why is it impossible to have a reference-to-void?

橙三吉。 提交于 2019-12-03 04:32:46
问题 Why is it impossible to have a reference to void? The only thing I found in the C++ Standard is this line, at 8.3.2.1 A declarator that specifies the type "reference to cv void" is ill-formed. Why is it that way? Why can't I write a "generic" function that accept a void& ? Just to be clear, I have no useful application in mind where using a reference-to-void could be better than using templates, but I'm just curious about the rationale for forbidding this construct. To clarify a little, I