delphi - Is PChar('') guaranteed to be a pointer to #0 (not nil)? -
i understand in delphi, empty string (ansistring or widestring) can represented nil pointer, or pointer actual empty string.
by experiment i've shown in delphi xe2 (with particular compiler settings) pchar('') <> nil
. guaranteed, or might alter in future version, or dependent on compiler setting?
i'm having crisis of confidence. if can give me definitive reply i'd grateful.
yes. type casts string literals pchar never null pointers. type casts strings of same character type pchar won't null, either. (string pchar, ansistring pansichar, etc.)
type casts of other things pchar may null, though. (pointer pchar, ansistring pwidechar, etc.)
the documentation covers in mixing delphi strings , null-terminated strings section of string types topic:
you can cast unicodestring or ansistring string null-terminated string. next rules apply:
if s unicodestring, pchar(s) casts s null-terminated string; returns pointer first character in s. such casts used windows api. example, if str1 , str2 unicodestring, phone call win32 api messagebox function this: messagebox(0, pchar(str1), pchar(str2), mb_ok);. utilize pansichar(s) if s ansistring. you can utilize pointer(s) cast string untyped pointer. if s empty, typecast returns nil. pchar(s) returns pointer memory block; if s empty, pointer #0 returned. when cast unicodestring or ansistring variable pointer, pointer remains valid until variable assigned new value or goes out of scope. if cast other string look pointer, pointer valid within statement typecast performed. when cast unicodestring or ansistring look pointer, pointer should considered read-only. can safely utilize pointer modify string when of next conditions satisfied: the look cast unicodestring or ansistring variable. the string not empty. the string unique - is, has reference count of one. guarantee string unique, phone call setlength, setstring, or uniquestring procedures. the string has not been modified since typecast made. the characters modified within string. careful not utilize out-of-range index on pointer.the same rules apply when mixing widestring values pwidechar values.
delphi null string null-terminated pchar
No comments:
Post a Comment