void-pointers

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

て烟熏妆下的殇ゞ 提交于 2019-12-05 10:54:31
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? Also, 16-bit dos is not what I want to hear! ;) void * and char * are guaranteed to have the same

Using and dereferencing (void**)

做~自己de王妃 提交于 2019-12-05 05:15:01
I would like to pass a "polymorphic" array of pointers to a function. I can do the following without warnings: foo (void* ptr); bar() { int* x; ... foo(x); } gcc apparently automatically casts x to a (void*) , which is just dandy. However, I get a warning when I do the following: foo (void** ptr); bar() { int** x; // an array of pointers to int arrays ... foo(x); } note: expected ‘void **’ but argument is of type ‘int **’ warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default] My question is: why is passing an (int*) as a (void*) argument not 'incompatible',

void* is literally float, how to cast?

不想你离开。 提交于 2019-12-05 04:44:13
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 during compile time. If I write float number = (float)voidPtr; it doesn't compile, which is

Replacing realloc (C --> C++)

空扰寡人 提交于 2019-12-05 02:09:33
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 *res; ntmp = 0; mtmp = CHUNK_SIZE; //tmp = malloc(sizeof(char*) * mtmp); <-- replaced with line below

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

点点圈 提交于 2019-12-05 00:34:49
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 . There's no requirement that a Fred* and a void* have the same size and representation. (I've worked on machines where they didn't, although that was before my C++ days.) When

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

a 夏天 提交于 2019-12-04 23:52:25
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::enable_if_t<std::is_floating_point<Floating>::value>* = 0> void foo(Floating) {} gives me a compile error :

C->C++ Automatically cast void pointer into Type pointer in C++ in #define in case of type is not given (C-style) [MSVS]

萝らか妹 提交于 2019-12-04 20:50:36
问题 Hi! I've used the following C macro, But in C++ it can't automatically cast void* to type* . #define MALLOC_SAFE(var, size) { \ var = malloc(size); \ if (!var) goto error; \ } I know, I can do something like this: #define MALLOC_SAFE_CPP(var, type, size) { \ var = (type)malloc(size); \ if (!var) goto error; \ } But I don't want to rewrite a big portion of code, where MALLOC_SAFE was used. Is there any way to do this without giving the type to the macro? Maybe some MSVC 2005 #pragma / _

How do you convert void pointer to char pointer in C

一个人想着一个人 提交于 2019-12-04 16:29:47
问题 Ok this has been become sooo confusing to me. I just don't know what is wrong with this assignment: void *pa; void *pb; char *ptemp; char *ptemp2; ptemp = (char *)pa; ptemp2 = (char *)pb; Can anyone tell me why I'm getting this error: error: invalid conversion from ‘void*’ to ‘char*’ 回答1: Actually, there must be something wrong with your compiler(or you haven't told the full story). It is perfectly legal to cast a void* to char* . Furthermore, the conversion is implicit in C (unlike C++),

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

时间秒杀一切 提交于 2019-12-04 06:50:18
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? 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*)buffer; (although you could use a static_cast also) To learn more about void pointers, take a look at 6.13 —

Return nothing or return void— What exactly does C# do at the end of a void Function() call?

微笑、不失礼 提交于 2019-12-04 04:21:07
问题 Consider the following C# function: void DoWork() { ... } The C# documentation states: When used as the return type for a method, void specifies that the method does not return a value . That seems straight-forward enough and, in most cases, that suits as a fair definition. However, for many lower-level languages (i.e. C) "void" has a slightly different meaning. Specifically, all blocks of code must return something . Therefore, void is a representation of the null pointer which is a