Array index out of bounds when trying to read from dealership vehicles
#1

As the title says, I'm trying to make a dynamic dealership system in my server, but for some reason it doesn't work and Crash Detect gives me the "out of bounds" error.

This function is called whenever the player enters one of the dealership markers.
PHP код:
ShowDealerShipVehicles(playeriddealershipid
{
    new 
dealershiptitle[128], vehiclestring[128], holder[512];
    
    for(new 
isizeof(DealerShipVehicle); i++)
    {
        
format(vehiclestringsizeof(vehiclestring), "{FFFFFF}%s {FFFFFF}(%d)\n"vehiclestringVehicleName(DealerShipVehicle[i][Model]), DealerShipVehicle[i][Price]);
        
strcat(holdervehiclestringsizeof(holder));
        
//if(DealerShipVehicle[i][DealerShipID] == dealershipid) {
        //}
    
}
    
format(dealershiptitlesizeof(dealershiptitle), "Vehicle Menu for %s!"DealerShipData[dealershipid][dsName]);
    
ShowPlayerDialog(playeridDIALOG_DEALERSHIP_VEHICLESDIALOG_STYLE_LISTdealershiptitleholder"Buy""Cancel");
    return 
1;

To be sure, here's the part of my OnPlayerEnterDynamicCP:
PHP код:
for(new 0sizeof(DealerShipData); i++)
    {
        if(
IsPlayerInRangeOfPoint(playerid1.5DealerShipData[i][dsX], DealerShipData[i][dsY], DealerShipData[i][dsZ]))
        {
            
watchingDealerShip[playerid] = i;
            
ShowDealerShipVehicles(playeridwatchingDealerShip[playerid]+1);
            break;
        }
    } 
The dialog doesn't even show up either.

EDIT: Here's the error from the server:
Код:
[21:26:41] [debug] Run time error 4: "Array index out of bounds"
[21:26:41] [debug]  Accessing element at negative index -400
[21:26:41] [debug] AMX backtrace:
[21:26:41] [debug] #0 000471b8 in VehicleName (modelid=0) at crp.pwn:3048
[21:26:41] [debug] #1 000482b8 in ShowDealerShipVehicles (playerid=0, dealershipid=1) at crp.pwn:3375
[21:26:41] [debug] #2 00024224 in public OnPlayerEnterDynamicCP (playerid=0, checkpointid=21) at crp.pwn:1139
Reply
#2

Posting what actually crashdetect printed would be useful otherwise it's just guessing.

For index -400, check VehicleName (model ID must be between 400-611 otherwise run time error 4).
For other index, see this:
pawn Код:
DealerShipData[dealershipid][dsName]
In case DealerShipData array is full and you are in range with the last point, dealershipid will be the last index + 1 which is actually out of bounds.

EDIT:

Yes, it was the name. DealerShipVehicle[i][Model] is 0 so either the vehicle does not exist or you forgot to assign the model ID.

Modify VehicleName and if the model ID is not between 400-611, do not retrieve the name from the array. Let it be NULL. You can also check in the loop, if the vehicle does not exist, skip to the next variable iterator (continue;).
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Posting what actually crashdetect printed would be useful otherwise it's just guessing.

For index -400, check VehicleName (model ID must be between 400-611 otherwise run time error 4).
For other index, see this:
pawn Код:
DealerShipData[dealershipid][dsName]
In case DealerShipData array is full and you are in range with the last point, dealershipid will be the last index + 1 which is actually out of bounds.

EDIT:

Yes, it was the name. DealerShipVehicle[i][Model] is 0 so either the vehicle does not exist or you forgot to assign the model ID.

Modify VehicleName and if the model ID is not between 400-611, do not retrieve the name from the array. Let it be NULL. You can also check in the loop, if the vehicle does not exist, skip to the next variable iterator (continue;).
Thanks, I fixed it by adding a check that looks if the model id is between 400 and 611.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)