SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 19/05/2018, 04:30 PM   #1
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,569
Reputation: 338
Default Array index out of bounds problem! [How to acknowledge and fix]

Array index out of bounds


So basically a few people been running into this problem for a while and i just tried searching but didnít find something that defines the issue.

Array index out of bounds" What does that mean?

Basically, you first must have a variable with an actual array, iíll start off simple, iíll assume that you know what an array is since this is not the purpose of the tutorial.

PHP Code:
 new Cars[20]; 
So the array of that variable is 20 cells, 19 for usage and 1 for null (in case of a string but in this case its still 20 for usage but you end with 19 since the cell count starts at 0 which makes sense in both cases). Which means you canít do this: new Cars[0]; Thatís impossible, but this: new Cars[1]; leaves out Cars[0] for usage and 1 for null if its a string or because the cells count starts off at 0.

"Index out of bounds" that means out of boundaries, breaching the number of cells the array can hold will result in the Array index out of bounds issue.

1- A simple variable:

Say you got a variable that holds cars like the one we used above^ and you did this:

PHP Code:
 Cars[20] =CreateVehicle.. 
Thats an index out of bounds if the compiler doesnít sign it off the logs of the server should. (applies to all numbers above 20 and so)

However thats too simple, another example would be this issue i found online:

2- A loop:

PHP Code:
public OnPlayerObjectMoved(playerid,objectid

    for ( new 
0<MAX_OBJECTSi++ ) 
    { 
        if ( 
objectid == A_Objects][playerid] ) 
        { 
            if ( 
IsValidPlayerObjectplayeridA_Objects][playerid] ) ) 
            { 
                  
DestroyPlayerObjectplayeridA_Objects][playerid] ); 

                
PlayerObjects[playerid]--; 
                if ( 
PlayerObjects[playerid] == && Objects_Hit2[playerid] != 200
                  { 
                      
Objects_Missed[playerid] = 200 Objects_Hit2[playerid]; 
                        
MoveObjectsForPlayerAgainplayerid ); 
                } 
            } 
        } 
    } 
    return 
1

Now this code generated an array index out of bounds, what did he do wrong?

This:

Code:
 new A_Objects[50][playerid];
Was his variable, and obviously the loop caused the variable Ďií to go over 50 since MAX_OBJECTS is over 50, how to fix it? change the array size to the max_objects or make the loop < 50 not < MAX_OBJECTS.

Another issue is the invalid values, many which tend to happen when you somehow pass an invalid_player_id or invalid_vehicle_id thats somewhere close to 65535 if iím not mistaken.

I dunno how to give you an example to this but maybe OnPlayerTakeDamage.

For example:

3- Invalid values:


PHP Code:
public OnPlayerTakeDamage(playeridissuerid Ö../ETC 
{     
    
PlayerInfo[issuerid][Damager] = true;     
    return 
1

This^ is a mistake, why?

OnPlayerTakeDamage is called whenever u take damage even if you fall, who damaged you when you fell? the issuerid in that case is INVALID_PLAYER_ID which is going to be out of the array boundaries of the variable
Code:
 new PlayerInfo[MAX_PLAYERS][Info];
Because we all know that INVALID_PLAYER_ID is way ahead of the MAX_PLAYERS size (1000 by default). NOTE: This doesnít only happen with OnPlayerTakeDamage, OnPlayerDeath as well and more i assume.

Can be evaded by using the following:
PHP Code:
 IsPlayerConnected(playerid// in case of a player IsValidVehicle(vehicleid) // in case of a vehicle Ö//etc 

4- Variable value within a variable array:


This is basically like #1 mixed with #2 and #3 but instead of a simple number you put other variables inside the array whether its from an enum or just a simple one, example of a problem that i myself had before:

Code:
 
for(new x; x < MAX_VEHICLES; x++) 
{     
    if(!IsValidVehicle(x)) continue;// Just like we saw in #3 but this isnt the case here  
    if(PlayerInfo[ VehicleInfo[x][VehicleDriver] ][PlayerValue])     
    {         
        //ETC
Now what do we have here as a problem? first let me fill you in on the variables:
Code:
 
new PlayerInfo[MAX_PLAYERS][PInfo]; 
new VehicleInfo[MAX_VEHICLES][VInfo];
So now we know how the arrays are, whatís the problem with the code above? Obviously, this was made to extract out playerids from the vehicleids by looping through all cars.

We exclude invalid cars and then loop within the MAX_VEHICLES array, you can see PlayerInfo which is a variable alone mixed with VehicleInfo another variable i colored them to remove confusion.

When does that array go out of its boundaries? out of that cell BEFORE the last one?

Invalid, just like #3 because previously i had filled that array with invalid_player_ids, so basically the vehicle looped to check for, for example vehicle id 50 was empty, it had no driver, so this:
Code:
 VehicleInfo[ x=50 ] [VehicleDriver]
Was = to INVALID_PLAYER_ID and when we put THAT into:

PlayerInfo[ INVALID_PLAYER_ID ] [PlayerValue]
Which is the same as
PlayerInfo[ VehicleInfo[x][VehicleDriver ][PlayerValue]

^ Now how does that look like? just like #3 you put INVALID_PLAYER_ID thatís above MAX_PLAYERS. It can get very twisted depending on your code, this was for sure the most complex one you just need to keep digging for the root of the problem.

Fix for it:
Code:
 if(VehicleInfo[x][VehicleDriver] != INVALID_PLAYER_ID)
Ending

Thatís about it! you just gotta look hard and find out where you went wrong based on this concept, if thereís an idea im missing let me know in the comments! hope it helped out, cya.
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link



Last edited by RogueDrifter; 01/08/2018 at 10:57 PM.
RogueDrifter is offline   Reply With Quote
Old 19/05/2018, 04:35 PM   #2
Freshncool
High-roller
 
Freshncool's Avatar
 
Join Date: Aug 2014
Location: Kingdom of Pakistan Reputation: 9999
Posts: 1,223
Reputation: 348
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Can now indent properly.

5 Stars.
Freshncool is online now   Reply With Quote
Old 19/05/2018, 04:35 PM   #3
Uproar
Gangsta
 
Uproar's Avatar
 
Join Date: Feb 2018
Location: India
Posts: 564
Reputation: 128
Default Re: Array out of bounds problem! [How to aknowledge and fix]

W0w mind blowing nice 1 brother!
+rep ofc (you know what to do next)
Much love <3
__________________
Uproar is offline   Reply With Quote
Old 19/05/2018, 04:38 PM   #4
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,569
Reputation: 338
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Quote:
Originally Posted by Freshncool View Post
Can now indent properly.

5 Stars.
I'm getting tears :')

Quote:
Originally Posted by Uproar View Post
W0w mind blowing nice 1 brother!
+rep ofc (you know what to do next)
Much love <3
Yes
<3
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 19/05/2018, 04:41 PM   #5
iAmir
Big Clucker
 
iAmir's Avatar
 
Join Date: May 2017
Posts: 84
Reputation: 133
Default Re: Array out of bounds problem! [How to aknowledge and fix]

woah I learned something new today thanks for showing me the light _/\_!
may allah bless u
N1cė Tut0r!al m4tÍ

Quote:
Originally Posted by Uproar View Post
(you know what to do next)
: smug :
__________________
Quote:
Originally Posted by Freshncool
haha
iAmir is offline   Reply With Quote
Old 19/05/2018, 04:48 PM   #6
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,569
Reputation: 338
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Quote:
Originally Posted by iAmir View Post
woah I learned something new today thanks for showing me the light _/\_!
may allah bless u
N1cė Tut0r!al m4tÍ


: smug :
Done :') NICE REP AMOUNT (it got fucked was 99)
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link



Last edited by RogueDrifter; 20/05/2018 at 09:51 AM.
RogueDrifter is offline   Reply With Quote
Old 19/05/2018, 04:56 PM   #7
Eoussama
High-roller
 
Eoussama's Avatar
 
Join Date: Jul 2016
Location: Kingdom of Morocco // Tangier
Posts: 1,259
Reputation: 227
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Quote:
Originally Posted by RogueDrifter View Post
OnPlayerTakeDamage is called whenever u take damage even if you fall, who damaged you when you fell? the issuerid in that case is INVALID_PLAYER_ID which is going to be out of the array boundaries of the variable
That would explain a lot of crashes lately, great tutorial, Jumped in just because I had nothing else to do, but unexpectedly, I did learn something, +rep.

EDIT: must spread some reps befor reping you.
__________________

|===[Web taxi]===|
List of my work
Github
Pastebin

|===[Interesting topics]===|
Semantic Versioning


Eoussama is online now   Reply With Quote
Old 19/05/2018, 05:15 PM   #8
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,569
Reputation: 338
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Quote:
Originally Posted by Eoussama View Post
That would explain a lot of crashes lately, great tutorial, Jumped in just because I had nothing else to do, but unexpectedly, I did learn something, +rep.

EDIT: must spread some reps befor reping you.
Yep its also linked to other callbacks such as OnPlayerDeath (such as dying by falling!) glad it was helpful :)
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 19/05/2018, 05:30 PM   #9
Verc
Big Clucker
 
Verc's Avatar
 
Join Date: Apr 2018
Location: 🇯🇵
Posts: 180
Reputation: 29
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Great.
It's 65535 by the way.
Verc is offline   Reply With Quote
Old 19/05/2018, 05:32 PM   #10
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 857
Reputation: 151
Default Re: Array out of bounds problem! [How to aknowledge and fix]

Mashallah
@Pottus your wish came true
__________________


Interested in up to 500 slots hosting for $5 only? PM me

Battlezone is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array index out of bounds problem ohmy Scripting Help 4 22/12/2014 09:18 AM
Array Out of bounds problem IceBilizard Scripting Help 0 13/12/2014 12:35 PM
[HELP] Problem with (array index out of bounds) GBLTeam Scripting Help 2 20/11/2014 10:12 PM
[Help]Array index out of bounds Problem Mamal75 Scripting Help 23 16/10/2014 12:27 PM
[Array index out of bounds] problem. rappy93 Scripting Help 24 17/05/2014 09:37 PM


All times are GMT. The time now is 10:40 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.