Raised This Month: $32 Target: $400
 8% 

2d array index out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Croxeye.
Member
Join Date: Nov 2018
Old 12-01-2018 , 12:34   2d array index out of bounds
Reply With Quote #1

PHP Code:
enum _:playerTable {
    
nick[32],
    ...
}

new 
g_PlayerData[33][playerTable];

get_user_name(idg_PlayerData[id][nick], charsmax(g_PlayerData[][nick])); 
Code:
error 032: array index out of bounds (variable "g_PlayerData")
Croxeye. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-01-2018 , 13:06   Re: 2d array index out of bounds
Reply With Quote #2

It compiles fine for me.
__________________
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-01-2018 , 14:14   Re: 2d array index out of bounds
Reply With Quote #3

What you posted is correct as HamletEagle says. But I see you have '...' after nick so there may be something you removed that is causing the error. You need to post full code if you want to get an answer.
__________________
Bugsy is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-01-2018 , 17:56   Re: 2d array index out of bounds
Reply With Quote #4

PHP Code:
enum _:playerTable {
    
nick[32],
    
steamId[45],
    
password[45],
    
language[3],
    
boolisOnline false,
    
boolisVip false,
    
boolisAdmin false
}

new 
g_PlayerData[33][playerTable];

public 
client_connect(id) {
    new 
Query[512], Data[1];
    
    
Data[0] = id;
    
    
get_user_name(idg_PlayerData[id][nick], charsmax(g_PlayerData[][nick])); //error
    
    
formatex(Querycharsmax(Query), "SELECT * FROM `player` WHERE `player`.`nick` = '%s'"g_PlayerData[id][nick]);
    
SQL_ThreadQuery(MySQL_GlobalConnection"MySQL_LoadPlayer"QueryData1);
}

public 
MySQL_LoadPlayer(FailState,Handle:SQL_Query,Error[],Errcode,Data[],DataSize) {
    if(
FailState == TQUERY_CONNECT_FAILED) {
        
console_print(0"Load - Could not connect to SQL database.  [%d] %s"ErrcodeError);
    } else if(
FailState == TQUERY_QUERY_FAILED) {
        
console_print(0"Load Query failed. [%d] %s"ErrcodeError);
    }
    
    new 
playerId;
    
    
playerId Data[0];
    
    
console_print(0"playerId: %i"playerId); // prints: "playerId: 0"
    
    
    
if(SQL_NumResults(SQL_Query) != 0) {
        
SQL_ReadResult(SQL_Query1g_player[playerId][nick], charsmax(g_player[][nick])); //error
        
SQL_ReadResult(SQL_Query2g_player[playerId][steamId], charsmax(g_player[][steamId])); //error
        
SQL_ReadResult(SQL_Query3g_player[playerId][password], charsmax(g_player[][password])); //error
                
SQL_ReadResult(SQL_Query3g_player[playerId][language], charsmax(g_player[][language])); //error
        
g_player[playerId][bcoins] = SQL_ReadResult(SQL_Query6); //error
        
g_player[playerId][isOnline] = true;
    }

        
SQL_FreeHandle(SQL_Query);


Last edited by Croxeye.; 12-01-2018 at 18:07.
Croxeye. is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 12-01-2018 , 18:24   Re: 2d array index out of bounds
Reply With Quote #5

When I removed "= false" from the three booleans it worked.
Nothing actually wrong, so I'm gonna guess the compiler has a hard time interpreting in this case.
Enums are always a bit weird, keep them as simple as possible.
Booleans are always initialized as false.

Code:
enum playerTable {     nick[32],     steamId[45],     password[45],     language[3],     bool:isOnline,     bool:isVip,     bool:isAdmin,     bcoins }
__________________
Black Rose is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-01-2018 , 18:37   Re: 2d array index out of bounds
Reply With Quote #6

Thank you so much, now it works...
Croxeye. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-01-2018 , 18:58   Re: 2d array index out of bounds
Reply With Quote #7

That was definitely the problem. The enumerator is really just a 'sizer' for an array so setting it to false does not make sense. The enum provides you offsets based on items preceding it, making it easy and fast to work with constants/variable-names to access items in an array. Setting to false for something would need to be done on the g_PlayerData[] array that was sized by playerTable.

The enum items in playerTable hold the below values. If the compiler allowed this, doing "isOnline = false" would change its value from 125 to 0 which essentially would over-write data where 'nick' is stored.

nick=0
steamID=32
password=77
language=122
isOnline=125
isVip=126
isAdmin=127
bcoins = 128
__________________

Last edited by Bugsy; 12-01-2018 at 19:01.
Bugsy is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-01-2018 , 19:00   Re: 2d array index out of bounds
Reply With Quote #8

Just one more question: How to get number of not empty rows in array ?

PHP Code:
new achievements[50][achievementsTable]; //But I only have 5 achievements

for(new 0sizeof(achievements); i++) {
    new 
InfoStatus[198];
    
formatex(InfoStatuscharsmax(InfoStatus), "\yAchievements:");
    
    new 
menu menu_create(InfoStatus"achievements_menu_handler");
    
    for(new 
0sizeof(achievements); i++) {
        
formatex(InfoStatuscharsmax(InfoStatus),  "\w%s \r:: \r%i \y%s"achievements[i][name], achievements[i][rewardAmount], SYMBOL);

        
menu_additem(menuInfoStatus"0"0);
    }
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);

Croxeye. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-01-2018 , 19:04   Re: 2d array index out of bounds
Reply With Quote #9

Change achievements[50][achievementsTable] to achievements[5][achievementsTable]? Or do you mean the specific player?

If it's the player who only has 5, whatever stores his achievements, do a if ( player variable ) { add to menu }
__________________

Last edited by Bugsy; 12-01-2018 at 19:04.
Bugsy is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-01-2018 , 19:12   Re: 2d array index out of bounds
Reply With Quote #10

No, no.. I'm getting achievements from database and I should change [50] to [] because I dont know number of achiements and I always can add more..
Croxeye. is offline
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

Forum Jump


All times are GMT -4. The time now is 21:12.


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