Delete element from a static array

吃可爱长大的小学妹 提交于 2020-01-17 05:37:32

问题


I am trying to remove an item from an array. The array is not dynamic!

I found many examples on how to do it for the dynamic variant but none for the static.

example from delphi:

var
  A: array of integer;
begin
  ...
  A:=[1,2,3,4];
  Delete(A,1,2); //A will become [1,4]
  ...
end;

example from another site:

type
  TIntArray = array of Integer;
procedure DeleteArrayElement(var AArray: TIntArray; const AIndex: Integer);
begin
  Move(AArray[AIndex + 1], AArray[AIndex], SizeOf(AArray[0]) * (Length(AArray) - AIndex - 1)); 
  SetLength(AArray, Length(AArray) - 1);
end;
...
//call via
DeleteArrayElement(IntArray, 3);
...

My array is defined as 0 .. 11 so this is not dynamic(i guess)?

When I try to use the SetLength function it says incompatible types.

Any idea how to solve this?


回答1:


When you declare a static array you tell the compiler that the memory for the whole array should be allocated and retained until the program is terminated (if allocated in global space).

You cannot change the size of a static array. This is the purpose why dynamic arrays are there in Delphi.

The Embarcadero documentation for static arrays says:

If you create a static array but don't assign values to all its elements, the unused elements are still allocated and contain random data; they are like uninitialized variables.



来源:https://stackoverflow.com/questions/33732376/delete-element-from-a-static-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!