Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: PByte strangeness



Permlink Replies: 7 - Last Post: Jan 5, 2018 11:52 AM Last Post By: Rudy Velthuis (...
Markus Humm

Posts: 5,113
Registered: 11/9/03
PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 1, 2018 9:32 AM
Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

a) why is this so?
b) how does a library of mine still compile when it uses PByte and I
couldn't spont $POINTERMATH ON anywhere globally, nor locally in that
method where the PByte is being used.
c) The help text only tells one that PByte is a pointer to a byte, but
not that it's only declared conditionally. The German help rather
repeats the sentence about PByte being a pointer to a byte a 2nd
time with the exact same wording ;-)

Greetings

Markus
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 1, 2018 9:48 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

I think there is a second declaration of PByte in sysutils, or
sytem.types or such.


a) why is this so?

Legacy probably.


--
Peter Below
TeamB

Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2018 2:19 AM   in response to: Peter Below in response to: Peter Below
Am 01.01.2018 um 18:48 schrieb Peter Below:
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

I think there is a second declaration of PByte in sysutils, or
sytem.types or such.

Ah! I didn't think of that one though.
Will look at the uses clause of that unit later on when I find the time
for programming...

Should have thought about that one myself. Duh! ;-)

Greetings

Markus
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2018 7:46 AM   in response to: Peter Below in response to: Peter Below
Am 01.01.2018 um 18:48 schrieb Peter Below:
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

I think there is a second declaration of PByte in sysutils, or
sytem.types or such.

Hello,

I looked at SysUtils now, where it is not declared and at System.Types.
There you can find a declaration, but this is:

PByte = System.PByte;

Not too helpfulo to clear my case up. There it is also declared as
{$EXTERNALSYM PBYTE}, whatever this actually means. Ok, I read the help
topic for that now, but I'm puzzled why one should exclude types from
the HPP header generation. Is it, because this declaration just doubles
the one from system.pas and C++ Builder wouldn't properly cope with such
a double declaration?

Greetings

Markus
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 3, 2018 9:16 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

Am 01.01.2018 um 18:48 schrieb Peter Below:
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON,
otherwise it >> doesn't seem to be defined, while all other such
pointer types like >> PWord are still defined.

I think there is a second declaration of PByte in sysutils, or
sytem.types or such.

Hello,

I looked at SysUtils now, where it is not declared and at
System.Types. There you can find a declaration, but this is:

PByte = System.PByte;

Not too helpfulo to clear my case up. There it is also declared as
{$EXTERNALSYM PBYTE}, whatever this actually means.

$EXTERNALSYM is a hint to the Delphi compiler when generating .hpp
files for C++Builder. It tells the compiler not to generate this in the
.hpp file, since C++ already has it defined elsewhere (in C++, the type
is not needed, they can use "unsigned char*" instead).

Please read my other reply. PByte is always defined, but it does not
need $POINTERMATH ON to do pointer math, because it was declared with
$POINTERMATH ON. The docs describe this.

You know, I think everyone should read the Object Pascal Language Guide
every now and then, from beginning to end.

--
Rudy Velthuis http://www.rvelthuis.de

"Tragedy is when I cut my finger. Comedy is when you walk into
an open sewer and die." -- Mel Brooks
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 3, 2018 9:10 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

What do you mean, "it is only defined when $POINTERMATH ON"?

No, it is always defined, but DECLARED with $POINTERMATH ON, which
means that it always allow pointer arithmetic, no matter the setting of
$POINTERMATH. It is, AFAIK, the only RTL type (beside
PChar/PAnsiChar/PWideChar) that allows this, as you can see in
System.pas.

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Pointers_and_Pointer_Types_(Delphi)#Byte_Pointer

This compiles and works fine:

  {$POINTERMATH OFF}
  var
    P: PByte;


So it is not "only defined when $POINTERMATH ON", on the contrary: it
can always do pointer math because it was DECLARED WITH $POINTERMATH ON.

This is explained in the docs:

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Pointer_Math_(Delphi)

"Also, if you declare a typed pointer with this directive ON, any
variable of that type allows scaled pointer arithmetic and array
indexing, even after the directive has been turned OFF"

They only did that for PByte, probably because it was required so often
for the mobile compilers anyway, as replacement for PAnsiChar.

--
Rudy Velthuis http://www.rvelthuis.de

"A radioactive cat has eighteen half-lives." -- Unknown
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 5:54 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Am 03.01.2018 um 18:10 schrieb Rudy Velthuis (TeamB, MVP):
Markus Humm wrote:

Hello,

if I look at the declaration of PByte in Delphi Berlin I find it in
system.pas. But: it is only defined when $POINTERMATH ON, otherwise it
doesn't seem to be defined, while all other such pointer types like
PWord are still defined.

What do you mean, "it is only defined when $POINTERMATH ON"?

No, it is always defined, but DECLARED with $POINTERMATH ON, which
means that it always allow pointer arithmetic, no matter the setting of
$POINTERMATH. It is, AFAIK, the only RTL type (beside
PChar/PAnsiChar/PWideChar) that allows this, as you can see in
System.pas.

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Pointers_and_Pointer_Types_(Delphi)#Byte_Pointer

This compiles and works fine:

  {$POINTERMATH OFF}
  var
    P: PByte;


So it is not "only defined when $POINTERMATH ON", on the contrary: it
can always do pointer math because it was DECLARED WITH $POINTERMATH ON.

Looking again at this shows you're right. Sorry for my confusion (I was
deep in trying to get some faulty code analyzed which I hadn't written
and where I still lack knowledge about what the algorithm is exactly
supposed to do :-( ).


I also looked at the help topic of PByte, but that doesn't point to the
topic mentioned by you (see also secion in German version at least is
empty) and it doesn't mention much details at all. It rather repeats the
same sentence that TBytes is a pointer to a byte for a 2nd time
superflously... ;-)

Greetings

Markus
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: PByte strangeness
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2018 11:52 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

Looking again at this shows you're right.

Of course! ;-)

--
Rudy Velthuis http://www.rvelthuis.de

"O'Toole's Corollary of Finagle's Law: The perversity of the
Universe tends towards a maximum."
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02