Crafty c questions

Discussion of chess software programming and technical issues.

Moderator: Ras

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty c questions

Post by bob »

mvk wrote:
bob wrote:
mvk wrote:
bob wrote:The char * to void * idea is what I had decided to use. Makes it a bit harder to read, however.
Check if you really need to spell out the explicit (void*) cast to suppress warnings. Normally you can leave that cast implicit if the prototype is known (and when compiling in C mode at least) and keep things a bit more readable.

You will need an explicit (char*) for arithmetic and be portable.
Some versions have been complaining, which is why I changed it to start with. Not sure what the gcc guys are doing nowadays...
There are two issues intermingled in this thread:
1. implicit cast to void* (should be fine) and 2. arithmetic on void* as if it were a char* (can be rightfully flagged as an error, as it is a language extension originating from gnu but spread out to more platforms but not all).

Can you be clear about which compiler complains about the first case: implicit cast to void*? I'm not talking about the arithmetic on void*, but about the passing of a non-void* to a properly prototyped function that accepts void* but is given another pointer type. I do the implicit casting in Floyd and other programs and have not heard about compilers complaining (yet) if they are instructed to compile C, and people use a wide variety of them.

From readability point of view it would be a pity if you need two consecutive casts. The need for char* for arithmetic is understandable from the language definition. Sometimes people just blindly add casts until the warnings go away, while it might be better to look at what is happening, maybe rearrange some parenthesis and achieve the same.
I will have to go back and test. But either gcc 4.6, 4.7, 4.8 or 5.0 complained. Only a warning. A similar problem with pthread_create().

Worst case is I can go back to char * until there is a complaint...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty c questions

Post by bob »

Fought this quite a bit today. using char * with & (to align) is problematic in the extreme and I could not get it to work across all compiler versions. The char * rather than void * seems to work OK, EXCEPT for the alignment trick. I simply recast the pointer to a 64 bit unsigned int, forced alignment, then recast it and saved in the pointer, so nothing should complain about that...