Hi, Frank and others
Frank Heckenbach wrote:
Anyway, the real error is in the index computations. I won't tell you exactly where, since you might learn more if you find out yourself. ;-)
Ouch. Thank you. My original program had arrays indexed from 0, and I forgot to adjust for this when I changed it. The loops should index thusly (see corrected program below): for i := 0 to nx do for j := 1 to nx do for k := 1 to nx do Field[i]^[j+(nx*(k-1))] := 1.0 * i * j * k; ^^^^^ Two questions: 1) Why didn't the program crash when doing the assignments? It crashed on the write loop, but not on the previous loop. Aren't both accessing illegal memory?
2) (only distantly related) when I change the loop counter from Integer to Card, I get a compiler warning for every loop, something like 'condition is always "True"'. What is this about?
Frank also noted:
But you don't actually have to, since using a schema type is not only easier and avoids the difficult size and index computations, but also avoids the upper limit.
I mentioned this is a previous email. Briefly, I need to be able to walk through the cube, one layer at a time, and move information to another cube. I don't have enough memory for both cubes, so I have to be able to peel more dynamically.
Frank is right that a schema would make this easier and avoid index computations. The only real reason for sticking with the way I'm doing it, is that I'd like it to be portable to other (inferior!) compilers. Not an elegant justification, I know.
regards, a happier Toby
-------------------------------------------------------------------- program testmem;
const Xmax = 1025;
type Fieldary = array[1..2] of Real; FPary = array[0..Xmax] of ^Fieldary;
var Field : FPary; FSize, i,j, k, nx : integer;
begin writeln; write('Enter the size: '); {257 etc. work fine now} readln(nx); FSize := SizeOf(Real) * sqr(nx); {making a cube.} for i := 0 to nx do GetMem(Field[i], FSize); writeln('Memory allocated OK'); for i := 0 to nx do for j := 1 to nx do for k := 1 to nx do Field[i]^[j+(nx*(k-1))] := 1.0 * i * j * k; writeln('Done assigning. Writing...'); for i := 0 to nx do for j := 1 to nx do begin for k := 1 to nx do write(Field[i]^[j+(nx*(k-1))]:8:1, ' '); writeln; end; writeln('Freeing memory...'); for i := 0 to nx do if (Field[i] <> Nil) then FreeMem(Field[i]); end.