TL;DR: Why isn't everybody screaming, "Don't use short
, int
, and long
unless you really need to, and you very likely don't need to!"
I understand that, in theory, by using the types short
, int
, and long
, you let the compiler choose the length that is most efficient for the given processor.
But is this a case of premature optimization being the root of all evil?
Suppose I have an integer variable that I know will always hold numbers from 1 to 1000. My understanding is that, assuming I am not worried about the memory difference between two and four bytes, the proponents of short
/int
/long
would have me make that variable an int
because that way the compiler can choose 16 bits or 32 bits depending on what is more efficient for the processor. If I had made it a uint16_t
, the compiler may not be able to make code that is quite as fast.
But on modern hardware is that even true? Or rather, is the speed that's going to gain me (if any), really worth the much more likely possibility that using an imprecise type leads to a major bug in my program? For instance, I might use int
throughout my program and think of it as representing a 32 bit value because that's what it's represented on every platform I've used for the past 20 years, but then my code is compiled on an unusual platform where int
is two bytes and all sorts of bugs happen.
And aside from bugs, it just seems like an annoyingly imprecise way for programmers to talk about data. As an example, here is the definition that Microsoft gives in 2019 for a GUID structure:
typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID;
Because of what a Uuid is, that long has to mean 32 bits, those shorts have to mean 16 bits, and that char has to mean 8 bits. So why continue to talk in this imprecise language of "short", "long" and (heaven help us) "long long"?
short
,int
, andlong
unless you really need to, and you very likely don't need to!" That may be true, but as this also indicates, there are cases in whichshort
,int
andlong
are needed. Since you only ever deprecate things that are not needed anymore, these types would obviously not be deprecated.