Forum > Beginners

Quick question about the string type

(1/2) > >>

djongepier:
Hello everyone, finaly really beginning to practice with Free Pascal.

Given this function, is it memory leaking because I did not free the string after initialization?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function reverseName(name: string):string;var  temp: string;  c: char;begin  temp := String.Create('');  for c in name do    temp.Insert(-1, c);  reverseName := temp;end; 
What would be the 'better' way to do this?

cdbc:
Hi
The 'String' type is a /managed/ type, i.e.: automatic memory-management by the compiler via ref-counting.
So it doesn't take a constructor per se, but just an assignment like so:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...var S: string;begin  S:= ''; // init to empty  S:= 'Fpc & Lazarus';  writeln(S);end; // here the compiler sees that S goes out of scope and free's it. 
That means your code would look like this:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function reverseName(name: string):string;var  temp: string;  c: char;begin  temp := ''; // no constructor  for c in name do    temp.Insert(1, c);  reverseName := temp;end; eta: NOTE: This function will *only* work with 7 bit chars < ord(127)!
For UTF8 and Unicode it will screw up the character sequences...
Regards Benny
eta2: See dseligo's answer...

dseligo:
I never used construction 'String.Create'.
In Pascal you can just write:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---temp := '';
In case you could have UTF8 characters in your string, I would do it like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...uses LazUTF8;...function reverseName(name: string):string;var i: Integer;begin  Result := '';  For i := UTF8Length(name) downto 1 do    Result := Result + UTF8Copy(name, i, 1);end;

djongepier:
Thank you for all the answers, very insightfull.

My background is rookie Python  ::)  (like bare minimum knowledge) and to need to think about UTF8 is new for me.

I'll practice some with your awnsers to really understand it.

wp:
Or:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  LazUnicode; function ReverseName(AName: String): String;var  ch: string;begin  Result := '';  for ch in AName do    Resule := ch + Result;end;

Navigation

[0] Message Index

[#] Next page

Go to full version