[Array index out of bounds] problem.
#1

[debug] Run time error 4: "Array index out of bounds"
[debug] Accessing element at index 10 past array upper bound 9
[debug] AMX backtrace:
[debug] #0 00066fb0 in ?? (0x00000001, 0x05f92ad4) from lsrp.amx
[debug] #1 0009a534 in public LoadDynamicHouses () from lsrp.amx


Okay, so I keep getting this error while I start my server. I tried to figure out where the problem was but I didn't succeed. What I noticed is that if I empty the "houses" table it doesn't return the error anymore. I checked and rechecked the table,but it seems legit. Anyone has any ideas ?

Below is the code.


pawn Код:
public LoadDynamicHouses()
{
    new rows, fields;
    new total = 0, house = 1, weapons[256];
    cache_get_data(rows, fields);
    if(rows)
    {
        while(total < rows)
        {
            HouseInfo[house][hID] = cache_get_row_int(total, 0);
            HouseInfo[house][hEntranceX] = cache_get_row_float(total, 1);
            HouseInfo[house][hEntranceY] = cache_get_row_float(total, 2);
            HouseInfo[house][hEntranceZ] = cache_get_row_float(total, 3);
            HouseInfo[house][hExitX] = cache_get_row_float(total, 4);
            HouseInfo[house][hExitY] = cache_get_row_float(total, 5);
            HouseInfo[house][hExitZ] = cache_get_row_float(total, 6);
            cache_get_row(total, 7, HouseInfo[house][hInfo], dbHandle, 128);
            cache_get_row(total, 8, HouseInfo[house][hOwner], dbHandle, 128);
            HouseInfo[house][hOwned] = cache_get_row_int(total, 9);
            HouseInfo[house][hLocked] = cache_get_row_int(total, 10);
            HouseInfo[house][hPrice] = cache_get_row_int(total, 11);
            HouseInfo[house][hLevelbuy] = cache_get_row_int(total, 12);
            HouseInfo[house][hRentprice] = cache_get_row_int(total, 13);
            HouseInfo[house][hRentable] = cache_get_row_int(total, 14);
            HouseInfo[house][hInterior] = cache_get_row_int(total, 15);
            HouseInfo[house][hWorld] = cache_get_row_int(total, 16);
            HouseInfo[house][hCash] = cache_get_row_int(total, 17);
            HouseInfo[house][hFurnitures] = cache_get_row_int(total, 18);
            cache_get_row(total, 19, weapons, dbHandle, 128);
            HouseInfo[house][hCheckPosX] = cache_get_row_float(total, 20);
            HouseInfo[house][hCheckPosY] = cache_get_row_float(total, 21);
            HouseInfo[house][hCheckPosZ] = cache_get_row_float(total, 22);
            HouseInfo[house][hRadio] = cache_get_row_int(total, 23);
            HouseInfo[house][hHouseOn] = 1;
            AssignHouseWeapons(house, weapons);
            if(HouseInfo[house][hOwned] == 0)
                format(msg, sizeof(msg), "House: %d\n%s\nPrice: $%d\nLevel: %d", house, HouseInfo[house][hInfo], HouseInfo[house][hPrice], HouseInfo[house][hLevelbuy]);
            else
                format(msg, sizeof(msg), "House: %d\n%s", house, HouseInfo[house][hInfo]);
            HouseInfo[house][hLabel] = CreateDynamic3DTextLabel(msg, COLOR_HOUSE, HouseInfo[house][hEntranceX], HouseInfo[house][hEntranceY], HouseInfo[house][hEntranceZ], 3.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100.0);
            HouseInfo[house][hCheckPoint] = CreateDynamicCP(HouseInfo[house][hEntranceX], HouseInfo[house][hEntranceY], HouseInfo[house][hEntranceZ], 1.5, -1, -1, -1, 2.0);
            if(HouseInfo[house][hFurnitures] > 0)
            {
                LoadHouseFurnitures(house);
            }
            house++;
            total++;
        }
    }
    format(msg,sizeof(msg), "Loaded %d dynamic houses from MySQL.", total);
    printf(msg);
    return 1;
}
Reply
#2

The index in arrays start from 0, not 1. By starting it at index 1, it goes out of bounds at the last index (last valid is 9 and you accessed element at index 10 (which is invalid)).
Reply
#3

So you're saying I should put it

house = 0 ?
Reply
#4

Yes. You store the original houseid in HouseInfo[house][hID] and perhaps the index is not equal with the houseid (don't get confused on those two).
Reply
#5

I put it 0, same error. Any other ideas ?
Reply
#6

Another possibility is that there are more rows in the database than the size of the array to store them.

Remove the "house" variable completely and keep only the "total". Replace in the HouseInfo array, the indexes with "total" and inside the while loop (at the top of it):
pawn Код:
if (total >= sizeof (HouseInfo))
{
    print("Error: There are currently more rows in the database for the houses than the size of the array to be stored.");
    break;
}
Reply
#7

There are no "house" indexes in HouseInfo to be replaced.Look , this is the enum :

pawn Код:
enum HOUSE_INFO
{
    hID,
    Text3D:hLabel,
    hCheckPoint,
    Float:hEntranceX,
    Float:hEntranceY,
    Float:hEntranceZ,
    Float:hExitX,
    Float:hExitY,
    Float:hExitZ,
    hInfo[128],
    hOwner[MAX_PLAYER_NAME],
    hOwned,
    hLocked,
    hPrice,
    hLevelbuy,
    hRentprice,
    hRentable,
    hInterior,
    hWorld,
    hCash,
    hFurnitures,
    hWeapon[10],
    hAmmo[10],
    hHouseOn,
    Float:hCheckPosX,
    Float:hCheckPosY,
    Float:hCheckPosZ,
    hRadio,
    hRadioOn,
    hRadioURL[256]
};
new HouseInfo[MAX_HOUSES][HOUSE_INFO];
Reply
#8

I meant in your code:
pawn Код:
HouseInfo[house][hID] = cache_get_row_int(total, 0);
HouseInfo[house][hEntranceX] = cache_get_row_float(total, 1);
...
You use the variable "house" as index, replace it with the "total" instead.
Reply
#9

I kinda did that already but I will do it again now. Thanks for your input.
Reply
#10

I did exactly as you said and I still got that error. The thing is that no indexes are defined as 10. I don't know where it gets that value.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)