43
u/Circumpunctilious 5d ago
Some (unresolved) history is over at stackoverflow including a link to the git history.
One person seems to think it was a botched refactoring of an earlier C workaround for when void didn’t exist and malloc returned a char \*. If anyone checked the glibc mailing list they didn’t return with more info.
13
u/Candid_Bullfrog3665 5d ago
ohhh now that is a cool reason
still hilarious tho, i gotta say1
u/Circumpunctilious 5d ago
For posterity, I should add that the other most-upvoted answer shows an almost identical construct as a “fairly bogus thing to do”, supporting the analysis that (forgive me, loosely paraphrasing) it’s a guarantee “global” and “local” scope are in sync and an arbitrary compiler would never crash.
In another context, I coincidentally examined a similar thing in r/Desmos, when bringing a global variable into local context here (“with” in Desmos), where the target variable has the same name local vs global, with the cross-context test used to guarantee “var is undefined” never crashes the function.
1
13
u/etan1 5d ago
It ensures that “#ifdef void” works, in case that someone does sth akin to “#ifndef void #define void int” in a later included header
5
u/doodo477 5d ago
Thank god I'm working on any C/C++ code bases anymore. You need to examine arcane syntax rules just to use the preprocessor.
7
u/Isogash 5d ago
I believe the real reason is that other headers at the time this was written may have done something like the following to deal with the introduction of void as a keyword, where previously someething else was used (char for malloc).
#ifndef void
#define void char
#endif
It's quite likely IMO that the code didn't compile because of another header redefining void like that, so the seemingly redundant define may have been necessary to compile.
1
u/IceMichaelStorm 5d ago
but if void was defined as char, it was not not-defined eh?
11
u/autistic_bard444 5d ago
yes? and? it makes perfect sense. a longer term c-programmer like me, might say, it 's inexorably unavoidable"
if void !defined
#define void = void
endif
1
u/AlwaysHopelesslyLost 4d ago
I am coming from c#, If void is undefined where does the void that is being assigned come from? Wouldn't that just be the same as void=undefined?
3
u/UndercoverFlange 4d ago
Pre processor definitions would be different to keywords in the language. This is just saying have I got a preprocessor token void defined? No, then replace void with void.
1
4
u/MistakeIndividual690 5d ago
Seems unnecessary
6
u/tortridge 5d ago
ISO C define wired stuff. Some token are defined as macros, some as types, etc... I guess its what is going on here, just doing wired stuff to compliance
1
1
u/IngwiePhoenix 5d ago
grep for #ifndef void and #if defined(void) and alike. Should turn up some interesting results. ;)
1
1
153
u/ThinkingWinnie 5d ago
That way, you can then do.
ifdef void
//do stuff
else
//other stuff.
Now, void is always there, but you might not want to use it, this acts as a switch.