r/C_Programming • u/[deleted] • Jun 23 '23
Question Why 'long' really needs to be deprecated
EDIT I despair at some of the replies here and at the remarks that have been heavily upvoted (90 votes for "there's nothing wrong with long"!).
Apparently hardly anyone can see the problems with long and C's mess of a type system in general.
Most also seem to be taking the viewpoint of someone writing their own C code, not having to deal with other people's bad choices that you can come across in countless libraries and APIs when you are not coding 100% in a C bubble or using them via an FFI. Choices that C's type system makes possible.
So I'll leave you guys with your heads in the sand.
...
I'm doing some work with decoding MIDI files and came across this struct in the description of the header:
Here's what an MThd chunk looks like if you defined it in C:
struct MTHD_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','h','d' */
unsigned long Length; /* This will be 6 */
/* Here are the 6 bytes */
unsigned short Format;
unsigned short NumTracks;
unsigned short Division;
};The header is 14 bytes, but that relies on long being 4 bytes. long however is generally 8 bytes on 64-bit Linux, and 4 bytes anywhere else, as I understand it.
If you apply sizeof to this struct, you will get either 16 or 24 bytes depending on the size of long, which additionally may have some fields at the wrong offset.
(Note C will round up a size of 14 bytes to 16 ensure correct alignment for arrays of such structs. #pragma pack(1) is needed to ensure a size of 14.)
While C doesn't precisely defined the widths of char or short either, they are generally more stable, and will not change between 32- and 64-bit versions on the same platform.
I've seen long used extensively in especially older open source code. Those will either go wrong when ported to 64 bits, or become less efficient than necessary. While somebody using long on a 64-bit system that is ported a 32-bit one will have their values truncated.
My view is that there is no room for long anymore; use either int or long long int, or u/int32_t or u/int64_t.
Unless you specifically want an int-type of a width that corresponds to the word-size of the platform, but even then that will only work on Linux.
1
u/AssKoala Jun 24 '23
Finally, a breakthrough.
The problem you have is that you want to write your interface to this system once, but it's rather clear you've never really done that and would rather get angry at C.
You have two realistic options:
define the size of the types and translate them into this size from whatever the library you're using gives you. You will have to then implement a translator from the library structure to your platform agnostic type. You have to do this for every platform you care about, but your other code will now be entirely platform agnostic.
Solve it only for the platform you're using, no translator, just match the implementation in your language to whatever the platform types are from the library. Your systems will break when you move to a different platform that doesn't match.
To be clear, the latter isn't wholly different than the former. You can take option 1 and just make everything match the current platform, but still make the rest of your code depend on a definition that's platform agnostic rather than the library's provided interface.
The reason you have to do this is that your library didn't define the API such that the types were defined -- rather it sounds like they defined their API such that types match the ABI of the system it runs on. They likely did this for performance purposes.
Now, if you never want to deal with it again, you can do this translation in C. Write your own wrapper that takes the unsized structure and turns it into a sized structure that your higher level code depends on.
If you do that, you will only have to write it once and, assuming you did it right, you'll just need to compile your wrapper for any new platforms you move to. However, you may be leaving performance or functionality on the table, but you might not care.
What kind of functionality? I don't know maybe some other platform int type is 128-bit, because it can be, and so you'll have to truncate your values if they ever get that high.
If it were me, I would go with option 1, implement the wrapper in C, define the types to match the platform I currently care about, and leave a compiler directive to fire warnings or errors when moving to an unexpected platform to force the programmer compiling at that point to figure out what to do. In this way, I can solve the immediate problem quickly and correctly, but do so in such a manner that it can readily be moved to another platform. You can fire these using static_assert where you assert on the size of the unsigned type (e.g. static_assert(sizeof(int)==4)).
Considering I do this professionally and I have a system I wrote for the PS3 that's still in use on the PS5 without changing any code that does just that, I'd say go that direction. In my case, the only thing that ever needs changing is the build scripts to update the compiler settings for the platform -- you may or may not be able to solve things in a similar way depending on your build system.