Tags: char, concatenate, const, convert, delphi, explanation, itprocedure, pchar, programming, string, subject, technically, wherei

concatenate pchar with char and convert to string

On Programmer » Delphi

3,187 words with 3 Comments; publish: Mon, 05 May 2008 18:02:00 GMT; (200125.00, « »)

Hello,

Subject maybe not technically correct, but there is something where

I have no explanation for it:

procedure Test(const S: string);

begin

WriteLn(Length(S));

end;

procedure TForm1.FormCreate(Sender: T);

var

P: PChar;

begin

AllocConsole;

GetMem(P, 512);

FillChar(P^, 511, ' ');

P[511] := #0;

Test(P + ^Z); // displays 256

Test(string(P) + ^Z); // displays 512 wich is correct

FreeMem(P);

end;

Wy is the length of the first string only 256 ?

I try this in Delphi 7.

Rgds, Wilfried

http://www.mestdagh.biz

Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org

All Comments

Leave a comment...

  • 3 Comments
    • Because AFAIK, the length of a string is only 256

      Strings are from 0255 which is 256 characters.

      HTH

      Fred

      Message

      From: "Wilfried Mestdagh" <wilfried (AT) mestdagh (DOT) biz>

      To: <Delphi-Talk (AT) elists (DOT) org>

      Sent: Wednesday, 25, 2006 1:35 PM

      Subject: concatenate pchar with char and convert to string

      Hello,

      Subject maybe not technically correct, but there is something where

      I have no explanation for it:

      procedure Test(const S: string);

      begin

      WriteLn(Length(S));

      end;

      procedure TForm1.FormCreate(Sender: T);

      var

      P: PChar;

      begin

      AllocConsole;

      GetMem(P, 512);

      FillChar(P^, 511, ' ');

      P[511] := #0;

      Test(P + ^Z); // displays 256

      Test(string(P) + ^Z); // displays 512 wich is correct

      FreeMem(P);

      end;

      Wy is the length of the first string only 256 ?

      I try this in Delphi 7.

      Rgds, Wilfried

      http://www.mestdagh.biz

      Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org

      Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org

      #1; Mon, 05 May 2008 18:03:00 GMT
    • Hello Fred,

      Because AFAIK, the length of a string is only 256

      So if not correct typecasted to a string it is truncated to 256 chars ?

      But can you explain me wy this is ? I should expect a compiler warning

      in that case. And it is only when concateneting an extra character.

      for example:

      Test(P); // display 511 witch is also corredt

      Rgds, Wilfried

      http://www.mestdagh.biz

      Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org

      #2; Mon, 05 May 2008 18:04:00 GMT
    • Wilfried Mestdagh wrote:

      Hello Fred,

      >Because AFAIK, the length of a string is only 256

      So if not correct typecasted to a string it is truncated to 256 chars ?

      No. PChars have been assignable to strings since at least Delphi 2,

      perhaps Delphi 1, without the need for type-casts. (Prior to that, a

      type-cast would not have been appropriate anyway. That's what the StrPas

      function was for.)

      Strings have had practically unlimited length since Delphi 2 as well.

      Their lengths are limited only be your process's memory.

      ShortStrings are limited to 255 characters. You can make "string" refer

      to ShortString instead of AnsiString with the $H- compiler directive,

      but I don't recommend that. As of Delphi 5, operations on ShortStrings

      are performed on temporary AnsiStrings anyway.

      #3; Mon, 05 May 2008 18:05:00 GMT