Recent

Author Topic: Is an uninitialized dynamic array's length always zero?  (Read 1167 times)

bstewart

  • New Member
  • *
  • Posts: 23
Is an uninitialized dynamic array's length always zero?
« on: July 22, 2026, 05:10:49 pm »
Suppose the following declaration:

Code: Pascal  [Select][+][-]
  1. var
  2.   A: array of byte;

Of course, we have to call SetLength to specify a number of elements for A before we can use  it. Suppose we call Length before SetLength; e.g.:

Code: Pascal  [Select][+][-]
  1. WriteLn(Length(A));

When we compile this, FPC correctly reports:

Quote
Warning: Variable "A" of a managed type does not seem to be initialized

(The output always seems to be 0, at least on Windows.)

Does an uninitialized dynamic array always have a length of zero, or is this an implementation detail?

LeP

  • Guest
Re: Is an uninitialized dynamic array's length always zero?
« Reply #1 on: July 22, 2026, 06:04:02 pm »
Yes, is the same of:

Code: Pascal  [Select][+][-]
  1. var A: array of byte = ();

until you don't declare something like this:

Code: Pascal  [Select][+][-]
  1. var A: array of byte = (0,1,2);


And is zero when is declared locally too.

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #2 on: July 22, 2026, 06:21:47 pm »
But is this merely an implementation detail, though?

For example, a declared but uninitialized pointer is not guaranteed to be nil (in fact, probably won't be).

Does the documentation explicitly state somewhere that an uninitialized dynamic array will have a length of zero? (If it does, I couldn't find it.)


Hartmut

  • Hero Member
  • *****
  • Posts: 1172
Re: Is an uninitialized dynamic array's length always zero?
« Reply #4 on: July 22, 2026, 06:39:14 pm »
Does the documentation explicitly state somewhere that an uninitialized dynamic array will have a length of zero? (If it does, I couldn't find it.)

Yes. Please look at https://www.freepascal.org/docs-html/ref/refse20.html

Dynamic arrays are "Managed types". They all are initializated automatically.
Dynamic arrays are initialized to Nil. This means, their Length is zero. This is sure.

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #5 on: July 22, 2026, 06:42:25 pm »
Ah - I missed that in the docs. Thank you!

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1604
    • Lebeau Software
Re: Is an uninitialized dynamic array's length always zero?
« Reply #6 on: July 22, 2026, 07:09:26 pm »
Dynamic arrays are "Managed types". They all are initializated automatically.

Why is there a warning about an uninitialized managed type? It is not uninitialized, it is auto-initialized by the compiler. It doesn't make sense to have such a warning, unless it appears only if the array is accessed before any call to SetLength() on it.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #7 on: July 22, 2026, 07:31:01 pm »
Quote
...unless it appears only if the array is accessed before any call to SetLength() on it.

Yes, and in fact that's precisely when it does appear (hence the question).

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #8 on: July 22, 2026, 08:04:00 pm »
This means, then, that initializing a dynamic array means to call SetLength with the dynamic array variable as a parameter.

In the documentation for the SetLength procedure, I read this:

Quote
In case the length is set to a larger length than the current one, the new elements are zeroed out for a dynamic array.

With this in mind, suppose the following code:

Code: Pascal  [Select][+][-]
  1. var
  2.   A: array of byte;
  3. begin
  4.   SetLength(A, 2);
  5. end;

Presumably, the condition "the length is set to a larger length than the current one" is true here--that is, the array pointed to by A is uninitialized (i.e., length is zero) prior to calling SetLength. The call to SetLength specifies that the dynamic array holds two elements (larger than the previous number which was zero)--which means, then that the dynamic array now holds two zero bytes.

Is my understanding correct?

Thaddy

  • Hero Member
  • *****
  • Posts: 19624
  • Glad to be alive.
Re: Is an uninitialized dynamic array's length always zero?
« Reply #9 on: July 22, 2026, 08:18:25 pm »
Yes, even if the dynamic array is allocated on the stack. Many would not expect the latter.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. procedure localalloc;
  3. var
  4.   A: array of byte;
  5. begin
  6.   SetLength(A, 2);
  7.   Writeln(A[0],'--',A[1]);
  8. end;
  9.  
  10. begin
  11.   localalloc;
  12. end.
« Last Edit: July 22, 2026, 08:25:50 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #10 on: July 22, 2026, 08:22:50 pm »
Thank you; just wanted to make sure I understood correctly.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12640
  • Debugger - SynEdit - and more
    • wiki
Re: Is an uninitialized dynamic array's length always zero?
« Reply #11 on: July 22, 2026, 09:07:09 pm »
Mind you: Result may have random-ish data in it.

If your result is a dyn-array, it can have data. Yes, I have seen that myself. Often data from a temp in the caller, e.g. when making several calls.

And, if you are doing "SetLength" and the new array needs more memory, then you may copy the data of the old array (that may matter if you have large arrays (millions of entries).


Btw, similar for ansistring.

bstewart

  • New Member
  • *
  • Posts: 23
Re: Is an uninitialized dynamic array's length always zero?
« Reply #12 on: July 22, 2026, 10:32:25 pm »
So you are saying that the following statement in the documentation is only sometimes true:
 
Quote
In case the length is set to a larger length than the current one, the new elements are zeroed out for a dynamic array.

This means that if I am using this:

Code: Pascal  [Select][+][-]
  1. var
  2.   A: array of byte;
  3. begin
  4.   SetLength(A, 10);
  5. end;

...then there's not really a guarantee that the dynamic array in variable A is initialized to zero.

Is that the case?

Thaddy

  • Hero Member
  • *****
  • Posts: 19624
  • Glad to be alive.
Re: Is an uninitialized dynamic array's length always zero?
« Reply #13 on: July 22, 2026, 10:40:07 pm »
In this case @Martin_fr is (very rare) mistaken: even on stack dynamic arrays are initialized as per my example.
Any "programmer" that knows only one programming language is not a programmer

jcmontherock

  • Sr. Member
  • ****
  • Posts: 363
Re: Is an uninitialized dynamic array's length always zero?
« Reply #14 on: July 22, 2026, 10:40:33 pm »
@Remy:   ....before any call to SetLength() or Insert on it.
Windows 11 UTF8-64 - Lazarus 4.8-64 - FPC 3.2.2

 

TinyPortal © 2005-2018