void-pointers

How dangerous is conversion from void ** to char **

巧了我就是萌 提交于 2019-12-07 04:04:01
问题 So we know that the standard doesn't force pointer sizes to be equal. (here and here) (and not talking about function pointers) I was wondering how in reality that can be an issue. We know that void * can hold anything, so if the pointer sizes are different, that would be the biggest size. Given that, assigning a void ** to a char ** means trouble. My question is how dangerous would it be to assume void * and char * have the same size? Is there actually an architecture where this is not true?

void* is literally float, how to cast?

て烟熏妆下的殇ゞ 提交于 2019-12-07 01:49:02
问题 So I'm using this C library in my C++ app, and one of the functions returns a void*. Now I'm not the sharpest with pure C, but have heard that a void* can be cast to pretty much any other *-type. I also know that I expect a float at the end somewhere from this function. So I cast the void* to a float* and dereference the float*, crash. darn!. I debug the code and in gdb let it evaluate (float)voidPtr and low and behold the value is what I expect and need! But wait, it's impossible to the same

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

拜拜、爱过 提交于 2019-12-06 20:20:34
问题 I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please could someone explain why a reinterpret_cast is needed to convert a pointer to Fred* to a pointer to void* but static_cast is fine to convert pointer to Fred to a pointer to void . 回答1: There's no requirement that a Fred* and a void* have the same size

Why `void* = 0` and `void* = nullptr` makes the difference?

試著忘記壹切 提交于 2019-12-06 16:49:23
问题 I was playing with SFINAE and found behavior I cannot explain. This compiles fine: template<typename Integer, std::enable_if_t<std::is_integral<Integer>::value>* = nullptr> void foo(Integer) {} template<typename Floating, std::enable_if_t<std::is_floating_point<Floating>::value>* = nullptr> void foo(Floating) {} While this ( nullptr replaced with 0 ): template<typename Integer, std::enable_if_t<std::is_integral<Integer>::value>* = 0> void foo(Integer) {} template<typename Floating, std:

Is it safe to assume sizeof(double) >= sizeof(void*)?

狂风中的少年 提交于 2019-12-06 14:50:49
Is it safe to assume that sizeof(double) will always be greater than or equal to sizeof(void*) ? To put this in some context, is the following portable? int x = 100; double tmp; union { double dbl; void* ptr; } conv; conv.ptr = (void*)&x; tmp = conv.dbl; conv.dbl = tmp; printf("%d\n", *((int*)conv.ptr)); It does work on the few machines that I've tested it on, but I can see this going horribly wrong if sizeof(void*) > sizeof(double) . On current systems yes. double is 64 bits on all current and future systems because they're aligned with IEEE arithmetic's double-precision. It's unlikely but

Invalid conversion from ‘void*’ to ‘unsigned char*’

折月煮酒 提交于 2019-12-06 02:06:40
问题 I have the following code; void* buffer = operator new(100); unsigned char* etherhead = buffer; I'm getting the following error for that line when trying to compile; error: invalid conversion from ‘void*’ to ‘unsigned char*’ Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it? 回答1: You need to cast as you can not convert a void* to anything without casting it first. You would need to do unsigned char* etherhead = (unsigned char*

Why is void* considered unsafe in C++? [duplicate]

走远了吗. 提交于 2019-12-06 00:10:37
This question already has answers here : What is type safety and what are the “type safe” alternatives? [duplicate] (10 answers) Closed last year . I'm reading Bjarne Stroustrup C++ FAQ site. Where I saw following line. avoid void* (keep them inside low-level functions and data structures if you really need them and present type safe interfaces, usually templates, to your users) Why is void* considered unsafe in C++? Why is void* considered unsafe in C++? Because void* represents a memory address without any type information. The compiler cannot know what data type structures are used what the

dereferencing structure from (void *) type

假装没事ソ 提交于 2019-12-05 22:28:29
问题 i'm trying to pass data with void pointer and then cast it to (pData *) type. What am i doing wrong? gcc gives me gcc test.c error: request for member ‘filename’ in something not a structure or union typedef struct data { char *filename; int a; } pData; void mod_struct(void *data) { printf("%s\n",(pData *)data->filename); //error on this line } void main() { pData *data; data = (pData *) malloc(sizeof(pData)); data->filename = (char *)malloc(100); strcpy(data->filename,"testing testing");

void* to Object^ in C++/CLI

故事扮演 提交于 2019-12-05 19:35:48
I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#. Some of the native C++ functions have a return type of void*. I am not sure how to handle this when I pass back the value to my calling code. For instance: if a C# app calls my dll wrapper, what do I return from the native call: void* start(ThreadFunc,void *, unsigned *); I am currently attempting to box the return in a generic System::Object^ with no luck. This is the call in the wrapper: m_NativeThread->start(cb, GCHandle::ToIntPtr(GCHandle::Alloc(o))

Dynamic Array of Void Pointers

时间秒杀一切 提交于 2019-12-05 13:27:42
I'm trying to create a dynamic set abstract data type, based on a dynamic array. However, I get a compiler warning and an error when I try to add the data to the array, which are: warning: dereferencing 'void *' pointer [enabled by default] error: invalid use of void expression My code is as follows, I've marked the problematic line with a comment struct SET { //general dynamic array void *data; int elements; //number of elements int allocated; // size of array }; struct SET create() { //create a new empty set struct SET s; s.data = NULL; s.elements = 0; s.allocated = 0; //allocations will be