2006-11-01

What is taught in schools...

Who said that Pascal is suitable language for the programming studying?

Well, about stack, heap, the difference between on-line storage, hard disk and above all why this separation is necessary I explained to my sister without a flub.

Took a time-out... I'm fussing about the syntax.

Such a doltish samples are given in books:
Type
vector = array[1..1000] of real;
p_vector = ^vector;

Var
a p_vector;

getmem(a,sizeof(vector));


Just an amazing possibility of declaring in the heap an array of fixed size.
Common sense waved me goodbye.

I had to dig in my old university programs.

Aha! To have a possibility of creation arrays of really beforehand unknown size a little trick is required:

row =  array[1..1] of real;


Little sister, have not you understood yet?

Look further:
Type
row = array[1..1] of real;
p_row = ^row;
Var
a p_row;
i integer;
Begin
getmem(a,10*sizeof(real));
for i := 1 to 10 do
a^[i] := i;
for i := 1 to 10 do
writeln(a^[i]);
End.


By declaring array of one element we deceive the compiler and later it allows us to refer to the array element by dereferencing the pointer.

Before this moment the inner organization of arrays was not very important for us but now the agreement that array elements are located close to each other has become critical.
Using it, we allocate memory for array with getmem function and calculate the size of the required piece just by multiplying the size of one element to their number. And we sure that the reference to an element number i is done by a^[i] expression.

Aha, and how many pupils spoiled by Pascal will guess how to create 2-D array, after all mentioned above?
Taking into account that the answer
row = array[1..1,1..1] of real;
is wrong?

I'm going to check my sister shrewdness.

No comments:

Post a Comment