Weird array statement in loop.



Hello,

While coding I came across this programming mistake which was not detected
by the Delphi 2007 compiler.

I simply forgot to assign a value to vArray[Index] but the code still
compiled ?!

Demonstration:

// *** Begin of Weird Code ***

program Project1;

{$APPTYPE CONSOLE}

{

Test weird array statement in loop.

Version 0.01 created on 21 may 2008 by Skybuck Flying

Weird code which is allowed to compile just fine ?:

for vIndex := 0 to vSize-1 do
begin
vArray[vIndex]; // does nothing, why is this allowed ? Weird ?!
end;

No warning, no hint, no error, no nothing ?!?!?!

}

uses
SysUtils;

procedure Main;
var
vArray : array of byte;
vIndex : integer;
vSize : integer;
begin

vSize := 10;
SetLength( vArray, vSize );

for vIndex := 0 to vSize-1 do
begin
vArray[vIndex]; // does nothing, why is this allowed ? Weird ?!
end;

// display array to do something usefull with it
for vIndex := 0 to vSize-1 do
begin
write( vArray[vIndex] : 4 );
end;
writeln;
end;

begin
try
Main;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
readln;
end.

// *** End of Weird Code ***

Bye,
Skybuck


.