Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-02

I don't Understand Array

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luis Del Aguila Mejía

When declare a Array variable with character, The compiler add 3 character, see the next code :

var
   antes:char;
   m     : array [0..3] of char;
   despues : char;
begin
    antes:='a';
    m:='hola';
    despues:='z';
    Writeln('m[-4] : ',m[-4]);     // variable antes
    Writeln('m[-3] : ',m[-3]);     // 1 byte
    Writeln('m[-2] : ',m[-2]);     // 1 byte
    Writeln('m[-1] : ',m[-1]);     // 1 byte
    Writeln('m[0] : ',m[0]);        //h
    Writeln('m[1] : ',m[1]);        //o
    Writeln('m[2] : ',m[2]);       //l
    Writeln('m[3] : ',m[3]);       //a
    Writeln('m[4] : ',m[4]);       //z variable despues
    Writeln('Tamanio de m : ',Sizeof(m));
    Write('Presione Enter ... ');
    Readln()
end;

Att.
Luis Del Aguila

Reply by email

Unknown author
to Luis Del Aguila Mejía
When declare a Array variable with character, The compiler add 3 character, see the next code :

Turn on range checking and you will get a range check error. What you get is completely normal; the code accesses the variables 'before' m[0]; If range checking is off, this is allowed.

Michael.

Show quoted text 31 lines
var antes:char; m : array [0..3] of char; despues : char; begin antes:='a'; m:='hola'; despues:='z'; Writeln('m[-4] : ',m[-4]); // variable antes Writeln('m[-3] : ',m[-3]); // 1 byte Writeln('m[-2] : ',m[-2]); // 1 byte Writeln('m[-1] : ',m[-1]); // 1 byte Writeln('m[0] : ',m[0]); //h Writeln('m[1] : ',m[1]); //o Writeln('m[2] : ',m[2]); //l Writeln('m[3] : ',m[3]); //a Writeln('m[4] : ',m[4]); //z variable despues Writeln('Tamanio de m : ',Sizeof(m)); Write('Presione Enter ... '); Readln() end; Att. Luis Del Aguila _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Gabor DEAK JAHN

At 02/16/2001 06:30, you wrote:

Dear Luis,

Show quoted text 9 lines
When declare a Array variable with character, The compiler add 3 character, see the next code : var     antes:char;     m     : array [0..3] of char;     despues : char; begin      Writeln('m[-4] : ',m[-4]);     // variable antes

If you declare a variable with indexes between [m..n], this means that you are allowed only to use those indexes when you use the array. In your case, m[0], m[1], m[2] and m[3] are the only allowed expressions (or, of course, an m[i] where i has a value of 0, 1, 2 or 3). The compiler notifies you with the warning 'Range check error while evaluating constants'. Actually, this should be labeled an error, not a warning, but its text clearly says: error.

Variables are completely independent. If you declare your antes, then m, then despues, you can make no assumptions about where they will be in the memory. It is far from certain that antes will be before m, even less certain that it will be immediately before it. The compiler is at perfect liberty to place these variables in any way it sees fit and for reasons of performance, they are usually aligned on multiple byte boundaries, meaning that up to a couple of bytes of hole can be between any two variables. This is the reason why you see three additional bytes between antes and m, the default alignment is at 4-byte boundaries.

If for any reason you want to keep some variables together, knowing their exact sequence, location and size, you have to use the device the language offers for this purpose, namely, a record (specifying the alignment as necessary).

Regards,

   Gabor DEAK JAHN

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
WWW: <http://www.tramontana.co.hu/>www.tramontana.co.hu E-mail:

Reply by email