Array index out of bounds
#1

Hi i have some problems with crashdetect.dll !



Code:
public CheckGas()
{
	for(new i=1; i<= MAX_VEHICLES; i++)
	{
			GetVehicleParamsEx(i,engine,lights,alarm,doors,bonnet,boot,objective);
			if(engine == VEHICLE_PARAMS_ON && alarmon[i] == 0)
			{
			    if(IsAPlane(i) == 0 && IsABoat(i) == 0 && IsABmx(i) == 0 && Gas[i] > 0  && i != 307 && i != 308 && i != 171 &&i != 172 && i != 173 && i != 174 && i != 161 && i != 162 && i != 163)
				{
					Gas[i]--;
					foreach(Player, o)
					{
					  new Float:Viteza = GetVehicleSpeed_P(o);
					  if(Viteza > 5 && GetPlayerState(o) == PLAYER_STATE_DRIVER && GetPlayerVehicleID(o) == i)
					  {
					    CarData[i-PreVehicle][cKM]++;
				        new query[MAX_STRING];
				        format(query, sizeof(query), "UPDATE mnxmasini SET KM='%d' WHERE id=%d", CarData[i-PreVehicle][cKM], i-PreVehicle);
				        mysql_query(query);
					  }
					}
				}
			}
	}
	foreach(Player, i)
	{
   	    if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
   	    {
       		new vehicle = GetPlayerVehicleID(i);
        	if(Gas[vehicle] >= 1)
	   		{
	   		    if(Gas[vehicle] <= 10) { PlayerPlaySound(i, 1085, 0.0, 0.0, 0.0); }
	   		}
   			else
           	{
              	GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
              	SetVehicleParamsEx(vehicle,VEHICLE_PARAMS_OFF,lights,alarm,doors,bonnet,boot,objective);
	        	GameTextForPlayer(i,"~w~~n~~n~~n~~n~~n~~n~~n~~n~Numai ai benzina",1500,3);
			}
		}
	}
	return 1;
}
[01:52:45] [debug] Run time error 4: "Array index out of bounds"
[01:52:45] [debug] Accessing element at index 500 past array upper bound 301
[01:52:45] [debug] AMX backtrace:
[01:52:45] [debug] #0 0006f8ec in public OnPlayerStateChange () from 1.amx
[01:52:45] [debug] Aborting...
Reply
#2

Check preVehicle value and Gas[] size
Reply
#3

PreVehicle = 618;
#define CAR_AMOUNT 1000 //Change to Your Vehicle Amount
new Gas[CAR_AMOUNT];
Reply
#4

Absolutely make sure you almost always define arrays sizes a with #define

#MAX_VEHICLES 1000
new VehicleList[MAX_VEHICLES];

#MAX_LABELS 300
new LabelList[MAX_LABELS];

This way when you do any sort of looping or accessing like player ids there will be minimal OOB errors.
Reply
#5

Define new Gas[MAX_VEHICLES] and i think sometimes i-PreVehicle can be < 0 that cause error. And i think you need yo start your first cycle from 0, not 1
Reply
#6

error 031: unknown directive
error 031: unknown directive
when i compile with
#MAX_VEHICLES 1000
#MAX_PLAYERS 100
I receive this errors! Thanks for helping me!
Reply
#7

#define MAX_VEHICLES 1000
#define MAX_PLAYERS 100
Reply
#8

The problem persist!
Reply
#9

Before #define
Use #undef MAX_VEHICLES
#undef MAX_PLAYERS
Reply
#10

This is only what it prints? I'd search at OnPlayerStateChange too. Anyways, debug your code to see where it stops.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)