[fpc-devel] Const optimization is a serious bug

Hans-Peter Diettrich DrDiettrich1 at aol.com
Sat Jul 9 09:52:25 CEST 2011


Chad Berchek schrieb:

> All the problems we've seen with const used in various contexts with 
> different data types--AnsiString, ShortString, records, etc.--are the 
> result of one fundamental problem, which is the language design. Const 
> is not clearly defined. All problems mentioned stem from this.

IMO you should remove the C specs from your brain first, before you 
start using Pascal. Pascal "const" parameters are optimization hints in 
the first place, the write protection of parameters is a less important 
item instead.

Also C++ references are somewhat different from Pascal references, while 
both can make your code crash when the referenced object is modified or 
destroyed during subroutine execution - regardless of const or not.

Pascal ShortString and AnsiString have no counterpart in the C world. If 
you don't understand how to use these language features, then use PChar 
instead, to get back your C feeling with strings.

If you are incapable of learning how to write proper code, that allows 
for "const" optimization, you better keep your hands off it.


> If we *knew* that const meant it would be by reference, then that 
> immediately eliminates the confusion in the case of ShortString and 
> records; modifying one instance through several references affects them 
> all, as expected. What the programmer says happens; there can be no 
> "bug", except in the programmer's own knowledge.

When you know that "const" parameters typically are passed by reference, 
what else do you have to know, that's different from C/C++ behaviour?

DoDi




More information about the fpc-devel mailing list