I get an error EStackOverflow when creating packed struct in delphi 7.0
Asked Answered
E

2

5

I'm getting an EStackOverflow when creating a packed struct in Borland Delphi 7.0

I want to do the following:

Type

 T4 = packed record
     VT  : integer;
     SKT : byte;
  end;

  T3 = packed record
     O : boolean;     
     TT4 : array of T4;  
  end;

  T2 = packed record
     con  : boolean;
     TT3 : array [64..90,64..90] of T3;
  End;

  TTT = array [64..90,64..90] of T2;


procedure TForm1.Button1Click(Sender: TObject);
var  
   Arr  : TTT;
begin 
        Arr[64,64].con:=false;
end;

But when I run the program and click the button, I get an EStackOverflow error on the begin line of Button1Click.

Can someone help me?

Ean answered 31/3, 2011 at 13:14 Comment(3)
It really does, I just confirmed it (and updated question slightly)Chimkent
@daemon That part is harmless it's just 4+1 bytes. The constant sized arrays are the problem. They're simply too big.Sobriquet
@daemon_x: he doesn't touch TT4 at all, and that expression should be sizeof(Pointer) anyway (dynamic array).Saltwater
S
12

Simple, the created items are too big for the default stack size. Either increase that when creating the thread or allocate the memory on the heap. Either way works.

Just do the math on it:

sizeof(T4) = 5
sizeof(T3) = 5
sizeof(T2) = 3646 // if I'm right
sizeof(TTT)= 2657934
Saltwater answered 31/3, 2011 at 13:20 Comment(1)
Spot on! Though I get SizeOf(TTT) = 3646*27*27 = 2657934 = 2.5MB The default Max Stack Size is only 1MB. :/Senzer
W
2

You can partially solve this by increasing your stack size in your project options.

But you shouldn't:

Don't create those huge structures on the stack. That's what the heap is for, not the stack.

Womenfolk answered 31/3, 2011 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.