Casting Delphi 2009/2010 string literals to PAnsiChar

前端 未结 4 1471
一向
一向 2021-02-11 01:33

So the question is whether or not string literals (or const strings) in Delphi 2009/2010 can be directly cast as PAnsiChar\'s or do they need an additional cast to AnsiString fi

4条回答
  •  无人及你
    2021-02-11 01:47

    To 'visualize' Barry Kelly and Mason Wheeler words:

    const
      FRED = 'Fred';
    
    var
      p: PAnsiChar;
      w: PWideChar;
    begin
      w := PWideChar(Fred);
      p := PAnsiChar(Fred);
    
    In ASM:
    Unit7.pas.32: w := PWideChar(Fred);
    00462146 BFA4214600       mov edi,$004621a4     
    // no conversion, just a pointer to constant/"-1 RefCounted" UnicodeString
    
    Unit7.pas.33: p := PAnsiChar(Fred);
    0046214B BEB0214600       mov esi,$004621b0
    // no conversion, just a pointer to constant/"-1 RefCounted" AnsiString
    

    As you can see in both cases PWideChar/PChar(FRED) and PAnsiChar(FRED), there is no conversion and Delphi compiler make 2 constant strings, one AnsiString and one UnicodeString.

提交回复
热议问题