This week I found a new interesting piece of code. Someone wanted to initialize a string with a special length of the same char.

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.

A better solution would be to set the length of the string and then to set all chars.

Or simply call the StringOfChar function.

This entry was posted in Tips and Tricks and tagged . Bookmark the permalink.