Recent

Author Topic: [Solved] pointer to out of scope const  (Read 1557 times)

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1296
[Solved] pointer to out of scope const
« 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?
« Last Edit: December 04, 2024, 01:10:28 pm by Joanna from IRC »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12006
  • FPC developer.
Re: pointer to out of scope const
« Reply #1 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.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1296
Re: pointer to out of scope const
« Reply #2 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 .
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 4908
Re: pointer to out of scope const
« Reply #3 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)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1296
Re: pointer to out of scope const
« Reply #4 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?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 4908
Re: pointer to out of scope const
« Reply #5 on: December 05, 2024, 12:51:22 am »
I  assume that this directive only applies to typed. Constants?
Correct.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018