Autor Beitrag
anx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Fr 04.12.09 22:49 
Hallo zusammen,
warum gibt das in Delphi keinen Fehler:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
 arr:array[0..7of integer; 
 size:integer;

for i:=0 to 8 do //Array ist doch zu klein???
  arr[i]:=random(50);
 for i:=0 to 8 do
  Label1.Caption:=Label1.Caption + IntToStr(arr[i]) + '       ';


Vielen Dank
lg, anx
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Fr 04.12.09 22:52 
Weil du die Bereichsprüfung in den Compileroptionen nicht aktiviert hast ;)
anx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Fr 04.12.09 22:55 
Achso...aber selbst wenn es der Kompiler nicht merkt, muss es doch Probleme auf der Ebene des Maschinencodes geben? Ich habe nun schlicht weg zu wenig Elemente!

Vielen Dank
lg, anx
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Fr 04.12.09 22:59 
Naja ... du schreibst an eine Adresse die hinter dem Array liegt. Das kann gut gehen, solange da nichts wichtiges ist was du überschreibst.

Im Maschinencode steht da ja im Grunde nur "Schreibe den Wert 15 an Adresse $984A789B" - und auf der Ebene kann man nicht mehr nachprüfen ob das in Ordnung ist ;)

(Wenn du die Bereichsprüfung aktivierst wird halt jedesmal bei einem Arrayzugriff vorher der Index überprüft - ist dementsprechend auch einen Hauch langsamer)
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 05.12.09 00:24 
Hey,

um wieviel is das langsamer?

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 05.12.09 02:23 
Wenn es dich interessiert, kannst du es gern nachmessen, aber da die Option normalerweise nur in Debug-Builds aktiviert sein sollte, ist das relativ egal, oder :nixweiss: ?

_________________
>λ=
ffgorcky
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 573

WIN XP/2000 & 7Prof (Familie:Win95,Win98)

BeitragVerfasst: Sa 05.12.09 11:17 
Ich würde das eher so schreiben:
ausblenden Delphi-Quelltext
1:
2:
for i:=0 to length(arr)-1 do
  arr[i]:=random(50);


...aber warum er bei Deinem Code keinen Fehler gibt, weiß ich nicht...
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Sa 05.12.09 16:35 
also ich eher:

ausblenden Delphi-Quelltext
1:
2:
for i:=0 to High(arr) do
  arr[i]:=random(50);


dadurch erspar ich mir das -1 ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Sa 05.12.09 18:28 
oder
ausblenden Delphi-Quelltext
1:
2:
for i := low(arr) to high(arr) do
  arr[i]:=random(50);

Dürfte auch gehen, wenn ich mich nicht täusche =D