AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Strange problem "Run time error 4: index out of bounds" (https://forums.alliedmods.net/showthread.php?t=171560)

FEELiFE 11-07-2011 14:11

Strange problem "Run time error 4: index out of bounds"
 
This error fills my logs every day, and I still don't know why. Here are the lines where is the bug:

PHP Code:

if(is_user_connected(id)) {
        
iPtz[id] -= item_cost[get_info];


The error is:
L 11/07/2011 - 20:46:10: [AMXX] Displaying debug trace (plugin "items.amxx")
L 11/07/2011 - 20:46:10: [AMXX] Run time error 4: index out of bounds

Any ideas?

Sylwester 11-07-2011 14:26

Re: Strange problem "Run time error 4: index out of bounds"
 
You get index out of bounds either on iPtz or item_cost

If you can't find the error yourself only with looking at the code and you can't post more code then you should start debugging it like this:
PHP Code:

 if(is_user_connected(id)) {
     if(
id >= sizeof iPtz)
         
log_amx("id value (%d) out of bounds, should be (0..%d)"idsizeof iPtz -1)
     if(
get_info >= sizeof item_cost || get_info 0)
         
log_amx("get_info value (%d) out of bounds, should be (0..%d)"get_infosizeof item_cost -1)
     
iPtz[id] -= item_cost[get_info];
 } 


FEELiFE 11-08-2011 14:01

Re: Strange problem "Run time error 4: index out of bounds"
 
Quote:

Originally Posted by Sylwester (Post 1592027)
You get index out of bounds either on iPtz or item_cost

If you can't find the error yourself only with looking at the code and you can't post more code then you should start debugging it like this:
PHP Code:

 if(is_user_connected(id)) {
     if(
id >= sizeof iPtz)
         
log_amx("id value (%d) out of bounds, should be (0..%d)"idsizeof iPtz -1)
     if(
get_info >= sizeof item_cost || get_info 0)
         
log_amx("get_info value (%d) out of bounds, should be (0..%d)"get_infosizeof item_cost -1)
     
iPtz[id] -= item_cost[get_info];
 } 


It returns: get_info value (-3) out of bounds, should be (0..2)

I use negative values for exceptions like the user doesn't have enough money to buy an item or has already the item.

Here is the code:
PHP Code:

new access,callbackinfo[3], get_info;
    
menu_item_getinfo(menu,item,accessinfo,2_,_callback);
    
get_info str_to_num(info);
    
    switch(
get_info)
    {
        case -
4:
        {
            
some code..
        }
        case -
3:
        {
            
some code..
        }
        case -
2:
        {
           If 
already has the item..
        }
        case -
1:
        {
            
Not enough money..
    }
    
    if(
is_user_connected(id)) {
        if(
is_user_connected(id)) {
            if(
id >= sizeof iPtz) {
                
log_to_file(log_file"[Item] id value (%d) out of bounds, should be (0..%d)"idsizeof iPtz -1);
            }
            if(
get_info >= sizeof item_cost || get_info 0) {
                
log_to_file(log_file"[Item] get_info value (%d) out of bounds, should be (0..%d)"get_infosizeof item_cost -1);
            }
            
iPtz[id] -= item_cost[get_info];
            
item_level[id][get_info]++;
        }
        
show_items_menu(id);
    } 

And the items arrays:
PHP Code:

new const item_name[][] =
{
    
"Item1",
    
"Item2"
};

new 
item_level[33][sizeof(item_name)+1];

new const 
item_cost[sizeof(item_name)+1] =
{
    
80,
    
50
}; 

So any ideas?

Sylwester 11-08-2011 15:15

Re: Strange problem "Run time error 4: index out of bounds"
 
In case of exception you don't execute part of code that you would execute in normal case.
PHP Code:

    switch(get_info)
    {
        case -
4:
        {
            
some code..
        }
        case -
3:
        {
            
some code..
        }
        case -
2:
        {
           If 
already has the item..
        }
        case -
1:
        {
            
Not enough money..
        }
        default:
        {
            
iPtz[id] -= item_cost[get_info];
            
item_level[id][get_info]++;
        }
    }
    
show_items_menu(id); 


FEELiFE 11-09-2011 04:06

Re: Strange problem "Run time error 4: index out of bounds"
 
Thank you Sylwester! It works perfectly now :). :bacon!:


All times are GMT -4. The time now is 14:20.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.