Why does Delphi warn when assigning ShortString to string?

后端 未结 6 1613
栀梦
栀梦 2021-01-07 18:42

I\'m converting some legacy code to Delphi 2010.

There are a fair number of old ShortStrings, like string[25]

Why does the assignment below:

         


        
6条回答
  •  感动是毒
    2021-01-07 19:33

    It's safe ( as long as you're using the ShortString for its intended purpose: to hold a string of characters and not a collection of bytes, some of which may be 0 ), but may have performance implications if you do it a lot. As far as I know, Delphi has to allocate memory for the new unicode string, extract the characters from the ShortString into a null-terminated string (that's why it's important that it's a properly-formed string) and then call something like the Windows API MultiByteToWideChar() function. Not rocket science, but not a trivial operation either.

提交回复
热议问题