Lazarus

Free Pascal => Beginners => Topic started by: Joanna from IRC on December 04, 2024, 11:57:21 am

Title: [Solved] pointer to out of scope const
Post by: Joanna from IRC on December 04, 2024, 11:57:21 am
I have a class which contains a variable; ar_checkbox_captions:tstringarray;

Code: Pascal  [Select][+][-]
  1.   PROCEDURE TINI_CHECKCOMBOBOX.TEST;
  2.            const ar:array [0..5] of string = ('1','2','3','4','5','6');
  3. BEGIN
  4. AR_CHECKBOX_CAPTIONS:= AR;
  5. LOAD;
  6. END;  

The ar_checkbox_captions starts with zero values and is initially set to nil. After I assign it to the const ar and leave the test procedure the values continue to be valid even though the array is no longer in scope. Did it make a copy of ar ? or is the information from ar still being used even though its no longer in scope?
Title: Re: pointer to out of scope const
Post by: marcov on December 04, 2024, 12:07:10 pm
The AR array is typed, so is a constant that has an address in data memory somewhere, and is not on the stack.

If tstringarray is a pointer, then the address of the constant is stored in it, and can be referenced outside.

Not languagewise elegant, but safe.
Title: Re: pointer to out of scope const
Post by: Joanna from IRC on December 04, 2024, 01:09:48 pm
That’s interesting. So the constant inside a procedure exists for the life of the program execution. That explains it. Thanks .
Title: Re: pointer to out of scope const
Post by: 440bx on December 04, 2024, 06:11:54 pm
That’s interesting. So the constant inside a procedure exists for the life of the program execution. That explains it. Thanks .
Note that depending on the setting of the $J directive that "constant" may be a variable (writable constant)
Title: Re: pointer to out of scope const
Post by: Joanna from IRC on December 04, 2024, 10:26:18 pm
That’s interesting. So the constant inside a procedure exists for the life of the program execution. That explains it. Thanks .
Note that depending on the setting of the $J directive that "constant" may be a variable (writable constant)
I  assume that this directive only applies to typed. Constants?
Title: Re: pointer to out of scope const
Post by: 440bx on December 05, 2024, 12:51:22 am
I  assume that this directive only applies to typed. Constants?
Correct.
TinyPortal © 2005-2018