Jump to content
dummzeuch

Assign a pointer to a dynamic array

Recommended Posts

This code (from a 3rd party library) compiles fine in Delphi XE2 but does not in Delhi 10.2 (both compiling for Win32):

type
  TWordArray = array of Word;

function SomeFunction(segment_ptr: Pointer; segment_count: Integer ): TSmallintArray;
var
  i: Integer;
   p : TWordArray;
begin
  SetLength(Result, segment_count);
  p := segment_ptr;
  i := 0;
  while ((i < segment_count)) do begin
    Result[i] := SwapInt16(p[i]);
    inc(i)
  end;
end;

I get an error in the line that assigns segment_ptr to p:

 

[dcc32 Error] test.pas(197): E2010 Incompatible types: 'TWordArray' and 'Pointer'

 

Is there a compiler setting which allows that kind of assignment? (I didn't even know that this is possible).

 

How else could that be resolved?

 

My first attempt is this:

function SomeFunction(segment_ptr: Pointer; segment_count: Integer): TSmallintArray;
type
  TWordArray0 = array[0..0] of Word;
  PWordArray = ^TWordArray0;
var
  i: Integer;
  p: PWordArray;
begin
  SetLength(Result, segment_count);
  p := segment_ptr;
  i := 0;
  while ((i < segment_count)) do begin
    Result[i] := SwapInt16(p^[i]);
    inc(i)
  end;
end;

This compiles and probably should work fine (haven't tried it), but it feels so 1990ies.

Edited by dummzeuch

Share this post


Link to post

Best solution would probably to change the type of segment_ptr but then probably the E2010 pops up somewhere else.

Anyhow a simple hardcast will do what it did before they tightened the assignment rule in 10.2:

 

p := TWordArray(segment_ptr);

 

Also shouldn't FastReports have fixed that code some while ago given they support 10.2 and higher?

Edited by Stefan Glienke
  • Thanks 1

Share this post


Link to post
44 minutes ago, Stefan Glienke said:

Also shouldn't FastReports have fixed that code some while ago given they support 10.2 and higher?

From what I see it's fixed like this:

    var
      p : PWord;

 

 

Edited by Cristian Peța
  • Like 1

Share this post


Link to post
7 hours ago, Stefan Glienke said:

shouldn't FastReports have fixed that code some while ago given they support 10.2 and higher?

Sh*t I forgot to replace the function name.

 

I guess they have, but that code is from a release a few years old, way before Delphi 10.x. Maybe we should have updated, but given the "fun" I just had with another component update, I decided to simply try and compile the sources.

Edited by dummzeuch

Share this post


Link to post
On 7/23/2020 at 1:13 AM, dummzeuch said:

How else could that be resolved?

I would use PWord:

function SomeFunction(segment_ptr: Pointer; segment_count: Integer ): TSmallintArray;
var
  i: Integer;
  p : PWord {absolute segment_ptr};
begin
  SetLength(Result, segment_count);
  p := PWord(segment_ptr);
  for i := 0 to segment_count-1 do begin
    Result[i] := SwapInt16(p[i]);
    // or, if POINTER_MATH is not available on PWord:
    // Result[i] := SwapInt16(p^); Inc(p);
  end;
end;

 

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×