Recent

Author Topic: [Solved] How to determine if string list has been allocated  (Read 5289 times)

cdbc

  • Hero Member
  • *****
  • Posts: 2462
    • http://www.cdbc.dk
Re: How to determine if string list has been allocated
« Reply #15 on: November 14, 2023, 01:33:21 pm »
Hi
@MarkMLl: I've seen it and even showed her, how to deal with oldschool objects, with a compilable example....
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: How to determine if string list has been allocated
« Reply #16 on: November 14, 2023, 02:06:22 pm »
@MarkMLl: I've seen it and even showed her, how to deal with oldschool objects, with a compilable example....
Yes, and on line 32 you call t.WriteMeaningOfLife.ToString on an TTest instance on the stack (not heap).
So TTest.Init isn't called then.

Now I'm beginning to understand...
If you have a TStringList inside TTest, how do you determine if TTest.Init is called ??

BTW. It is very bad programming if you do it like that (with TTest on the stack and not calling .Init where certain things are initialized).
You would have failed my entry exam of my bulletin board if you did it like that  ;D  ;)


cdbc

  • Hero Member
  • *****
  • Posts: 2462
    • http://www.cdbc.dk
Re: How to determine if string list has been allocated
« Reply #17 on: November 14, 2023, 02:18:24 pm »
Hi
It was merely to show, how to go about calling the con/de-structor of an object.
The calling TTest was just to show, that that it can be used locally(obviously with calls to init&done) without creating a pointer to object, and that "Init" gets called with the extended New-syntaks...
Sorry mate, I'm not going to create a full blown program with bells and whistles, only to show 3 lines of code, for a design, I personally think is flawed from the getgo...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: How to determine if string list has been allocated
« Reply #18 on: November 14, 2023, 02:26:31 pm »
Yes, and on line 32 you call t.WriteMeaningOfLife.ToString on an TTest instance on the stack (not heap).
So TTest.Init isn't called then.

Choosing my terminology carefully: I think a significant problem is that while you can declare (the pointer to) an instance of a class with the initial value nil, you can't do the same for an object.

Thus you can see when a variable containing (the pointer to) an instance on the heap has been initialised, but you can't reliably get the same information for an object on the stack. And while you can possibly rely on global variables starting off zeroed, you can't rely on space newly-allocated on the stack being zeroed. And for that matter I'm not sure that objects have a meaningful equivalent of FreeAndNil().

Now I'm afraid that I think there's been some poor communication right the way through this thread, which possibly hasn't helped Joanna realise where her wires are crossed. But having got to this point, I'm not sure there's much the rest of us can do to help.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 2462
    • http://www.cdbc.dk
Re: How to determine if string list has been allocated
« Reply #19 on: November 14, 2023, 02:32:04 pm »
Hi
@MarkMLl: Well spoken, mate.
+1  :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: How to determine if string list has been allocated
« Reply #20 on: November 14, 2023, 02:48:37 pm »
Sorry mate, I'm not going to create a full blown program with bells and whistles, only to show 3 lines of code, for a design, I personally think is flawed from the getgo...
+1  :)

The example was perfect and showed that if Init isn't called, that there is no meaning to life  :D

And indeed, the design is flawed from the start.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: How to determine if string list has been allocated
« Reply #21 on: November 14, 2023, 03:58:47 pm »
And indeed, the design is flawed from the start.

But it's not helped by the very poor terminology that Object Pascal's inherited from Delphi, and which the FPC project as a whole has resolutely avoided even attempting to fix.

Now would probably be a good time to remind everybody of the "OOP Structures in Pascal" document at https://github.com/zsoltszakaly/OOPstructuresinpascal

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 2462
    • http://www.cdbc.dk
Re: How to determine if string list has been allocated
« Reply #22 on: November 14, 2023, 07:30:09 pm »
Hi
Nice share, he does a good writeup, thorough :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

Joanna

  • Hero Member
  • *****
  • Posts: 1400
Re: How to determine if string list has been allocated
« Reply #23 on: November 14, 2023, 09:47:11 pm »
It is kind of confusing that there are two different versions of tobject in different units. I believe I’m using the one in classes unit. I had also not thought about the difference between how objects and classes are allocated. That might explain why the uncreated string list can’t be tested for being nil? I will try using class instead of object.

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: How to determine if string list has been allocated
« Reply #24 on: November 14, 2023, 09:53:29 pm »
It is kind of confusing that there are two different versions of tobject in different units.
How do you mean two kinds of TObject ?
There is only one TObject.

But you were using Object without the T in front (which is old style OOP).
Object had nothing to do with TObject.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: How to determine if string list has been allocated
« Reply #25 on: November 14, 2023, 10:07:23 pm »
It is kind of confusing that there are two different versions of tobject in different units.

Well, you're the one who keeps insisting how great Pascal is.

Quote
I believe I’m using the one in classes unit. I had also not thought about the difference between how objects and classes are allocated. That might explain why the uncreated string list can’t be tested for being nil? I will try using class instead of object.

For God's sake, stop floundering like a clueless newbie. This is nothing about whether you're using the Classes unit or not, these are fundamental language facilities implemented by the compiler.

Read the documentation, but in particular read the notes at the link I posted earlier.

I could write more in explanation, but I really don't want to give you an excuse to not do your own homework. If, after reading those notes and possibly re-reading the relevant bits of the documentation there's stuff you don't understand, there's plenty of people happy to help.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Joanna

  • Hero Member
  • *****
  • Posts: 1400
Re: How to determine if string list has been allocated
« Reply #26 on: November 14, 2023, 10:27:50 pm »
I took some advice from gerardus and tried using a property for stringlist. here is the code
Code: Pascal  [Select][+][-]
  1. TSTRINGSTACK = OBJECT
  2.    STRICT PRIVATE
  3.     VAR VSTRINGLIST:TStringList;
  4.     FUNCTION GET_LATEST_STRING: STRING;
  5.     PROCEDURE SET_LATEST_STRING(CONST AVALUE: STRING) ;
  6.     FUNCTION GET_STRINGLIST: TSTRINGLIST;
  7.   PUBLIC
  8.      CONSTRUCTOR INIT;
  9.      DESTRUCTOR DONE; VIRTUAL;
  10.      PROPERTY STRING_LIST:TStringList READ GET_STRINGLIST;
  11.      PROPERTY LATEST_STRING:STRING READ GET_LATEST_STRING WRITE SET_LATEST_STRING;
  12.      PROCEDURE SHOW_STRINGS; // msg with array content that isnt blank
  13.      PROCEDURE TEST;
  14.      PROCEDURE TRUNKATE(CONST START_IDX:Word); // makes everything at and beyond idx empty char
  15.  END;  
  16.  
  17. { TSTRINGSTACK }
  18.  
  19.  CONSTRUCTOR TSTRINGSTACK.INIT; // this is not called automatically
  20.  BEGIN
  21.   VSTRINGLIST:= TStringList.Create;
  22.  END;
  23.  
  24.  DESTRUCTOR TSTRINGSTACK.DONE;
  25.  BEGIN
  26.  STRING_LIST.Free;
  27.  END;
  28.  
  29.  FUNCTION TSTRINGSTACK.GET_LATEST_STRING: STRING;
  30.  BEGIN
  31.  WITH STRING_LIST DO
  32.        IF Count > 0
  33.           THEN RESULT:= Strings[0]
  34.           ELSE Result:= '';
  35.  END;
  36.  
  37.  PROCEDURE TSTRINGSTACK.SET_LATEST_STRING( CONST AVALUE: STRING) ;
  38.  BEGIN
  39.  IF (AVALUE = '')  THEN EXIT;  // dont accept empty strings
  40.  with STRING_LIST  DO
  41.       BEGIN
  42.       IF IndexOf(AVALUE) = 0
  43.          THEN EXIT // it is first one so dont try to insert twice
  44.          ELSE IF IndexOf(AVALUE) <> -1
  45.          THEN Delete(IndexOf(AVALUE));  // clear the duplicate
  46.       Insert(0,AVALUE);
  47.       END;
  48.  END;
  49.  
  50.  FUNCTION TSTRINGSTACK.GET_STRINGLIST: TSTRINGLIST;
  51.           CONST READY:BOOLEAN = FALSE;
  52. BEGIN
  53. IF NOT READY
  54.    THEN BEGIN
  55.         INIT;
  56.         READY:= TRUE;
  57.         END;
  58. RESULT:= VSTRINGLIST;
  59. END;
  60.  
  61.  PROCEDURE TSTRINGSTACK.SHOW_STRINGS;
  62.            VAR
  63.            X:Integer;
  64.            S:STRING;
  65.  BEGIN
  66.  S:= 'TSTRINGSTACK.SHOW_STRINGS-> ';
  67. WITH STRING_LIST DO
  68.      FOR X:= 0 TO Count-1 DO
  69.          S:= S +' | ' +Strings[X];
  70.  ShowMessage(S);
  71.  END;
  72.  

440bx

  • Hero Member
  • *****
  • Posts: 5805
Re: How to determine if string list has been allocated
« Reply #27 on: November 14, 2023, 10:37:36 pm »
In all fairness, someone who has not been involved with Pascal since TP 4.5 (when objects were introduced) until at least Delphi 2, is going to have a hard time making sense of all the similar yet quite different "objects" the language now supports.

and, that's just the "objects", there are so many "string" variations with behavior that is not easy to predict that, I've personally abandoned Pascal strings (and very glad I did.)


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: How to determine if string list has been allocated
« Reply #28 on: November 14, 2023, 10:47:35 pm »
I took some advice from gerardus and tried using a property for stringlist. here is the code
And do you have a special reason to use old style OOP with Object over the 'new' class(TObject) ??
(New is relative because it's the preferred method since mid '90.)

Now you are mixing two technologies.
TStringList is also a new type class.
So who insist in using old Object ? (I'm really curious about the reason.)

Joanna

  • Hero Member
  • *****
  • Posts: 1400
Re: How to determine if string list has been allocated
« Reply #29 on: November 14, 2023, 10:59:05 pm »
I just tried using a class instead of object and it makes it crash on line 58 !

It could be because I didn’t create the class maybe?

Classes have to be created but objects not. Maybe simple object is not so good for things that need to be created at runtime.

@rvk  I used to use them with turbo pascal and though that they would be suitable for something simple.

Thanks everyone for helping me figure this out. The different objects are kind of ambiguous. I’ve decided to use a class for this .
« Last Edit: November 14, 2023, 11:25:38 pm by Joanna »

 

TinyPortal © 2005-2018