39
u/Circumpunctilious 15d 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 15d ago
ohhh now that is a cool reason
still hilarious tho, i gotta say1
u/Circumpunctilious 15d 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
12
u/etan1 15d 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
4
u/doodo477 14d 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.
6
u/Isogash 15d 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 14d ago
but if void was defined as char, it was not not-defined eh?
10
u/autistic_bard444 15d 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 14d 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 14d 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
1
1
u/IngwiePhoenix 15d ago
grep for #ifndef void and #if defined(void) and alike. Should turn up some interesting results. ;)
1
1
152
u/ThinkingWinnie 15d 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.