{"id":61,"date":"2012-10-11T22:30:06","date_gmt":"2012-10-11T20:30:06","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=61"},"modified":"2012-10-11T22:30:06","modified_gmt":"2012-10-11T20:30:06","slug":"stringofchar","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/11\/stringofchar\/","title":{"rendered":"StringOfChar"},"content":{"rendered":"<p>This week I found a new interesting piece of code. Someone wanted to initialize a string with a special length of the same char.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"StringOfChar\">\r\nprocedure TMyClass.Init;\r\nconst\r\n  cBufferLength = 100;\r\nvar\r\n  I: Integer;\r\nbegin\r\n  ...\r\n  FBuffer := ''\r\n  for I := 1 to cBufferLength do\r\n    FBuffer := FBuffer + ' ';\r\n  ...\r\nend;\r\n<\/pre>\n<p>Come on guys, this is complete rubbish! In short words, a string is a record that allocs as much memory as the string needs. With this for loop the poor memory manager has to alloc a piece of memory 100 times! As you can image this is not fast and will also turn your memory into small pieces.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"StringOfChar\">\r\nprocedure TMyClass.Init;\r\nconst\r\n  cBufferLength = 100;\r\nvar\r\n  I: Integer;\r\nbegin\r\n  ...\r\n  SetFBuffer := ''\r\n  for I := 1 to cBufferLength do\r\n    FBuffer := FBuffer + ' ';\r\n  ...\r\nend;\r\n<\/pre>\n<p>A better solution would be to set the length of the string and then to set all chars.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"StringOfChar\">\r\nconst\r\n  cBufferLength = 100;\r\nvar\r\n  I: Integer;\r\nbegin\r\n  ...\r\n  SetLength(FBuffer, cBufferLength);\r\n  for I := 1 to cBufferLength do\r\n    FBuffer[I] := ' ';\r\n  ...\r\nend;\r\n<\/pre>\n<p>Or simply call the StringOfChar function.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"StringOfChar\">\r\nconst\r\n  cBufferLength = 100;\r\nvar\r\n  I: Integer;\r\nbegin\r\n  ...\r\n  FBuffer := StringOfChar(' ', cBufferLength)\r\n  ...\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This week I found a new interesting piece of code. Someone wanted to initialize a string with a special length of the same char. procedure TMyClass.Init; const cBufferLength = 100; var I: Integer; begin &#8230; FBuffer := &#8221; for I &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/11\/stringofchar\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":7,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}