9

Currently I have a TCube array

CreateCube : array[1..1000] of tcube;

Currently using them as a map so you might have 30 cubes wide, 20 cubes high, thus making a big grid. But 1000 cubes is not really enough for what I need, I need more like 10,000 cubes.

Is having an array this size going to cause issues down the road? Any other options?

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Glen Morse
  • 2,437
  • 8
  • 51
  • 102
  • 1
    aint a problem unless you have not enough memory – Sergio Jul 30 '13 at 09:21
  • So in this case, the 1000 cubes are made at runtime, so when i create them is it using more memory? or by defining the array , its all ready creating the cube and i am just editing the memory when i do CreateCube[i]:=tcube.Create(self); – Glen Morse Jul 30 '13 at 09:57

3 Answers3

16

There are two main scenarios where large arrays are problematic:

  1. The array is so large that it will not fit into a contiguous block of memory. If the array holds references rather than values then you may have sufficient memory for the array, but insufficient memory for the objects that are referred to.
  2. The array is declared as a local variable and leads to a stack overflow. The way you avoid that problem is to move the array onto the heap. In Delphi the cleanest way to do that is to make the array a dynamic array. Even if you know the dimensions at compile time, you can use a dynamic array to move the storage off the stack and onto the heap.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

An array can be as large as memory allows. But if it's a local variable, or if you pass it by value to some method, then beware, you can easily get out of stack.

nullptr
  • 11,008
  • 1
  • 23
  • 18
2

Choosing the right data structure is something I can only advise you on. Much of it will depend on how populated the array will be. A sparse array might well work if the array is large but lightly populated.

Personally, I'd code up a custom list class to contain TCube instances. This has several advantages over an array. Firstly, it will consume memory dynamically. Secondly, you can add additional methods to this class to suit your applications.

Andy_D
  • 2,340
  • 16
  • 21