In WinSock2.h, the invalid socket and socket error are defined as these? Is there any significance to this?
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
In WinSock2.h, the invalid socket and socket error are defined as these? Is there any significance to this?
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
Long ago, early in the morning of a cold rainy day, in the midst of misty green covered hills in a quaint but growing suburb of Seattle, WA, there was a small yet sprawling cluster of red-bricked buildings, in one of which was a medium sized conference room, where a growing number of network software engineers, managers, and architects were gathering. The meeting had started quietly enough, with the mostly male group sipping their cups of tea and coffee, twirling their mechanical pencils while scribbling on their memo pads, or scratching away on their Palm Pilots (there was that guy with the Newton, but everyone ignored him).
As the presenter continued with scrolling through the source code, he was stopped by one of the 5 software architects that had specifically not been invited to the review but showed up anyway. "What, pray tell, is that.", he seethed, wagging his finger just like President Clinton. The presenter stopped and stared at the finger, finding himself somehow mesmerized by the wiggling of the finger as the architect continued to wag. "I said," he began to repeat.
The presenter looked up at the screen, and didn't notice anything amiss. "What are you talking about?" he asked.
The architect raised his eyebrows, as his face first expressed consternation, then turned to indignation, then to constipation, as he yelled "That is a blatant coding style guideline violation!".
The observers began to whisper among themselves, wondering what the issue was all about. But, the presenter was nonplussed (the second kind), quickly gave thanks for the feedback, and continued with the review.
"I will not be ignored!", the architect screamed in a high pitched squeal, as he stood up suddenly, slamming his palms to the table top with a kind of a wimpy thud.
The room erupted as fists pounded the table and managers began devising ways to cover their backsides and engineers began flipping through their coding style manuals, complete with table of contents, index, footnotes, and references.
The presenter remained externally unperturbed, but was feeling annoyed, as he desperately wanted the review completed so that he could go on his ski trip in Snoqualmie. "Are you talking about the type mismatch?", he casually asked.
The architect for a moment looked taken aback, but quickly recomposed himself, "Obviously", he snorted.
The presenter replaced the two characters '-1' with '~0' and turned around. The architect scrutinized the change, then the rest of the screen, then consulted his notes, then finally sat back down.
The rest of the review was uneventful.
On a two's complement system (and Windows is always two's complement), ~0
is equal to -1
, so there's no significance to the compiler.
There may be a significance to the reader: ~0
emphasizes that it's a value with all bits set, whereas -1
emphasizes that it's a value 1 less than 0.
Aside:
On a system which is not two's complement, and assuming that SOCKET
is an unsigned type, it is generally wrong to write (SOCKET)(~0)
. The reason is that on such systems, ~0
does not represent the value -1, it's one of INT_MIN
, negative zero, or a trap representation. Hence it will not necessarily convert to type SOCKET
as the value with all bits zero, rather it will convert as INT_MAX+2
, 0
, or goodness-knows-what (perhaps the value with all bits set).
So generally you should initialize unsigned types with -1
to get the value with all bits set. You could use UINT_MAX
, or ~0UL
, or similar, if you know which unsigned type you're dealing with. But it's not worth it, because -1
works for all unsigned types.
winsock2.h
thought the important difference was. I'm telling you what difference I think it makes.
Commented
May 30, 2012 at 22:57
Mostly it is sloppiness. -1 and ~0 are effectively the same thing.
You could argue that ~0 is better style, as a socket is an unsigned int, but really it doesn't make any difference for any practical purpose.
Because they use "#define" instead of "const unsigned" you are still open to strangeness. e.g.
unsigned a = SOCKET_ERROR;
long long b = a;
if (b != SOCKET_ERROR)
std::cout << "????\n";
might give some people a surprise.
because the SOCKET is unsigned, so can't use the (-1).
-1
for various other unsigned types in the Win32 API. They could have just as easily done the same thing in this case, eg: #define INVALID_SOCKET (SOCKET)(-1);
. It is anyone's guess why they didn't.
Commented
May 30, 2012 at 15:31
INVALID_SOCKET
is used for functions which return a SOCKET
, which a pointer (much like a HANDLE
). On 32-bit systems it will be a 32-bit value, while on 64-bit systems it will be a 64-bit value.
When properly cast to an integer of equivalent size, that will be the same as -1
. But it is much safer to not assume that it will always be properly cast.
Every (-1) is (~0) but not every (~0) is (-1).