1

For this code:

int
alpha(int n)
{
    n += 3;
    return n;
}

int
main(int argc, char* argv[])
{
    int n = 1231231;

    int m = alpha(n);
    return 0;
}

When compiled x86_64, the int is treated as 32 bits:

https://godbolt.org/z/UceAtE

Note in particular this assembly in the compiled alpha function:

    movl    %edi, -4(%rbp)
    addl    $3, -4(%rbp)
    movl    -4(%rbp), %eax

See that it is pulling 4 bytes (32 bits) for the int parameter.

On an 64 bit architecture, however, I would have expected an int to be treated as 64 bits. What am I misunderstanding?

firebush
  • 5,180
  • 4
  • 34
  • 45
  • 9
    Your expectations are wrong. – KamilCuk Dec 05 '19 at 16:21
  • 2
    Have a look at [SO: Why is int typically 32 bit on 64 bit compilers?](https://stackoverflow.com/q/17489857/7478597). – Scheff's Cat Dec 05 '19 at 16:21
  • 3
    https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models – Mat Dec 05 '19 at 16:21
  • x64 means memory addresses are 64bit (increasing maximum limit of memory size of the system), it has nothing to do with `int` size. – Nick Skywalker Dec 05 '19 at 16:23
  • @Scheff and Kamil: yes that answers my question. I did not see that in a search nor was it suggested as I wrote this question. Thanks! – firebush Dec 05 '19 at 16:23
  • 1
    FWIW, if you want a integer of a specific size, use the `(u)intN_t` types. – NathanOliver Dec 05 '19 at 16:24
  • 1
    I found even more suprising that `long` has different sizes in x64 when I compared VS2013 (32 bit) with `g++` (in cygwin64 64 bit). ;-) – Scheff's Cat Dec 05 '19 at 16:26
  • 1
    @Scheff *Nix uses LP64, while Windows uses LLP64. ref: https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models – NathanOliver Dec 05 '19 at 16:29
  • 1
    `int` is 32 bit on *some* 64 bit platforms. On others it is 64 bit. All the standard guarantees you is that `int` is equal in size *or* larger than `short` and at least 16 bits, so both are fine. – Jesper Juhl Dec 05 '19 at 16:33

0 Answers0