SA-MP Forums Archive
Array Index Out Of Bounds issue. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Array Index Out Of Bounds issue. (/showthread.php?tid=600907)



Array Index Out Of Bounds issue. - Dokins - 14.02.2016

Confused by this error. It's returning in the server log (server console). When I use the function.

The line is where the loop starts as in the first line after the for.

I'm making it so anytime a 911 call is placed they move up on the police computer I.e a new call equals 0 so the old 0 should move to slot 1 (up to 15 calls on the list with the latest at the top.

pawn Код:
format(string, sizeof(string), "OPERATOR: 'Okay, we have located your call to being around %s, please remain calm and await emergency services.'", GetLocation(playerid));
            SendClientMessage(playerid, COLOUR_BLUE, string);
            format(string, sizeof(string), "%s", GetLocation(playerid));
            strcpy(PoliceCall[0][pol_loc], string);
            strcpy(PoliceCall[0][pol_in], PoliceCallP[playerid][pol_in]);
            PoliceCall[0][pol_rl] = PoliceCallP[playerid][pol_rl];
            PoliceCall[0][pol_tih] = THrs;
            PoliceCall[0][pol_tim] = TMins;
            for(new x = 0; x < 15; x++)
            {
                PoliceCall[x + 1][pol_rl] = PoliceCall[x][pol_rl];
                PoliceCall[x + 1][pol_tih] = PoliceCall[x][pol_tih];
                PoliceCall[x + 1][pol_tim] = PoliceCall[x][pol_tim];
                strcpy(PoliceCall[x + 1][pol_loc], PoliceCall[x][pol_loc]);
                strcpy(PoliceCall[x + 1][pol_in], PoliceCall[x][pol_in]);
                strcpy(PoliceCall[x + 1][pol_nm], PoliceCall[x][pol_nm]);
            }



Re: Array Index Out Of Bounds issue. - Pottus - 14.02.2016

Out of bounds are one of the most common errors and easiest to fix looking at your code it is quite clear why you are having issues.

Код:
			for(new x = 0; x < 15; x++)
			{
			    PoliceCall[x + 1][pol_rl] = PoliceCall[x][pol_rl];
This will OOB because PoliceCall has only 15 indexes but you are adding one, rule of thumb when doing looping like this do it like this.

Код:
#define             MAX_POLICE_CALLS            15

for(new x = 0; x < MAX_POLICE_CALLS-1; x++)
{
    PoliceCall[x + 1][pol_rl] = PoliceCall[x][pol_rl];
You still may need to do some logic re-working I don't know I don't have the code in front of me to test.


Re: Array Index Out Of Bounds issue. - PrO.GameR - 14.02.2016

I don't know what confused you, but it's perfectly obvious that when x becomes 14, you will have a PoliceCall[15] which is not a valid array index, hence it's "out of bounds" of your array define, as Pottus explained, it's perfectly easy to fix.


Re: Array Index Out Of Bounds issue. - Dokins - 14.02.2016

Thanks Pottus, Honestly. I knew it was something silly, I forgot about the part of adding one onto it, that makes a lot of sense. I appreciate it. I honestly just had a mind blank.

ProGamer, I feel your comment was unnecessary because sometimes it just takes a fresh pair of eyes to notice something silly. I think the 'perfectly obvious' and the subtle sarcasm are just unfair and not needed.