Recent

Author Topic: TByteArray not the same as array of byte?  (Read 2623 times)

Mario

  • New Member
  • *
  • Posts: 30
TByteArray not the same as array of byte?
« on: July 13, 2024, 12:08:11 pm »
Hi,

the following code does not compile, it gives an error "type mismatch" when I try to set the length of Ax. Any ideas why? After reading the docs https://www.freepascal.org/docs-html/rtl/sysutils/tbytearray.html I expected something like this to compile...

Code: Pascal  [Select][+][-]
  1. program testarray3;
  2.  
  3. uses
  4.   sysutils;
  5.  
  6. Type
  7.   TA = array of byte;
  8.  
  9. var
  10.   A      : TA;
  11.   Ax     : TByteArray;
  12.  
  13. begin
  14.   Setlength(A,10); // OK
  15.   Setlength(Ax,10); // Nope
  16. end.
  17.  

TRon

  • Hero Member
  • *****
  • Posts: 3646
« Last Edit: July 13, 2024, 12:25:27 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: TByteArray not the same as array of byte?
« Reply #2 on: July 13, 2024, 12:24:27 pm »
Via the RTL docs at https://www.freepascal.org/docs.html you get to https://www.freepascal.org/docs-html/current/rtl/objects/tbytearray.html which demonstrates that TByteArray is an array with a fixed maximum size.

Array of byte, OTOH, is a dynamically-sized array defined by a parameter block with the actual storage on the heap.

As a hypothetical experiment, if you used a debugger you would find that the address of element [ 0] of a static array was the same as the address of the overall array, while that is not the case for a dynamic array.

If you really had to you could cast the elements of a dynamic array starting at [ 0] to a TByteArray, but it would hardly be safe practice.

Generally speaking, TByteArray (or a pointer to one) is used as a convenience when passing a parameter of an arbitrary size. One would not be expected to actually allocate storage space for one- either on your stack (a local variable) or elsewhere in memory: the reason should, by now, be obvious.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Mario

  • New Member
  • *
  • Posts: 30
Re: TByteArray not the same as array of byte?
« Reply #3 on: July 13, 2024, 12:27:33 pm »
Ok but then - a TByteArray is a 32k object?

I wanted to load a blob from a database and want to allocate space for it. The dynamic array is the correct one, right?

TRon

  • Hero Member
  • *****
  • Posts: 3646
Re: TByteArray not the same as array of byte?
« Reply #4 on: July 13, 2024, 12:33:19 pm »
Ok but then - a TByteArray is a 32k object?
Yes, that is correct.

Quote
I wanted to load a blob from a database and want to allocate space for it. The dynamic array is the correct one, right?
Yes, but also see my revised first response. There is TByteDynArray in unit types.

See also MarkMLI's excellent answer (sorry for the crosspost MarkMLI).

edit: fwiw normally it should not matter whether you use the declaration in unit types or define your own (even overriding TByteArray if you like) but in case of the event that TByteDynArray gets helpers then you might miss out on that. Some consistency (by using types unit) might be beneficial on the long run.
« Last Edit: July 13, 2024, 12:39:35 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: TByteArray not the same as array of byte?
« Reply #5 on: July 13, 2024, 12:47:59 pm »
Ok but then - a TByteArray is a 32k object?
Yes, that is correct.

Hold it. That is not strictly correct, its size is defined by the constants used in its definition which are likely to be platform/version specific.

See also MarkMLI's excellent answer (sorry for the crosspost MarkMLI).

No problem, but I thought it worth adding mine because of the explicit link to the root of documentation tree.

MarkMLl


Quote
MaxBytes

Maximum data size (in bytes)
Declaration

Source position: objects.pp line 114

const MaxBytes = 128 * 1024 * 128;
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 3646
Re: TByteArray not the same as array of byte?
« Reply #6 on: July 13, 2024, 12:52:04 pm »
Hold it. That is not strictly correct, its size is defined by the constants used in its definition which are likely to be platform/version specific.
I sense a miscommunication coming up (or already present).

The static array is declared as an array of 32768 bytes... how does that not correspond to occupying 32k of bytes ? I give you that it is not a packed array so perhaps mileage might vary depending on platform but that strikes me as out of the ordinary.
« Last Edit: July 13, 2024, 12:53:44 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: TByteArray not the same as array of byte?
« Reply #7 on: July 13, 2024, 12:56:42 pm »
Hold it. That is not strictly correct, its size is defined by the constants used in its definition which are likely to be platform/version specific.
I sense a miscommunication coming up (or already present).

The static array is declared as an array of 32768 bytes... how does that not correspond to occupying 32k of bytes ? I give you that it is not a packed array so perhaps mileage might vary depending on platform but that strikes me as out of the ordinary.

Docs say a TByteArray has 0..(128 * 1024 * 128) - 1 bytes. That's not 32K.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 3646
Re: TByteArray not the same as array of byte?
« Reply #8 on: July 13, 2024, 01:01:22 pm »
Docs say a TByteArray has 0..(128 * 1024 * 128) - 1 bytes. That's not 32K.
Ah, I see it now. You used the reference to objects not sysutils  ;)

Yes, in the case of using the objects unit I fullhearted agree with your statements.

fwiw: you gotta love that search engines prefer indexing objects above sysutils.
« Last Edit: July 13, 2024, 01:05:50 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: TByteArray not the same as array of byte?
« Reply #9 on: July 13, 2024, 01:08:37 pm »
Should probably be rated a mistake on my part, but I'm intermittently juggling antenna connections here.

In any event, that type's intended to be used as a parameter-passing convenience either via a var parameter or a pointer, not as something for actual allocation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 3646
Re: TByteArray not the same as array of byte?
« Reply #10 on: July 13, 2024, 01:13:06 pm »
@MarkMLI:
All is good :) . You are not the first and definitely not the last person to run into a similar situation (points at me).
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: TByteArray not the same as array of byte?
« Reply #11 on: July 13, 2024, 02:35:24 pm »
fwiw: you gotta love that search engines prefer indexing objects above sysutils.

Except that I was referring to the official documentation. In case of a clash the index should have indication of unit/namespace... and that's fair and square an FPC tools issue.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: TByteArray not the same as array of byte?
« Reply #12 on: July 13, 2024, 02:52:00 pm »
mixing up the lot:
In types there are much better options:
https://www.freepascal.org/docs-html/current/rtl/types/tbytedynarray.html
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: TByteArray not the same as array of byte?
« Reply #13 on: July 13, 2024, 03:24:43 pm »
I really don't think the TByteArray was intended to be used as a heap or static allocated item, that's insane.

However, it works well as a Type Cast to a generic pointer to unknown sized memory being managed dynamically.

Just my opinion, and I have lots of them btw!  :D

The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1282
Re: TByteArray not the same as array of byte?
« Reply #14 on: July 13, 2024, 08:51:59 pm »
The fact that I didn't raise my head too far about the parapet is possibly fortunate, since I have already been accused in a legal hearing of being too interested in obsolete languages and hardware (it didn't stick).

But it has established a pattern of conduct that can be used as evidence in future proceedings. :)
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

 

TinyPortal © 2005-2018