void-pointers

Dereferencing void pointers

纵然是瞬间 提交于 2019-12-18 20:02:14
问题 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

Is there a way to cast shared_ptr<void> to shared_ptr<T>?

余生长醉 提交于 2019-12-18 14:14:31
问题 I want to keep the smart behavior of std::shared_ptr . So is there a way to cast a shared void pointer to another type while without confusing the reference counting? I can't get the raw pointer and create a new shared pointer from it. 回答1: You can use the pointer casts from rob mayoff's answer; but be careful. It is easy to unintentionally trigger undefined behavior here: struct MyClass {}; void* rawPtr = new MyClass; shared_ptr<void> exampleVoid(rawPtr); // Undefined behavior; // calls

sizeof void pointer

青春壹個敷衍的年華 提交于 2019-12-18 10:54:08
问题 why is sizeof void pointer 2 ? 回答1: The size of a void* is a platform dependent value. Typically it's value is 4 or 8 bytes for 32 and 64 bit platforms respectively. If you are getting 2 as the value then your likely running on a 16 bit coding platform (or potentially have a coding error). Could you post the code you are using and some more information about your environment / operating system? 回答2: Per the online C standard (n1256 draft): 6.2.5 Types ... 27 A pointer to void shall have the

access element of struct passed into a void* pointer

邮差的信 提交于 2019-12-18 07:02:36
问题 I'm working with a binary search tree data structure to sort a series of structs with the type definitions: typedef struct { char c; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t *right; } The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function: static void *recursive_search_tree(node_t *root, void *key, int cmp(void*,void*)) Within the

access element of struct passed into a void* pointer

烈酒焚心 提交于 2019-12-18 07:01:05
问题 I'm working with a binary search tree data structure to sort a series of structs with the type definitions: typedef struct { char c; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t *right; } The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function: static void *recursive_search_tree(node_t *root, void *key, int cmp(void*,void*)) Within the

How to make generic function using void * in c?

泪湿孤枕 提交于 2019-12-17 23:05:33
问题 I have an incr function to increment the value by 1 I want to make it generic,because I don't want to make different functions for the same functionality. Suppose I want to increment int , float , char by 1 void incr(void *vp) { (*vp)++; } But the problem I know is Dereferencing a void pointer is undefined behaviour . Sometimes It may give error : Invalid use of void expression . My main funciton is : int main() { int i=5; float f=5.6f; char c='a'; incr(&i); incr(&f); incr(&c); return 0; }

Converting double to void* in C

…衆ロ難τιáo~ 提交于 2019-12-17 20:11:15
问题 I'm writing an interpreter and I'd like to be able to store whatever value a function returns into a void pointer. I've had no problem storing ints and various pointers as void pointers but I get an error when trying to cast a double as a void pointer. I understand that doubles are stored differently than integers and pointers at the bit level, but I don't understand why I can't place whatever bits I want into the pointer (assuming it has enough memory allocated) and then take them out later,

Casting void pointers

倾然丶 夕夏残阳落幕 提交于 2019-12-17 09:43:50
问题 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? 回答1: Your own explanation is the right one. Pre-ANSI C ('K&R' C) did not have a void * type with implicit conversion. char * doubled as a pseudo void * type, but you needed the explicit conversion of a type cast. In modern

What is a void pointer and what is a null pointer?

懵懂的女人 提交于 2019-12-17 06:29:58
问题 So I was going through some interview questions and I came across one about void and null pointers, which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right. Please express your views as

Using intptr_t instead of void*?

﹥>﹥吖頭↗ 提交于 2019-12-17 06:09:09
问题 Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void* ? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html) For what I've already read: int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do pointer arithmetics on both void* and intptr_t require casts, so none gets advantage here void* means less explicit casts when