Why are types always a certain size no matter its value?

后端 未结 19 1844
谎友^
谎友^ 2021-01-30 15:22

Implementations might differ between the actual sizes of types, but on most, types like unsigned int and float are always 4 bytes. But why does a type always occupy a certai

19条回答
  •  别那么骄傲
    2021-01-30 15:54

    It can be less. Consider the function:

    int foo()
    {
        int bar = 1;
        int baz = 42;
        return bar+baz;
    }
    

    it compiles to assembly code (g++, x64, details stripped)

    $43, %eax
    ret
    

    Here, bar and baz end up using zero bytes to represent.

提交回复
热议问题