The Benefits of Constants

前端 未结 14 1871
南笙
南笙 2021-02-04 07:46

I understand one of the big deals about constants is that you don\'t have to go through and update code where that constant is used all over the place. Thats great, but let\'s s

相关标签:
14条回答
  • 2021-02-04 08:04

    Constants are immutable values which are known at compile time and do not change for the life of the program.

    Constants differ from variables in one significant way in that once a value has been assigned to a constant it cannot subsequently be changed.

    At runtime you can be sure that the value defined in a constant won't change and consequently your program won't break.

    0 讨论(0)
  • 2021-02-04 08:05

    There is another benefit, stack and data segment sizes.

    Consider:

    function Recurser(i : Integer) return Integer is
      ia : array(0..9) of Integer
              := (1, 2, 3, 4, 5, 6, 7, 8, 9, 1000);
      r : Integer;
    begin
       if i = 0 then return 0; end if;
       r := ia(i mod 10);
       return r + Recurser(i - 1);
    end;
    

    Every time that function recurses you create a 320 byte structure on the Stack. But since the value of a doesn't change the stack is increasing to hold a variable that is constant. This can be very important on embedded platforms with small stacks.

    Package level variables also increase the size of the data segment. Pushing up your memory requirements.

    0 讨论(0)
  • 2021-02-04 08:06

    If you declare something as a constant, and then accidentally attempt to modify its value in your code, the compiler will tell you about your mistake. This form of static type checking is actually the main reason for using constants.

    0 讨论(0)
  • 2021-02-04 08:07

    In addition to whatever has been already said, declaring the variable as a constant gives more freedom for the optimizer. It can eliminate reading its values, it can eliminate need to create a temporary copy, it can eliminate the variable itself (this is especially true for the numeric constants).

    Another big use case for the constants is constant objects. Having a constant object (or giving away a reference to a constant object in a function) you can be sure your object is not modified, and only methods (called const methods) of that object are allowed to be called. This is however true for C++, I am not sure if the same concept is valid in Ada as well.

    0 讨论(0)
  • 2021-02-04 08:10

    If you declare a variable to be a constant, then the optimizer can often eliminate it via 'constant folding' and thus both speed up your program and save you space. As an example, consider this:

    var int a = 5;
    const int b = 7;
    ...
    c = process(a*b);
    

    The compiler will end up creating an instruction to multiply a by 7, and pass that to 'process', storing the results in c. However in this case:

    const int a = 5;
    const int b = 7;
    ...
    c = process(a*b);
    

    The compiler will simply pass 35 to process, and not even code the multiply. In addition, if the compiler knows that process has no side effects (ie, is a simple calculation) then it won't even call process. It will just set c to be the return value of process(35), saving you a function call.

    0 讨论(0)
  • 2021-02-04 08:12

    Constants are usually stored in Read Only Memory, which prevents changing them during the execution and accessing them might be faster, or at least as fast as accessing RAM.

    0 讨论(0)
提交回复
热议问题