void-pointers

C isn't that hard: void ( *( *f[] ) () ) ()

删除回忆录丶 提交于 2019-11-27 09:57:41
I just saw a picture today and think I'd appreciate explanations. So here is the picture: I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture: So this "reading spirally" is something valid? Is this how C compilers parse? It'd be great if there are simpler explanations for this weird code. Apart from all, can these kind of codes be useful? If so, where and when? There is a question about "spiral rule", but I'm not just asking about how it's applied or how expressions are read with

Pointer arithmetic when void has unknown size [closed]

…衆ロ難τιáo~ 提交于 2019-11-27 09:10:41
In Visual Studio C++ version 9 (and probably other versions too), the following code: int a = sizeof(void); void const *b = static_cast<void const *>("hello world"); b += 6; Generates these errors : error C2070: 'void': illegal sizeof operand error C2036: 'const void *' : unknown size This code works under GCC, which treats sizeof(void) as 1 . Is there some way around this limitation, as casting explicitly to char * for purposes of pointer arithmetic adds to the confusion ( void * is well recognised and used as a typeless pointer to raw memory). Update0 Please note, I'm well aware of the

multiple inheritance: unexpected result after cast from void * to 2nd base class

本秂侑毒 提交于 2019-11-27 09:07:21
My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting these void* in case of classes with multiple base classes fails and even crashes my program after invoking methods on these down casted pointers even if the memory addresses seem to be correct. The crash happens during access to "vtable". So I have created a small test case, environment is gcc 4.2 on Mac OS X: class Shape { public: virtual int w() = 0;

Casting void pointers

假如想象 提交于 2019-11-27 08:39:22
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? 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 C the casting is frowned upon because it can suppress compiler warnings for a missing prototype of malloc .

Test for void pointer in C++ before deleting

夙愿已清 提交于 2019-11-27 08:01:44
问题 I have an array in C++: Player ** playerArray; which is initialized in the constructor of the class it is in. In the destructor I have: delete playerArray; except when testing the program through Valgrind it says that there are some calls to delete to a void pointer: operator delete(void*) I want to test whether the playerArray is a void pointer before calling delete to avoid this error. Does anyone know how to do this? 回答1: Perhaps you meant delete [] playerArray . You need the [] if the

Byte precision pointer arithmetic in C when sizeof(char) != 1

穿精又带淫゛_ 提交于 2019-11-27 07:37:27
问题 How can one portably perform pointer arithmetic with single byte precision? Keep in mind that: char is not 1 byte on all platforms sizeof(void) == 1 is only available as an extension in GCC While some platforms may have pointer deref pointer alignment restrictions, arithmetic may still require a finer granularity than the size of the smallest fundamental POD type 回答1: Your assumption is flawed - sizeof(char) is defined to be 1 everywhere. From the C99 standard (TC3), in section 6.5.3.4 ("The

can void* be used to store function pointers? [duplicate]

落爺英雄遲暮 提交于 2019-11-27 06:39:20
问题 This question already has an answer here: Why are function pointers and data pointers incompatible in C/C++? 14 answers void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? int send(); void* p = send; Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable? 回答1: No it may not. According to the C Standard (6.3.2.3 Pointers) 1 A pointer to void may be

How to cast an integer to void pointer?

爱⌒轻易说出口 提交于 2019-11-27 06:34:34
While working with Threads in C, I'm facing the warning "warning: cast to pointer from integer of different size" The code is as follows #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> void *print(void *id) { int a=10; printf("My thread id is %ld\n",pthread_self()); printf("Thread %d is executing\n",id); return (void *) 42; } int main() { pthread_t th[5]; int t; int i; int status; void *ret; for(i=0;i<5;i++) { status=pthread_create(&th[i],NULL,print,(void *)i); //Getting warning at this line if(status) { printf("Error creating threads\n"); exit(0); } pthread_join

How void pointer arithmetic is happening in GCC

好久不见. 提交于 2019-11-27 06:29:57
问题 int main() { int a; void *p; p = &a; printf("%ld\n",(long)p); p = p+1; printf("%ld\n",(long)p); } In this program, p+1 is just incrementing the value of p by 1. I know void pointer arithmetic is not possible in C , so GCC is doing it implicitly. And if yes, then is it taking it as char pointer . Also, why dereferencing is not possible for void pointer, if it is implicitly doing pointer arithmetic. 回答1: C does not allow pointer arithmetic with void * pointer type. GNU C allows it by

dynamic_cast from “void *”

情到浓时终转凉″ 提交于 2019-11-27 05:14:04
According to this , void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please clarify the issue. vitaut dynamic_cast works only on polymorphic types, i.e. classes containing virtual functions. In gcc you can dynamic_cast to void* but not from : struct S { virtual ~S() {} }; int main() { S* p = new S(); void* v = dynamic_cast<void*>(p); S* p1 = dynamic_cast<S*>(v); // gives an error } In 5.2.7 - Dynamic cast [expr.dynamic.cast] it says that for dynamic_cast<T>(v) : If T is a