Recent

Author Topic: Help needed about pointers  (Read 509 times)

nikel

  • Full Member
  • ***
  • Posts: 162
Help needed about pointers
« on: December 01, 2022, 09:05:59 pm »
Hello I'm new to pointers and am not sure what I'm doing. What's wrong at this code:

Code: Pascal  [Select][+][-]
  1. procedure TMain.DoRun;
  2. var
  3.   ...
  4.   ArtistPtr           : ^String;
  5.   DurationPtr         : ^String;
  6.   BpmPtr              : ^String;
  7.   KeyPtr              : ^String;
  8.   PriorityPtr         : ^String;
  9. begin
  10.     Str:='';
  11.  
  12.     ArtistPtr :=Nil;
  13.     ArtistPtr:=@Str;
  14.  
  15.     DurationPtr :=Nil;
  16.     DurationPtr:=@Str;
  17.  
  18.     BpmPtr :=Nil;
  19.     BpmPtr:=@Str;
  20.  
  21.     KeyPtr :=Nil;
  22.     KeyPtr:=@Str;
  23.  
  24.     PriorityPtr :=Nil;
  25.     PriorityPtr:=@Str;
  26.  
  27.     ...
  28.     ArtistPtr^:=GetTagFromFile(P, 'TAG:artist');
  29.     ArtistPtr^:=ArtistPtr^.Substring(0, ArtistPtr^.Length);
  30. end;
  31.  

How can I correctly use pointers?
« Last Edit: December 01, 2022, 09:19:26 pm by nikel »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 962
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Help needed about pointers
« Reply #1 on: December 01, 2022, 09:30:43 pm »
How can I correctly use pointers?
By start reading its usage, for example here or here.
A general (not FPC specific) but good to read and understand tutorial you can read here.

I hope that helps you on your journey.  O:-)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 962
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Help needed about pointers
« Reply #2 on: December 01, 2022, 09:37:28 pm »
Hello I'm new to pointers and am not sure what I'm doing.
For strings you can easy use PChar or a specific version PAnsiChar/PWideChar, easy handling, many types have in Lazarus a pointer-part premade for you to use  :-*
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

nikel

  • Full Member
  • ***
  • Posts: 162
Re: Help needed about pointers
« Reply #3 on: December 01, 2022, 09:51:09 pm »
Thanks for the replies. Can you recommend me some fix for my code? I tried PChar but it gave me conversion errors.
« Last Edit: December 01, 2022, 09:52:57 pm by nikel »

Bart

  • Hero Member
  • *****
  • Posts: 4888
    • Bart en Mariska's Webstek
Re: Help needed about pointers
« Reply #4 on: December 01, 2022, 09:56:21 pm »
Why not simply use strings, since the routines return strings as well?

Bart

KodeZwerg

  • Hero Member
  • *****
  • Posts: 962
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Help needed about pointers
« Reply #5 on: December 01, 2022, 10:51:15 pm »
Thanks for the replies. Can you recommend me some fix for my code? I tried PChar but it gave me conversion errors.
In my thinking you misses to allocate memory for your string.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

egsuh

  • Hero Member
  • *****
  • Posts: 1074
Re: Help needed about pointers
« Reply #6 on: December 02, 2022, 07:49:19 am »
If there is any problem, it should be about GetTagFromFile.

alpine

  • Hero Member
  • *****
  • Posts: 635
Re: Help needed about pointers
« Reply #7 on: December 02, 2022, 09:26:34 am »
Hello I'm new to pointers and am not sure what I'm doing. What's wrong at this code:

Code: Pascal  [Select][+][-]
  1. procedure TMain.DoRun;
  2. var
  3.   ...
  4.   ArtistPtr           : ^String;
  5.   DurationPtr         : ^String;
  6.   BpmPtr              : ^String;
  7.   KeyPtr              : ^String;
  8.   PriorityPtr         : ^String;
  9. begin
  10.     Str:='';
  11.  
  12.     ArtistPtr :=Nil;
  13.     ArtistPtr:=@Str;
  14.  
  15.     DurationPtr :=Nil;
  16.     DurationPtr:=@Str;
  17.  
  18.     BpmPtr :=Nil;
  19.     BpmPtr:=@Str;
  20.  
  21.     KeyPtr :=Nil;
  22.     KeyPtr:=@Str;
  23.  
  24.     PriorityPtr :=Nil;
  25.     PriorityPtr:=@Str;
  26.  
  27.     ...
  28.     ArtistPtr^:=GetTagFromFile(P, 'TAG:artist');
  29.     ArtistPtr^:=ArtistPtr^.Substring(0, ArtistPtr^.Length);
  30. end;
  31.  

How can I correctly use pointers?

Personally, I wouldn't dare to use pointers for anything managed, the Strings are just that kind of type. You can't be sure what will happen using them in different scopes, e.g. after returning from the procedure, passing the pointer to another one, etc.

Actually ^String is of "pointer to pointer" type, because internally String is a pointer.

One can intermix Strings with PChars but only when he have a deep understanding what he's doing. Usually it was made for performance reasons.

Specifically for your case - the following statement:
Code: Pascal  [Select][+][-]
  1.   Str:='';
Makes the Str a Nil pointer, i.e. pointing to nowhere. The FPC compiler is aware of that and and comparing it to '' (empty string) is OK, but there is actually _no memory allocation_  for it. Then:
Code: Pascal  [Select][+][-]
  1.   ArtistPtr :=Nil;
  2.   ArtistPtr:=@Str;
From now on, you can easily confuse the compiler how to deal with the ArtistPtr^, especially if you do something to obscure the pointed type. At least you have a variable with a different life-cycle than the one it points to (non-managed vs managed). See dangling pointers.

Why you're doing that (taking pointers to strings) in the first place?

As egsuh wrote, it should be something with the GetTagFromFile. What is the implementation of it?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

nikel

  • Full Member
  • ***
  • Posts: 162
Re: Help needed about pointers
« Reply #8 on: December 02, 2022, 04:33:04 pm »
How can I correctly use pointers?
By start reading its usage, for example here or here.
A general (not FPC specific) but good to read and understand tutorial you can read here.

I hope that helps you on your journey.  O:-)

That helped a lot: https://wiki.lazarus.freepascal.org/Pointer

I changed my code to
Code: Pascal  [Select][+][-]
  1. New(ArtistPtr)

alpine

  • Hero Member
  • *****
  • Posts: 635
Re: Help needed about pointers
« Reply #9 on: December 02, 2022, 04:53:07 pm »
I changed my code to
Code: Pascal  [Select][+][-]
  1. New(ArtistPtr)
And what is the difference except that it created a leak?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

KodeZwerg

  • Hero Member
  • *****
  • Posts: 962
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Help needed about pointers
« Reply #10 on: December 02, 2022, 05:05:04 pm »
And what is the difference except that it created a leak?
Only when you not Dispose().
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

egsuh

  • Hero Member
  • *****
  • Posts: 1074
Re: Help needed about pointers
« Reply #11 on: December 02, 2022, 05:19:19 pm »
I have no problem with following codes.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    p: ^string;
  4.    s: string;
  5. begin
  6.    s:= '';
  7.    p := nil;
  8.    p := @s;
  9.    p^ := 'this is text';
  10.    showmessage(s);
  11. end;              

alpine

  • Hero Member
  • *****
  • Posts: 635
Re: Help needed about pointers
« Reply #12 on: December 02, 2022, 05:31:50 pm »
I have no problem with following codes.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    p: ^string;
  4.    s: string;
  5. begin
  6.    s:= '';
  7.    p := nil;
  8.    p := @s;
  9.    p^ := 'this is text';
  10.    showmessage(s);
  11. end;              
Exactly! What's the OP's problem though?

Tom Knight and the Lisp Machine
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018