[fpc-devel] Const optimization is a serious bug

Jonas Maebe jonas.maebe at elis.ugent.be
Sat Jul 9 18:41:05 CEST 2011


On 09 Jul 2011, at 03:29, Martin wrote:

> We do know:
> http://www.freepascal.org/docs-html/ref/refsu58.html#x135-14500011.4.4
>> A constant argument is passed by reference if its size is larger than a pointer. It is passed by value if the size is equal or is less then the size of a native pointer.

That part of the manual is wrong. What const does is by design completely implementation-dependent (except for cdecl/cppdecl routines, where it behaves the same as in C/C++, and for mwpascal routines, where it behaves the same as in MetroWerks Pascal). And yes, this is different from the meaning of const C and C++ (which is why the meaning is specified separately there). And yes, this means that it can be unsafe if you pass global variables as const and the modify them in the called routine, and that every objection voiced already 10 times in this thread applies. That is how const has always worked since it was introduced in Turbo Pascal. And no, that does not mean this is ideal, but rewriting the entire const handling is not going to happen.

Alternatives (such as constref) can be used, and checks can be added, etc, but in general const has always meant in FPC (and Delphi) and will probably always mean "I promise that I will not modify this parameter in any way, please help me as far as you can in preventing accidental modifications, and apart from that you can generate any code you want based on that assumption".


Jonas


More information about the fpc-devel mailing list