Friday 15 March 2013

freepascal - Pascal - size of array -



freepascal - Pascal - size of array -

i have code, in input each number on new line until write 0. confused size of array. in begining declare array indices 1 5. when maintain writing , set more 5 numbers, seems still saved , later used in sec cycle. how possible? thought if run out of indices error.

program testarray; var hmez, i:integer; a:array [1..5] of integer; begin i:=0; repeat inc(i); readln(a[i]); if a[i]=0 hmez:=i-1; until a[i]=0; i:=1 hmez writeln(a[i]); end.

you overwriting memory doesn't belong a variable. sooner or later cause unhandled exception (run-time error) or other 'weird' behavior caused memory leak. unlike 'managed' languages such c# or java in pascal run-time checking of array indices optional feature, not mandatory, enforced, language feature.

pascal compilers have alternative turn on or off array index checks called 'range checking'. directive {$r+} turns on these range checks — see documentation here.

in case code seems 'work' because, likely, there's no other useful variable placed in memory after space allocated a. please note conincidence , may not case in runs of program. generally, programme as-is incorrect.

arrays freepascal

No comments:

Post a Comment