Raised This Month: $51 Target: $400
 12% 

Solved Why 33+32?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nG_getwreck
Senior Member
Join Date: Oct 2020
Location: Philippines from South K
Old 12-30-2020 , 11:49   Why 33+32?
Reply With Quote #1

Hello, i'm just confused.

Why plugin authors use [33][32]? It's should [32][32] right? Since maxplayers in server is only 32.

Code example: The code is from admin check plugin


new adminnames[33][32]
this matrix is created with 33 rows and 32 columns to store names. Since max players are 32, the column 32 represents each player’s name. Why need 33 rows and why need 2D array?
__________________

Last edited by nG_getwreck; 01-12-2021 at 13:13.
nG_getwreck is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-30-2020 , 11:58   Re: Why 33+32?
Reply With Quote #2

It must be 33 as the elements in the array sized at 32 are accessed with 0 to 31. If you size it at 32, you will get index out of bounds if a player connects on the last slot (32).

sizing at 33 allows slots of 0 to 32. The first slot of 0 is technically a waste but this required to allow 32 to be a valid slot.

The second part of your example variable is sized at 32 for the max length of a string value.
__________________

Last edited by Bugsy; 12-30-2020 at 12:00.
Bugsy is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 12-30-2020 , 12:04   Re: Why 33+32?
Reply With Quote #3

I'm not quite sure on how to answer your first question, as it just goes in my head without even thinking on how to create those arrays. This might help you a bit https://wiki.alliedmods.net/Pawn_Tutorial#Arrays
https://wiki.alliedmods.net/index.ph...nsional_Arrays

The nessecarity of the 2D array depends on how it's used in the code. Mostly you're creating 2D arrays to avoid using multiple arrays for the same purpose. (storing weapon info, players info, menu items, menu prices, etc...)

There's also something i found coded by bugsy which could help you understand on how and why these arrays are declared this way. (Look at how their array sizes are different, and why they are.)

https://forums.alliedmods.net/showpo...80&postcount=5
__________________

Last edited by Napoleon_be; 12-30-2020 at 12:08.
Napoleon_be is online now
Send a message via Skype™ to Napoleon_be
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-03-2021 , 05:22   Re: Why 33+32?
Reply With Quote #4

Quote:
Originally Posted by nG_getwreck View Post
new adminnames[33][32]
this matrix is created with 33 rows and 32 columns to store names. Since max players are 32, the column 32 represents each player’s name. Why need 33 rows and why need 2D array?
Bugsy already explained why we use 33 instead of 32 so I'm going to explain why you need a 2D array.

For a moment, let's assume you are building an XP system and you want to store how many points each player has. For this you would use an array like this:
PHP Code:
new playerPoints[33
Each cell in this array is able to store a 4 bytes integer, meaning you can do things like:
PHP Code:
//give 3200 points to the player with index "id".
playerPoints[id] = 3200 
Now, imagine you also want to store rank(where each rank is a string like "Rank1", "Rank2"). What happens if you try the following:
PHP Code:
new playerRank[33]
playerRank[id] = "Rank1" 
Remember I said each cell can hold 4 bytes of data. How many bytes do you need in order to store "Rank1"? In pawn, a string is an array of characters. "Rank1" contains 5 characters, so 5 characters * 4 bytes = 20 bytes.
Oops, your playerRank array can only hold 4 bytes in each cell, but your strings have 20 bytes, you need more space!

The solution is to use a 2D array or a matrix.
PHP Code:
new playerRank[33][5
Intuitively, what happens here is now each slot in the array(0, 1, ..., 32) is made up of 5 cells instead of only one. Therefore, each slot can now hold 5 cells * 4 bytes each cell = 20 bytes(just perfect for your rank strings).

To visualize this:
When you do playerRank[33] you end up with something like this:
Code:
playerRank[0] -> [3200]
playerRank[1] -> [1500]
...
playerRank[32] -> []
([] means a space where you can put a value)
When you do playerRank[33][5] you have something like:
Code:
playerRank[0] -> [][][][][]
playerRank[1] -> [R][a][n][k][2]
...
playerRank[32] -> [][][][][]
Someone who knows C may notice I'm dancing around the notion of "array of pointers to arrays". But if you do know C, I believe this is the best way to understand this.
View the 2D array as an array where each slot is a pointer(or link) to an array of size 5.

Another way is to view the matrix in one line(called a linearised matrix):
Code:
playerRank[0][0] playerRank[0][1] ... playerRank[0][4] | playerRank[1][0] playerRank[1][1] ... playerRank[1][4] | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row                                                            second row                                                            last row
(each row has 5 slots and can store a string of 5 characters or less).

If you did:
PHP Code:
copy(playerRank[0], "Rank1")
copy(playerRank[1], "Rank2")

//which is the same as:
playerRank[0][0] = 'R'
playerRank[0][1] = 'a'
playerRank[0][2] = 'n'
playerRank[0][3] = 'k'
playerRank[0][4] = '1'

playerRank[1][0] = 'R'
playerRank[1][1] = 'a'
playerRank[1][2] = 'n'
playerRank[1][3] = 'k'
playerRank[1][4] = '2' 
The memory would look like:
Code:
'R' 'a' 'n' 'k' '1' | 'R' 'a' 'n' 'k' '2' | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row               second row                                                            last row
For your original example, replace 5 with 32.
__________________

Last edited by HamletEagle; 01-03-2021 at 05:31.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-04-2021 , 00:31   Re: Why 33+32?
Reply With Quote #5

For more info, take a look at the programming tutorials here.
__________________
fysiks 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 05:56.


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