Home  >  Q&A  >  body text

c++ - “编译过程中若发现使用常量则直接以符号表中的值替换”与“const 可以节省空间,避免不必要的内存分配”是否矛盾?

大家讲道理大家讲道理2765 days ago647

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:38:57

    The two statements you used to compare are both debatable.

    The compiler may fold compile-time constants into instructions and not allocate memory on the stack at all. It is also possible to combine multiple identical constants into one and place them in the data segment. Making such a decision requires analyzing the complete code, and the factors that affect the decision include more than just define and const.

    tldr: Don’t guess the compilation results by looking at the code. The only reliable comparison method is objdump.

    reply
    0
  • Cancelreply