Is it dangerous to promote types in printf arguments?

前端 未结 4 633
囚心锁ツ
囚心锁ツ 2021-02-09 06:36

My questions stem from trying to use printf to log things when trying to build for multiple bit-depth platforms (32/64 for example).

A problem that keeps rearing its ugl

4条回答
  •  一个人的身影
    2021-02-09 07:06

    1. The arguments are passed in the stack, which has a fixed width (32 or 64) bits per entry. The compiler 'casts' the integers, chars, shorts to the native width of the architecture or in the case of a double (or long long) at 32 architecture, it allocates two slots from the stack. The "padding" is done either with zeroes, or the sign bit of the variable is copied to the remaining bits. (called sign bit extension)

    2. One downside in promoting to 64 bits is the lack of compatibility in embedded systems, which do not often provide 64-bit printing. Also it means in 32-bit system some performance penalty as the top 32-bits are always passed and converted (there's a 64 bit wide division by 10 involved) without any real use. The bigger problem however, falls in to the domain of software engineering: does a "future compatible" log give a false hope that also all computation and all the input to the system works in 64-bit mode on 32-bit systems.

    (long) in 32-bit architectures doesn't mean 64 bit. That is notated with (long long).

提交回复
热议问题