PDA

View Full Version : Array index out of bounds


thimo
11/08/2013, 12:23 PM
Hello, this is my code:

public OnFilterScriptInit ()
{
printf (" \nSchool Project Loaded.\n");

SetTimer ("CheckValidTextDraws", 1000, 1);

RadarHud = TextDrawCreate (495.0, 200.0, "~b~Model: ~n~~r~Speed: kmph");
TextDrawFont (RadarHud, 2);
TextDrawLetterSize (RadarHud, 0.3, 1.0);

for (new i = 1; i <= MAX_VEHICLES; i++)
{
VehRadarID [i] = -1;
SpeedAndModel [i] = TextDrawCreate (545.0, 200.0, "~b~ ~n~~r~");
TextDrawFont (SpeedAndModel [i], 2);
TextDrawLetterSize (SpeedAndModel [i], 0.3, 1.0);
CheckingSpeed [i] = 0;
}

return 1;
}

And that gives me something back from crashdetect:

[13:45:15] [debug] Run time error 4: "Array index out of bounds"
[13:45:15] [debug] Accessing element at index 2000 past array upper bound 1999
[13:45:15] [debug] AMX backtrace:
[13:45:15] [debug] #0 000001f0 in public OnFilterScriptInit () from SchoolProject.amx

Why does it do that?

Misiur
11/08/2013, 12:29 PM
Probably you have somewhere in your code something like
new SpeedAndModel[2000]
//and/or
new VehRadarID[2000]
//and/or
new CheckingSpeed[2000]

You have 2 options:
#NAME#[2000]
//changes into
#NAME#[2001]
or
for (new i = 1; i <= MAX_VEHICLES; i++)
//changes into
for (new i = 0; i != MAX_VEHICLES; i++)

If you choose second option, you save one cell in each array, but you have to check your whole code and change all instances of this loop