Forum > Beginners

About FP types

<< < (2/2)

Responding to your third question - it depends what you mean by safe.

PChar(anAnsitString) is always a valid cast in the sense that it will not raise an exception or seg fault, because of the way that the Pascal AnsiString type is defined.
However, AnsiStrings can contain embedded #0 characters (since their length is not determined by the position of #0), so such casts may not give you the results you expect. Here's a little example:

--- Code: ---program Project1;

{$mode objfpc}{$H+}

  anS: AnsiString;
  aP: PChar;

  anS:= 'first'#0'second';
  WriteLn('Displaying AnsiString: "',anS,'"');
  WriteLn('Displaying PChar cast: "',aP, '"');

--- End code ---

It might be better to start a new thread for somewhat unrelated questions (this helps people searching for specific topics in future, since they don't have to wade through a long rambling conversation).


--- Quote from: Leledumbo on September 28, 2014, 04:59:45 pm ---
--- Quote ---Maxint is an alias for maxSmallint which is declared in
--- End quote ---
SysUtils override it to MaxLongInt AFAIK.

--- End quote ---

No, since sysutils won't be guaranteed included in objfpc/delphi mode. The system unit addendum for those modes is objpas.

If you choose mode delphiunicode in 2.7.1 or use the modeswitch, unit uuchar gets additionally added. (which redefines char and pchar for D2009+ compat)

Note that starting with 2.6.x afaik also pascal boolean types exist in multiple sizes, with normal boolean being equal to boolean8.



--- Quote from: marcov on October 10, 2014, 01:05:32 pm ---No, since sysutils won't be guaranteed included in objfpc/delphi mode. The system unit addendum for those modes is objpas.

--- End quote ---
Whoops, I wrote the wrong unit :-[


--- Quote from: howardpc on October 10, 2014, 12:12:55 pm depends what you mean by safe.
--- End quote ---
Safe type conversion (typecasting), in my opinion, type conversion without any errors (including hidden errors, e.g. padding), and without loss of data.


--- Quote from: Sergios on September 28, 2014, 09:08:55 am ---2. Where can I find identifiers for Byte and Boolean?

--- Quote from: ref.pdf, page 14 ---Remark: Predefined types such as Byte, Boolean and constants such as maxint are not reserved words.
They are identifiers, declared in the system unit.
--- End quote ---

--- End quote ---

Compiler built-in types can be seen in symdef, like:

--- Code: ---    function torddef.GetTypeName : string;
        names : array[tordtype] of string[20] = (

--- End code ---

Howardpc's example uses WriteLn, which the compiler translates into one or more calls to fpc_write_text_* procedures in ninl.pas
for ansi string parameters it calls fpc_write_text_AnsiStr and it uses the length provided by the AnsiString itself.
while for pchar parameters it calls fpc_write_text_PChar_As_Pointer and it finds the lengths based of 1st #0 char.
both are declared in
In both cases data came from the same address. You can check with: PtrUInt(@anS[1]) = PtrUInt(aP)

Conversion from String to Pointer, as in 1 and 3, is safe and fast. Conversion from Pointer to String (2 and 4) is not as safe, because it involves memory allocation. Notice that fpc_PWideChar_To_WideStr and fpc_PChar_To_AnsiStr are used in this case and also declared in,, and


[0] Message Index

[*] Previous page

Go to full version