Sign in Join

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

Mailing lists / fpc-devel / 2018-04

Type Compatibility: dynarray <=> Pointer

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Martok

Hi all,

I have started debugging 0031215, and discovered something slightly unrelated, but odd. I hope someone can clear that up.
While testing, I found out that the following compiles, and becomes a call to fpc_dynarray_assign:

var
  arr: array of byte;
begin
  arr:= Pointer(42);
end.

This eventually happens because of the conversion case arraydef<=pointerdef in defcmp.pas:1151, where a comment says: "nil and voidpointers are compatible with dyn. arrays". This comment was there since the very first SVN import.

My question: why *are* voidpointers assignment compatible to dynarrays? It does seem to be Delphi compatible, but I couldn't find any mention in either documentation that this is possible - only the reserved/constant "nil" is compatible, and handled elsewhere.

Regards, Martok

Reply by email

Thorsten Engler
to Martok

A zero length dynamic array is represented by a nil pointer instead of a pointer to the heap with the information that the length of the array is 0.

arr := nil;

is equivalent to:

SetLength(arr, 0);

And the nil assignment variant is pretty much ubiquitous in any code involving dynamic arrays that I'm aware of.

Show quoted text 40 lines
-----Original Message----- From: fpc-devel On Behalf Of Martok Sent: Tuesday, 17 April 2018 01:21 To: Subject: [fpc-devel] Type Compatibility: dynarray <=> Pointer Hi all, I have started debugging 0031215, and discovered something slightly unrelated, but odd. I hope someone can clear that up. While testing, I found out that the following compiles, and becomes a call to fpc_dynarray_assign: var arr: array of byte; begin arr:= Pointer(42); end. This eventually happens because of the conversion case arraydef<=pointerdef in defcmp.pas:1151, where a comment says: "nil and voidpointers are compatible with dyn. arrays". This comment was there since the very first SVN import. My question: why *are* voidpointers assignment compatible to dynarrays? It does seem to be Delphi compatible, but I couldn't find any mention in either documentation that this is possible - only the reserved/constant "nil" is compatible, and handled elsewhere. -- Regards, Martok _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Martok
to Thorsten Engler
Thorsten Engler wrote
And the nil assignment variant is pretty much ubiquitous in any code involving dynamic arrays that I'm aware of.

Yes. I know ;-)

> only the reserved/constant "nil" is compatible, and handled elsewhere

I asked specifically about assigning arbitrary untyped pointers.

Reply by email

Stefan Glienke
to Martok: I have started debugging 0031215, and discovered something slightly unrelated, but odd. I…

FWIW this has been changed in Delphi 10.2 and does not compile anymore but gives: E2010 Incompatible types: 'Dynamic array' and 'Pointer'

Show quoted text 33 lines
On 16 April 2018 at 17:21 Martok wrote: Hi all, I have started debugging 0031215, and discovered something slightly unrelated, but odd. I hope someone can clear that up. While testing, I found out that the following compiles, and becomes a call to fpc_dynarray_assign: var arr: array of byte; begin arr:= Pointer(42); end. This eventually happens because of the conversion case arraydef<=pointerdef in defcmp.pas:1151, where a comment says: "nil and voidpointers are compatible with dyn. arrays". This comment was there since the very first SVN import. My question: why *are* voidpointers assignment compatible to dynarrays? It does seem to be Delphi compatible, but I couldn't find any mention in either documentation that this is possible - only the reserved/constant "nil" is compatible, and handled elsewhere. -- Regards, Martok _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Martok
to Stefan Glienke
Stefan Glienke wrote
FWIW this has been changed in Delphi 10.2 and does not compile anymore but gives: E2010 Incompatible types: 'Dynamic array' and 'Pointer'

That is what I would have expected.
Could you please test what the following does in D10.2?

var arr: array of byte;
begin
  arr:= Pointer(0);
end.

What I'd like to know: do they check the "nil" literal, or pointers of value nil?

Regards, Martok

Reply by email

Stefan Glienke
to Martok

That compiles as well because Pointer(0) is converted by the compiler to nil.

There is even a dedicated warning for this when used in a const:

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

Martok wrote 13 lines
Am 17.04.2018 um 11:52 schrieb Stefan Glienke: > FWIW this has been changed in Delphi 10.2 and does not compile anymore but gives: > E2010 Incompatible types: 'Dynamic array' and 'Pointer' That is what I would have expected. Could you please test what the following does in D10.2? var arr: array of byte; begin arr:= Pointer(0); end. What I'd like to know: do they check the "nil" literal, or pointers of value nil?

---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft. https://www.avast.com/antivirus

Reply by email