Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What defines the size of a type?

Tags:

c

gcc

sizeof

The ISO C standard says that:

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

I am using GCC-8 on BIT Linux mint (19.1) and the size of long int is 8.

I am using an app which uses GCC 7 and the compiler is 64-bit. The size of long int is 4. Does the compiler or the operating system define the size of a long int?

like image 789
Michi Avatar asked May 15 '19 19:05

Michi


3 Answers

The compiler calls all the shots. The operating system just runs the resulting binary.

That being said, the compiler will normally make an executable the operating system can use, so there's some interplay here. Since things like the size of int don't really matter so long as they're consistent, you will see variation.

In other words, if the kernel expects long int to be 8 bytes because of how it was compiled, then you'll want to compile that way to match or your compiled code won't match and none of the shared libraries will work.

like image 87
tadman Avatar answered Nov 15 '22 04:11

tadman


The Application Binary Interface for an operating system/architecture specifies the sizes of basic types:

ABIs cover details such as (bolding mine):

  • a processor instruction set (with details like register file structure, stack organization, memory access types, ...)
  • the sizes, layouts, and alignments of basic data types that the processor can directly access
  • the calling convention, which controls how functions' arguments are passed and return values are retrieved; for example, whether all parameters are passed on the stack or some are passed in registers, which registers are used for which function parameters, and whether the first function parameter passed on the stack is pushed first or last onto the stack
  • how an application should make system calls to the operating system and, if the ABI specifies direct system calls rather than procedure calls to system call stubs, the system call numbers
  • and in the case of a complete operating system ABI, the binary format of object files, program libraries and so on.
like image 31
Andrew Henle Avatar answered Nov 15 '22 02:11

Andrew Henle


This is left to the discretion of the implementation.

It's the implementation (compiler and standard library) that defines the size of long, int, and all other types.

As long as they fit the constraints given by the standard, the implementation can make all the decisions as to what sizes the types are (possibly with the exception of pointers).

like image 29
S.S. Anne Avatar answered Nov 15 '22 03:11

S.S. Anne