How SetLength () allocates memory in Delphi

When an array is declared in this form, memory is allocated statically:

var
  Data: array[0..5] of integer;

My question is when the array is declared as follows:

var
  Data: array of integer;
....
SetLength( Data, Length( Data ) + 1 );

Is memory allocated statically or dynamically?

I think the memory is allocated statically and the array is copied to memory, but I'm not sure.

+4
source share
2 answers

This dynamic allocation is for three reasons:

  • Static distribution can only be performed at compile time. As a rule, if you use a procedure or function for this, dynamic memory is allocated from the memory manager.
  • Length( Data ) + 1 , , .
  • "", "". SetLength , 1. , .
+5

, , array of Integer, . SetLength, .

+2

Source: https://habr.com/ru/post/1524532/


All Articles