SA-MP Forums Archive
array index out of bounds - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: array index out of bounds (/showthread.php?tid=264841)



array index out of bounds - Rickye - 28.06.2011

I have a problem in a piece of code, where dont find the soluction:

PHP код:
#define MAX_TASKS         100
enum CTask
{
    
int:iSleep,             
    
int:iRemainingTime,
    
int:iType,             
    
sParam1[256],       
    
sParam2[256],       
    
bool:bActive false,
};
new 
pTasks[MAX_TASKS][CTask];
forward CreateTask(int:Sleepint:Type, const Param1[], const Param2[]);
public 
CreateTask(int:Sleepint:Type, const Param1[], const Param2[])
{
    
//get a first non active pTasks to overwrite
    
new iEmptySlot = -1;
    for (new 
0MAX_TASKSi++)
    {
        if ( 
pTasks[i][bActive] ) continue;
        
iEmptySlot i;
        break;
    }
    
    if ( 
iEmptySlot == -1) return false;
    
    
pTasks[iEmptySlot][iSleep]             = Sleep
    
pTasks[iEmptySlot][iRemainingTime]     = Sleep//LINE 93
    
pTasks[iEmptySlot][iType]           = Type//LINE 94
    
pTasks[iEmptySlot][bActive]         = 1;
    
format(pTasks[iEmptySlot][sParam1], 256"%s"Param1); //LINE 96
    
format(pTasks[iEmptySlot][sParam2], 256"%s"Param2); //LINE 97
    
    
return true;

In the lines marked, give a "array index out of bounds".
Where is the error?

Код:
C:\Program Files\Rockstar Games\samp03csvr_R2-2_win32\gamemodes\rick.pwn(93) : error 032: array index out of bounds (variable "pTasks")
C:\Program Files\Rockstar Games\samp03csvr_R2-2_win32\gamemodes\rick.pwn(94) : error 032: array index out of bounds (variable "pTasks")
C:\Program Files\Rockstar Games\samp03csvr_R2-2_win32\gamemodes\rick.pwn(96) : error 032: array index out of bounds (variable "pTasks")
C:\Program Files\Rockstar Games\samp03csvr_R2-2_win32\gamemodes\rick.pwn(97) : error 032: array index out of bounds (variable "pTasks")

Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


4 Errors.



AW: array index out of bounds - Nero_3D - 28.06.2011

The enum variable always saves next unused constant number

Lets start in our enum
pawn Код:
enum CTask //normal mode (+= 1)
{
    int:iSleep, // = 0
    int:iRemainingTime, // = 1
    int:iType, // = 2
    sParam1[256], // = 3 - 258
    sParam2[256], // = 259 - 514
    bool:bActive = false, //normally 515 but we set it to 0 again
}; // => last constant number + 1 step => CTask = 1

new pTasks[MAX_TASKS][CTask];
Now it should be clear why the out of bounds error appeared

Since all variables are by default 0 (false) you dont need to set bActive to false


Re: array index out of bounds - Rickye - 28.06.2011

Wow.. nice explanation!

Now working..

Thanks.