Why does sizeof(int) vary across different operating systems?

Why so?

Historical reasons.

Before the advent of the ANSI C standard and size_t in 1989, int was the type used to index into arrays. malloc took an int as its argument, strlen returned one. Thus int had to be large enough to index any array, but small enough to not cause too much overhead. For file offsets, typically a larger type such as long was typedef‘d to off_t.

On the PDP-11 were C was first implemented in the early 1970s, int was as large as a processor register: 16 bits. On larger machines such as the VAX, it was widened to 32 bits to allow for larger arrays.

This convention has been largely abandoned; the C and C++ standards use size_t and ssize_t for indices and lenghts of arrays. On 64-bit platforms, often int is still 32 bits wide while size_t is 64 bits. (Many older APIs, e.g. CBLAS, still use int for indices, though.)

Leave a Comment