Help needed about pointers lazarus / 21-General ---------------------------------------------------------------------- nikel · 2022-12-01 20:05 Hello I'm new to pointers and am not sure what I'm doing. What's wrong at this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMain.DoRun;var  ...  ArtistPtr           : ^String;  DurationPtr       : ^String;  BpmPtr              : ^String;  KeyPtr              : ^String;  PriorityPtr         : ^String;begin    Str:='';     ArtistPtr :=Nil;    ArtistPtr:=@Str;     DurationPtr :=Nil;    DurationPtr:=@Str;     BpmPtr :=Nil;    BpmPtr:=@Str;     KeyPtr :=Nil;    KeyPtr:=@Str;     PriorityPtr :=Nil;    PriorityPtr:=@Str;   ...    ArtistPtr^:=GetTagFromFile(P, 'TAG:artist');    ArtistPtr^:=ArtistPtr^.Substring(0, ArtistPtr^.Length);end;  How can I correctly use pointers? ---------------------------------------------------------------------- KodeZwerg · 2022-12-01 20:30 Quote from: nikel on December 01, 2022, 09:05:59 pmHow 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. ---------------------------------------------------------------------- KodeZwerg · 2022-12-01 20:37 Quote from: nikel on December 01, 2022, 09:05:59 pmHello 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 ---------------------------------------------------------------------- nikel · 2022-12-01 20:51 Thanks for the replies. Can you recommend me some fix for my code? I tried PChar but it gave me conversion errors. ---------------------------------------------------------------------- Bart · 2022-12-01 20:56 Why not simply use strings, since the routines return strings as well? Bart ---------------------------------------------------------------------- KodeZwerg · 2022-12-01 21:51 Quote from: nikel on December 01, 2022, 09:51:09 pmThanks 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. ---------------------------------------------------------------------- egsuh · 2022-12-02 06:49 If there is any problem, it should be about GetTagFromFile. ---------------------------------------------------------------------- alpine · 2022-12-02 08:26 Quote from: nikel on December 01, 2022, 09:05:59 pmHello I'm new to pointers and am not sure what I'm doing. What's wrong at this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMain.DoRun;var  ...  ArtistPtr           : ^String;  DurationPtr       : ^String;  BpmPtr              : ^String;  KeyPtr              : ^String;  PriorityPtr         : ^String;begin    Str:='';     ArtistPtr :=Nil;    ArtistPtr:=@Str;     DurationPtr :=Nil;    DurationPtr:=@Str;     BpmPtr :=Nil;    BpmPtr:=@Str;     KeyPtr :=Nil;    KeyPtr:=@Str;     PriorityPtr :=Nil;    PriorityPtr:=@Str;   ...    ArtistPtr^:=GetTagFromFile(P, 'TAG:artist');    ArtistPtr^:=ArtistPtr^.Substring(0, ArtistPtr^.Length);end;  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ArtistPtr :=Nil;  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? ---------------------------------------------------------------------- nikel · 2022-12-02 15:33 Quote from: KodeZwerg on December 01, 2022, 09:30:43 pmQuote from: nikel on December 01, 2022, 09:05:59 pmHow 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.  That helped a lot: https://wiki.lazarus.freepascal.org/Pointer I changed my code to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i New(ArtistPtr) ---------------------------------------------------------------------- alpine · 2022-12-02 15:53 Quote from: nikel on December 02, 2022, 04:33:04 pmI changed my code to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i New(ArtistPtr)And what is the difference except that it created a leak? ---------------------------------------------------------------------- KodeZwerg · 2022-12-02 16:05 Quote from: y.ivanov on December 02, 2022, 04:53:07 pmAnd what is the difference except that it created a leak?Only when you not Dispose(). ---------------------------------------------------------------------- egsuh · 2022-12-02 16:19 I have no problem with following codes. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var   p: ^string;   s: string;begin   s:= '';   p := nil;   p := @s;   p^ := 'this is text';   showmessage(s);end; ---------------------------------------------------------------------- alpine · 2022-12-02 16:31 Quote from: egsuh on December 02, 2022, 05:19:19 pmI have no problem with following codes. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var   p: ^string;   s: string;begin   s:= '';   p := nil;   p := @s;   p^ := 'this is text';   showmessage(s);end;               Exactly! What's the OP's problem though? Tom Knight and the Lisp Machine