Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an array be too big?

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?

like image 207
Glen Morse Avatar asked Jul 30 '13 09:07

Glen Morse


People also ask

How large can an array size be?

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property.

What limits the maximum size of an array?

The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details.

Is an array limited in size?

The maximum allowable array size is 65,536 bytes (64K). Reduce the array size to 65,536 bytes or less. The size is calculated as (number of elements) * (size of each element in bytes).

What is a big array?

An array of different things or people is a large number or wide range of them.


3 Answers

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.
like image 152
David Heffernan Avatar answered Nov 08 '22 13:11

David Heffernan


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.

like image 35
nullptr Avatar answered Nov 08 '22 13:11

nullptr


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.

like image 2
Andy_D Avatar answered Nov 08 '22 15:11

Andy_D