Suddenly we faced the following problem that the allocation of the multidimensional array consumes more memory than it is required for the array itself.
Minimal code to reproduce:
type
TConfig1 = packed array [1..16777216] of Byte; { Works }
TConfig2 = packed array [1..256, 1..256, 1..256] of Byte; { Works }
TConfig3 = packed array [1..8, 1..8, 1..8, 1..8, 1..8, 1..8, 1..8, 1..8] of Byte;
TClass1 = class
private
FConfig: TConfig1;
public
constructor Create;
end;
TClass2 = class
private
FConfig: TConfig2;
public
constructor Create;
end;
TClass3 = class
private
FConfig: TConfig3;
public
constructor Create;
end;
constructor TClass1.Create;
begin
var X := FConfig[1];
end;
constructor TClass2.Create;
begin
var X := FConfig[1];
end;
constructor TClass3.Create;
begin
var X := FConfig[1];
end;
begin
Writeln(SizeOf(TConfig1)); { Outputs 16777216 }
Writeln(SizeOf(TConfig2)); { Outputs 16777216 }
Writeln(SizeOf(TConfig3)); { Outputs 16777216 }
TClass1.Create.Free; { success }
TClass2.Create.Free; { success }
TClass3.Create.Free; { stack overflow }
end.
So, for some reason, the number of dimensions limits the amount of memory possible for allocation in the class field.
When I do 1 or 2 dimensions only, I can allocate much more than 16MB! But, once the dimension count grows, it feels like it hits a 16MB limit (which is by default a minimum stack size).
Am I missing something?
FConfig[1]then it doesn't do that. I'm not sure what it's trying to do.mov eax,$00000200; add esp,$fffff004; push eax; dec eax; jnz ...So, a loop with 512 iterations that adjustESPand pushEAX(the loop counter). All beforeSystem._ClassCreate()is called. Thepusheventually overflows the stack.