Recent

Author Topic: const variable can be modified in FP?  (Read 1587 times)

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
const variable can be modified in FP?
« on: May 21, 2022, 05:05:40 pm »
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. const
  4.   SomeValue: integer = 1234;
  5.  
  6. begin
  7.   SomeValue := 0;
  8. end.
  9.  
The above code actually compiles and runs. I tested this code in Delphi 7 which produces expected error stating you cannot modify a const variable.

Am I missing something?
« Last Edit: May 21, 2022, 05:10:28 pm by ssawgift »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: const variable can be modified in FP?
« Reply #1 on: May 21, 2022, 05:11:10 pm »
Yes, that is normal

It's basically a writeable constant.

 
When ever you apply a type to it then becomes a writeable constant which basically means it gets loaded into memory at program load time.

 you can turn this off but you'll find that feature very desirable.

 In other languages they call it differently, STATIC for example comes to mind..
The only true wisdom is knowing you know nothing

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
Re: const variable can be modified in FP?
« Reply #2 on: May 21, 2022, 05:39:29 pm »
Yes, that is normal

It's basically a writeable constant.

 
When ever you apply a type to it then becomes a writeable constant which basically means it gets loaded into memory at program load time.

 you can turn this off but you'll find that feature very desirable.

 In other languages they call it differently, STATIC for example comes to mind..
Then I have a natural followon question - what is the difference between a var and a const in FP?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: const variable can be modified in FP?
« Reply #3 on: May 21, 2022, 05:44:32 pm »
Then I have a natural followon question - what is the difference between a var and a const in FP?

First you have two types. Notation without : are pure constants.   (like const y= 5  ;  or const y=longint(5);).

Then you have so called typed consts (const y: integer =5;), which are initialized variables depending on the $J+ directive or  {$writableConst}.

Delphi 4+ changed strategy and also allowed you to have initialized global variables   (var y : integer =5;), to make intentions clearer.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: const variable can be modified in FP?
« Reply #4 on: May 21, 2022, 06:16:41 pm »
The default setting for {$J} in commonly used fpc modes is {$J+} which is why the code in your initial post gave the typed const the behaviour of an initialised variable.
If you set {$J-} in that code:
  • program test;
  • {$mode objfpc}
  • {$J-}
  • const
  •   SomeValue: integer = 1234;
  • begin
  •   SomeValue := 0;
  • end.
then you cannot compile and get a message such as
test.lpr(10,3) Error: Can't assign values to const variable

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: const variable can be modified in FP?
« Reply #5 on: May 21, 2022, 06:24:22 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   a: Integer;
  4. const
  5.   b = 1;
  6.   c : Integer = 1;
  7. begin
  8.   a := a + 1;
  9.   b := b + 1;
  10.   c := c + 1;
  11.   ShowMessage(a.ToString + ' ' + b. ToString + ' ' + c.ToString);
  12. end;

a (line #3) is a variable. When entering the procedure the value of a is undefined. Usually we need to initialize the variable before using it. Except when using it for passing as a out-parameter for a procedure/function. When re-entering the procedure/function the value will become undefined, it can be anything.

b (line #5) is a 'true' constant. The value of b will be same forever and cannot be changed. So line #9 will cause a compile-time error.

c (line #6) is a typed constant. If writable constant is enabled, the value of c will be kept when exiting the procedure. The name typed constant is a bit misleading. But here it means the value will be unchanged when exiting the procedure/function.

Remove the line #9 and run the button-click even several times, if you want to better understand the different.
« Last Edit: May 21, 2022, 06:35:37 pm by Handoko »

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
Re: const variable can be modified in FP?
« Reply #6 on: May 21, 2022, 06:36:30 pm »
you can turn this off but you'll find that feature very desirable.
Can you list some scenarios where this feature is useful?

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: const variable can be modified in FP?
« Reply #7 on: May 21, 2022, 06:40:23 pm »
Type constant is useful, I often use it as a switch - to remember something has been done. Same thing can be achieved without typed constant but you have to use a global variable or variable at the outer block.

This link below has a collection of demos:
https://wiki.freepascal.org/Portal:HowTo_Demos

Click the link above and find these names and lines:
  • Binary file demo - line 67
  • Game Map Editor - line 183, 184
  • Fading images using BGRABitmap - line 84, 85, 86
  • Button with animation - line 39
« Last Edit: May 21, 2022, 07:04:37 pm by Handoko »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: const variable can be modified in FP?
« Reply #8 on: May 21, 2022, 09:13:52 pm »
I'm not 100% sure, but I guess, consts should be created in "const" executable image section, i.e. read-only section. I.e. such code should compile, but it should cause access violation at runtime. I guess, compiler optimizes such code some way, so such variable is created on stack, that is obviously writable.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: const variable can be modified in FP?
« Reply #9 on: May 21, 2022, 09:41:29 pm »
Can you list some scenarios where this feature is useful?
these "writeable constants" (horrible name) are useful when a function/procedure wants to keep track of some value across calls.  For instance:
Code: Pascal  [Select][+][-]
  1. function MyFunction(Parameter : TSOMETYPE) : boolean;
  2. const
  3.    PersistentCallCount : DWORD = 0;
  4. var
  5.    SomeVar       : DWORD;
  6. begin
  7.    result := FALSE;
  8.  
  9.   { keep track of the number of times this function has been called }
  10.  
  11.   inc(PersistentCallCount);  
  12.  
  13.   SomeVar := <some expression here that assigns a value to SomeVar>;
  14.  
  15.   { presumably use SomeVar in subsequent statements  }
  16.  
  17.   { statement ... }
  18.   { statement ... }
  19.   { statement ... }
  20.  
  21.   result := TRUE;
  22. end;

The variable PersistentCallCount, unlike SomeVar, _retains_ its value when the function is exited.  The first time the function is called, its value is zero, the second time, it's value is 1, the third time its value is 2 and so on.

OTH, the initial value of SomeVar when the function is called is completely unpredictable and for that reason it must be initialized every time the function is called with a value that is presumably useful.

A very important characteristic of typed constants is that their write-ability is controlled by the {$J} directive.  When on {$J+} the compiler will place it in a writeable data segment (same area of the executable it would place a global variable), when off {$J-} it will place it in a read-only section of the executable (if the O/S supports such sections.) Also, important is to be aware that the setting of $J can be changed as many times as necessary to control whether or not the "constant" is writeable (Thanks to @benibela for pointing this out in another thread.)

Basically, a writeable constant is a global variable whose visibility is limited to the function/procedure it is declared in.   It is still global because the function could include a parameter that returns a pointer to it (such as @PersistentCallCount) which would enable outer functions to change its value but, that is not only cumbersome and very poor programming, it totally defeats the reason to declare it in the function/procedure's scope.

The equivalent of an FPC writeable constant in C is a "static" variable.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
Re: const variable can be modified in FP?
« Reply #10 on: May 22, 2022, 04:50:53 am »
these "writeable constants" (horrible name) are useful when a function/procedure wants to keep track of some value across calls. 
This one cleared cloud in my head. I agree the name 'writeable constants' is a horrible name in that it's not logical at all, and it kills my brain.
It really deserves a dedicated keyword like static in C/C++ or something similar.
« Last Edit: May 22, 2022, 04:56:39 am by ssawgift »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: const variable can be modified in FP?
« Reply #11 on: May 23, 2022, 01:27:13 pm »
I'm not 100% sure, but I guess, consts should be created in "const" executable image section, i.e. read-only section. I.e. such code should compile, but it should cause access violation at runtime. I guess, compiler optimizes such code some way, so such variable is created on stack, that is obviously writable.

No stack involved. Typed constants with writable constants enabled are simply placed in the same image section as initialized variables. And if writeable constants are off then the compiler will already do assignment checks at compile time though some might escape to runtime (e.g. if you take a pointer to a typed constant and try to write to that).

It really deserves a dedicated keyword like static in C/C++ or something similar.

It is this way since Turbo Pascal. Nothing will be changed there.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: const variable can be modified in FP?
« Reply #12 on: May 23, 2022, 05:48:27 pm »
Hi!


The introduction of Typed Constants was made by Borland in the early 80s.

There was a lot of discussion about sense and non-sense of the Typed Constants.

But after 40 years there will be no change.

From:

 
TURBO Pascal
Reference Manual
Version 3.0
Copyright C> 1983, 1984, 1985 by
BORLAND INTERNATIONAL Inc.


================================
Chapter 13
TYPED CONSTANTS
Typed constants are a TURBO specialty. A typed constant may be
used exactly like a variable of the same type. Typed constants may
thus be used as "initialized variables", because the value of a typed
constant is defined, whereas the value of a variable is undefined until
an aSSignment is made. Care should be taken, of course, not to assign
values to typed constants whose values are actually meant to be
constant.
The use of a typed constant saves code if the constant is used often
in a program, because a typed constant is included in the program
code only once, whereas an untyped constant is included every time it
is used.
Typed constants are defined like untyped constants (see page 40),
except that the definition specifies not only the value of the constant
but also the type. In the definition the typed constant identifier is
succeeded by a colon and a type identifier, which is then followed by
an equal sign and the actual constant
================================

Found in my bookshelf and here:

http://bitsavers.informatik.uni-stuttgart.de/pdf/borland/turbo_pascal/TURBO_Pascal_Reference_Manual_CPM_Version_3_Dec88.pdf



Winni

 

TinyPortal © 2005-2018