Raised This Month: $ Target: $400
 0% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FEELiFE
Member
Join Date: Jul 2010
Old 11-07-2011 , 14:11   Strange problem "Run time error 4: index out of bounds"
Reply With Quote #1

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?
FEELiFE is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 11-07-2011 , 14:26   Re: Strange problem "Run time error 4: index out of bounds"
Reply With Quote #2

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];
 } 
__________________
Impossible is Nothing

Last edited by Emp`; 11-07-2011 at 15:19. Reason: small fix
Sylwester is offline
FEELiFE
Member
Join Date: Jul 2010
Old 11-08-2011 , 14:01   Re: Strange problem "Run time error 4: index out of bounds"
Reply With Quote #3

Quote:
Originally Posted by Sylwester View Post
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?
FEELiFE is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 11-08-2011 , 15:15   Re: Strange problem "Run time error 4: index out of bounds"
Reply With Quote #4

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); 
__________________
Impossible is Nothing
Sylwester is offline
FEELiFE
Member
Join Date: Jul 2010
Old 11-09-2011 , 04:06   Re: Strange problem "Run time error 4: index out of bounds"
Reply With Quote #5

Thank you Sylwester! It works perfectly now .
FEELiFE is offline
Reply



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

Forum Jump


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


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