Not all "const" parameters are passed by "reference", right?

From: Ian Boyd (ian.borlandnews010_at_avatopia.com)
Date: 06/26/04

  • Next message: Andrew Cruickshank: "Re: Not all "const" parameters are passed by "reference", right?"
    Date: Sat, 26 Jun 2004 17:17:34 -0400
    
    

    It's been stuck in my head, a phrase that Mike Lische of TVirtualTree
    mentioned once"

    He said that it was wasteful and unnecessary to pass (for example) an
    Integer as const. This is because the integer itself is 32-bits, and so
    would the pointer pointing to that Integer. Both can be passed in registers
    equally well - except that passing a 32-bit value by reference would incur
    the extra cost of getting the address of the Integer.

    After this had been nagging me for a year, i finally checked it. i wrote
    both function, and compared the compiled operations. i wrote two function,
    that take an Integer parameter; one const and the other not.

    It looked to me (with my extremely limited experience with ASM) as though
    Delphi didn't bother to get the address of the Integer variable. i.e. it did
    the same operations both times.

    So is it correct that the compiler is smart enough to not pass @variable,
    when the varible itself is 32 bits? What if it's < 32 bits?

    If it's not a performance penalty (however slight) i like having const on
    "input" parameters - it helps the source be self-documenting, as well as
    it's safety checks by the compiler that i'm not trying to modify something i
    shouldn't.

    p.s. Sorry if this is not the most appropriate group. Borland has a
    newsgroup for every compiler, except Delphi.


  • Next message: Andrew Cruickshank: "Re: Not all "const" parameters are passed by "reference", right?"

    Relevant Pages

    • Re: How to convert Infix notation to postfix notation
      ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
      (comp.lang.c)
    • Re: Writing single bits to a file
      ... The 'const' keyword provides the same kind of benefit that prototypes ... enabling the compiler to warn you if it detects the fact that you ... I had not gone and more correctly fixed up the precedence heirarchy (unary ... I like to implement scripting languages for breakfast ...
      (comp.lang.c)
    • Re: const
      ... > const modifiers, ... willing to remove the const'ness with a cast, ... I don't know how good a cross-file compiler could be - it would have to be ... it's a guarantee by the programmer. ...
      (comp.programming)
    • Re: Index a #define string
      ... In this case, i wouldn't use an array myself but a const char * const, but ... should one make it of static storage? ... If they were on the stack, where do you think their values ... You mean that the compiler optimises it out. ...
      (comp.lang.cpp)
    • Re: How to define a const and initialize it?
      ... > do the same work as const int MYCONST will do.... ... But the thing is that macro substitution takes ... C++ compiler isn't even aware of its existence. ... This Item might better be called "prefer the compiler to the preprocessor," because #define is often ...
      (alt.comp.lang.learn.c-cpp)