Board index » delphi » PChar and Array[0..X] of char

PChar and Array[0..X] of char

Hi.

Can anyone explain the difference between these two.  Don't tell me there
isn't because there is.

Does a PChar hold somewhere a size byte?

I had a problem saving a pchar into a stream.  It didn't work but it did
with an array of char.

thx

 

Re:PChar and Array[0..X] of char


In article <19970916023100.WAA02...@ladder01.news.aol.com>, bea...@aol.com

Quote
(Beagli) writes:
>Can anyone explain the difference between these two.  Don't tell me there
>isn't because there is.

>Does a PChar hold somewhere a size byte?

>I had a problem saving a pchar into a stream.  It didn't work but it did
>with an array of char.

The array is fixed to whatever you put in for X and the array hold the
string characters.

(P)Char is a (P)ointer to a dynamic character string.  That is,  the PChar
doesn't hold the characters of the string like the array does,  it only
points to the area in memory where the first character of the actual string
is stored.

The array length ends with the value of X.   The string pointed to by
PChar ends with a null.  

PChar doesn't store the length of the string in bytes,  that information
is stored with the string that PChar points to.

Re:PChar and Array[0..X] of char


Quote
>PChar doesn't store the length of the string in bytes,  that information
>is stored with the string that PChar points to.

So when you call StrLen(mypchar), how does it calculate the length? If it
is only pointing to a char. I don't think it would count until the #0.
What do you say...?

My problem arose when I tried saving a Pchar to a filestream.  The stream
didn't have the complete pchar. It cut off about 5 chars from the end and
inserted 4 bytes of garbage in the front.  I tried saving the same thing
using an array of char and it worked fine.

Re:PChar and Array[0..X] of char


Quote
Beagli wrote:

> >PChar doesn't store the length of the string in bytes,  that information
> >is stored with the string that PChar points to.

> So when you call StrLen(mypchar), how does it calculate the length? If it
> is only pointing to a char. I don't think it would count until the #0.
> What do you say...?

        Well, you're entitled to your opinion.

var P: PChar; s: string;
begin
  s:= 'Hello World';
  P:= PChar(s); //assuming D2 or D3!
  ShowMessage(inttostr(StrLen(P)));

  s[5]:= #0;
  P:= PChar(s);
  ShowMessage(inttostr(StrLen(P)));
end;

        What do you think now?

Quote
> My problem arose when I tried saving a Pchar to a filestream.  The stream
> didn't have the complete pchar. It cut off about 5 chars from the end and
> inserted 4 bytes of garbage in the front.  I tried saving the same thing
> using an array of char and it worked fine.

--
David Ullrich

sig.txt not found

Re:PChar and Array[0..X] of char


Quote
Beagli wrote:

> >PChar doesn't store the length of the string in bytes,  that information
> >is stored with the string that PChar points to.

> So when you call StrLen(mypchar), how does it calculate the length? If it
> is only pointing to a char. I don't think it would count until the #0.
> What do you say...?

> My problem arose when I tried saving a Pchar to a filestream.  The stream
> didn't have the complete pchar. It cut off about 5 chars from the end and
> inserted 4 bytes of garbage in the front.  I tried saving the same thing
> using an array of char and it worked fine.

Hi,

1. I guess all problems came out of a wrong assumption that PChar points
to a string. Wrong, PChar points to a character. If you want to avoid
troubles you better use typecast String(mypchar) whenever you need to
use a PChar as a String. (BTW, that will create a temporary string
object).

2. StrLen(mypchar) does count until the #0, look at the implementation
of this function:

function StrLen(Str: PChar): Cardinal; assembler;
asm
        MOV     EDX,EDI
        MOV     EDI,EAX
        MOV     ECX,0FFFFFFFFH
        XOR     AL,AL
        REPNE   SCASB
        MOV     EAX,0FFFFFFFEH
        SUB     EAX,ECX
        MOV     EDI,EDX
end;

-Vladimir

Re:PChar and Array[0..X] of char


In article <3422E450.4...@rgti.com>, Vladimir Vinogradsky <v...@rgti.com>
writes:

Quote
>> My problem arose when I tried saving a Pchar to a filestream.  The stream
>> didn't have the complete pchar. It cut off about 5 chars from the end and
>> inserted 4 bytes of garbage in the front.  I tried saving the same thing
>> using an array of char and it worked fine.

>Hi,

>1. I guess all problems came out of a wrong assumption that PChar points
>to a string. Wrong, PChar points to a character. If you want to avoid
>troubles you better use typecast String(mypchar) whenever you need to
>use a PChar as a String. (BTW, that will create a temporary string
>object).

I don't think so.  PChar points to a string of characters.  if it only
pointed to the first character the rest of a string allocation would be
opened to reallocation by other dynamic or static objects.

mypchar points to the string,  mypchar[2] points to a character.

If mypchar points to this string:   'This is a sample string . . .'

dereference mypchar and you get what mypchar points to:  
     mypchar^  := 'This is a sample string . . .'

dereference mypchar[2]^  and you get 'h'

Re:PChar and Array[0..X] of char


In article <34215210.4...@math.okstate.edu>, David Ullrich

Quote
<ullr...@math.okstate.edu> writes:
>> >PChar doesn't store the length of the string in bytes,  that information
>> >is stored with the string that PChar points to.

>> So when you call StrLen(mypchar), how does it calculate the length? If it
>> is only pointing to a char. I don't think it would count until the #0.
>> What do you say...?

>    Well, you're entitled to your opinion.

>var P: PChar; s: string;
>begin
>  s:= 'Hello World';
>  P:= PChar(s); //assuming D2 or D3!
>  ShowMessage(inttostr(StrLen(P)));

>  s[5]:= #0;
>  P:= PChar(s);
>  ShowMessage(inttostr(StrLen(P)));
>end;

>    What do you think now?

Same thing unless I'm missing something.  The length of P isn't stored
with P.  P only stores an integer value that points to the dynamic string.
P has no length.  By default Delphi automatically de-references PChars for
string functions.  When you call StrLen(P) you're actually calling
StrLen(P^).  Correct?

Re:PChar and Array[0..X] of char


Quote
D1Scully wrote:

> In article <34215210.4...@math.okstate.edu>, David Ullrich
> <ullr...@math.okstate.edu> writes:

> >> >PChar doesn't store the length of the string in bytes,  that information
> >> >is stored with the string that PChar points to.

> >> So when you call StrLen(mypchar), how does it calculate the length? If it
> >> is only pointing to a char. I don't think it would count until the #0.
> >> What do you say...?

> >       Well, you're entitled to your opinion.

> >var P: PChar; s: string;
> >begin
> >  s:= 'Hello World';
> >  P:= PChar(s); //assuming D2 or D3!
> >  ShowMessage(inttostr(StrLen(P)));

> >  s[5]:= #0;
> >  P:= PChar(s);
> >  ShowMessage(inttostr(StrLen(P)));
> >end;

> >       What do you think now?

> Same thing unless I'm missing something.  

        Um, same as what? First, note that you're saying I wrote things
that were actually written by someone else. It's Beagli who said
"I don't think it would count until the #0", and the point to the
example I gave was to show that "count until the #0" is exactly
what StrLen does.

Quote
> The length of P isn't stored
> with P.  P only stores an integer value that points to the dynamic string.
> P has no length.  By default Delphi automatically de-references PChars for
> string functions.  When you call StrLen(P) you're actually calling
> StrLen(P^).  Correct?

        Yes and no. Yes: It's certainly true that a PChar per se is
just a pointer to a character, with no knowledge of how long the
string it points to is - the "length" is defined by the number of
characters before the first #0.
        No: If you say APChar:= PChar(AString) then of course the
string contains information about its length. You knew that. You
might not have known that IF you allocate memory for a PChar
using StrAlloc then the StrAlloc function stores information
about the size of the allocation somewhere (presumably just
before the start of the allocation?), so IF memory for a PChar
was allocated via StrAlloc then you can get the amount of
memory allocated from the StrBufSize function. StrBufSize gives
the size of the allocation, regardless of where any nulls might
be, while StrLen looks for the first null, regardless of the
size of the allocation. (Note that the value of StrBufSize is
undefined unless you used StrAlloc to allocate memory for the
PChar.)

--
David Ullrich

sig.txt not found

Other Threads