tags:

views:

113

answers:

2

I have in Delphi application declared tables:

 x,y,z,r:array [1..10000000] of double;
 t1,t2,t3,t4:array [1..10000000] of integer;

Before everything was ok but now I get in some pcs error (in most pc:s error does not come) :

"The application failed to initialize properly (0xc0000005)"

If I change tables smaller:

 x,y,z,r:array [1..5000000] of double;
 t1,t2,t3,t4:array [1..5000000] of integer;

error disappears

+11  A: 

Your computer is running out of memory.

  • A double needs 8 bytes. Initialization of 4 arrays of 10.000.000 doubles uses 320.000.000 bytes.
  • An integer needs 4 bytes. Initialization of 4 integer arrays uses 160.000.000 bytes.

At startup you have effectively used up 480MB, not counting anything else.

Instead of allocating all memory at startup, you should use a generic or specialised container that automatically grows when more items are added.

Some containers that come to mind

Lieven
+4  A: 

Consider using dynamic arrays, so that you only allocate memory as you need it.

x,y,z,r:array of double;
t1,t2,t3,t4:array of integer;

To add an element to the array:

SetLength(x, 1);
x[0] := 0.0;

Although in a previous question I asked I learnt that this isn't wholly necessary, I tend to always call

Finalize(x);

at the end, just to be sure.

_J_
Don't call Finalize() directly. Use SetLength(..., 0) instead.
Remy Lebeau - TeamB
What's the difference, Remy?
_J_
Normally, `Finalize` leaves the variable in an indeterminate state. If the program noticed the variable was non-nil when it went out of scope, it would attempt to release the array that the variable still appears to refer to, causing a crash. But it looks like `Finalize` on a dynamic array is a special case. Instead of going to one of the `Finalize` functions in *System.pas*, the compiler turns the call into code equivalent to assigning nil to the variable, which in turn is equivalent to setting the length to zero, which in turn is equivalent to simply letting the variable fall out of scope.
Rob Kennedy