Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Apple define UInt32 as long or int depending on platform?

Tags:

c

macos-carbon

I noticed that UInt32 is defined differently based on the platform in MacTypes.h

#if __LP64__
typedef unsigned int                    UInt32;
typedef signed int                      SInt32;
#else
typedef unsigned long                   UInt32;
typedef signed long                     SInt32;
#endif

If unsigned int is always 32 bits on 32 and 64bit machines, why do they bother conditionally checking the platform?

like image 615
Corey Farwell Avatar asked Oct 15 '25 19:10

Corey Farwell


1 Answers

The type UInt32 existed before 64-bit support. It has historically been defined as unsigned long. It could have been unsigned int. I don't know why long was chosen over int at that time. The choice would have been largely arbitrary.

Once that choice was made, though, it can't be changed, even though unsigned int would work for both 32- and 64-bit.

The big thing that would break if it were changed would be C++. In C++, the types of arguments are baked into the symbol names in the object files and libraries. long and int are different types, so void foo(long); and void foo(int); are separate functions with separate symbol names. If UInt32 were to change in 32-bit, then you wouldn't be able to link against libraries that were built with the old definition. If the libraries were rebuilt with the new definition, then old compiled code would not be able to load them.

like image 150
Ken Thomases Avatar answered Oct 18 '25 12:10

Ken Thomases