Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-03

Conversion between ordinals and pointers is not portable

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Roland Turcan

Hello all,

I have this code:

TYPE  PRDBRecord  =^TRDBRecord;
      TRDBRecord  =PACKED RECORD
                     Content :ARRAY [0..65500] OF BYTE;
                   END;
VAR MOffset:INTEGER;
    p      :PByteArray;
    FData  :PRDBRecord;
  ...
  p := PByteArray (PtrUInt (@FData.Content[0]) + MOffset);
  ...

But the compiler informs about %subj%.

How to get rid of this warning and to make code portable?

Thanks in advance.

Jonas Maebe
to Roland Turcan
Roland Turcan wrote 8 lines
... p := PByteArray (PtrUInt (@FData.Content[0]) + MOffset); ... But the compiler informs about %subj%. How to get rid of this warning and to make code portable?

Typecast to PByte instead of to PtrUInt.

Jonas

Tomas Hajny
to Roland Turcan: I have this code: TYPE PRDBRecord =^TRDBRecord; TRDBRecord =PACKED RECORD Content…

On Wed, March 2, 2011 09:02, Roland Turcan wrote:

Hello,

I have this code:

.
.

Show quoted text 7 lines
p := PByteArray (PtrUInt (@FData.Content[0]) + MOffset); ... But the compiler informs about %subj%. How to get rid of this warning and to make code portable?

At least with 2.4.2 it is not a warning but just a hint. You should not try to avoid all the hints, but they may be useful when trying to find out source of some issues.

One concrete comment would be that MOffset is an integer and thus result of the addition is an int64 (as also pointed out by another hint). This means that you in fact typecast an int64 value to a pointer. That is always a valid operation on 64-bit platforms but on 32-bit ones it may potentially overflow depending on the values of MOffset and @FData. Obviously, this should never happen if you use the 'right' values of MOffset (and if not, it's equally dangerous on 32-bit or 64-bit regardless of overflows anyway ;-) ) but the compiler cannot really check that.

Tomas

Max Vlasov
to Jonas Maebe: Typecast to PByte instead of to PtrUInt. Jonas
Jonas Maebe wrote 16 lines
On 02 Mar 2011, at 09:02, Roland Turcan wrote: ... > p := PByteArray (PtrUInt (@FData.Content[0]) + MOffset); > ... > > > But the compiler informs about %subj%. > > How to get rid of this warning and to make code portable? > Typecast to PByte instead of to PtrUInt.

If Roland wants to keep compatibility with Delphi for some reasons, I suggest using something like

{$ifdef fpc}
  AddrInt = PByte;
{$else}
  AddrInt = integer;
{$endif}

if this approach is used the line
PByteArray (AddrInt(@FData.Content[0]) + MOffset)
should not lead to any question both from fpc and Delphi

Max