AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Array Size (https://forums.alliedmods.net/showthread.php?t=328376)

Napoleon_be 11-06-2020 17:44

Array Size
 
I've always been wondering if something like this is possible, and if it is, is it allowed to use?

PHP Code:

new szTemp[sizeof(szCommands)]; 

The reason why i'm not testing it is because i'll need it a lot in the upcoming update for my glowmenu.

Shadows Adi 11-07-2020 03:26

Re: Array Size
 
Yea, it's possible, because:
First you create an array which let say will hold 33 cells ( new szCommands[33] )

Then, you create a new array which will have the value sizeof(szCommands) which will be 33 cells lenght, so practically this is same as new szTemp[33], but less hardcoded, if it's value depends on szCommands lenght.

Napoleon_be 11-07-2020 08:28

Re: Array Size
 
Quote:

Originally Posted by Shadows Adi (Post 2724052)
Yea, it's possible, because:
First you create an array which let say will hold 33 cells ( new szCommands[33] )

Then, you create a new array which will have the value sizeof(szCommands) which will be 33 cells lenght, so practically this is same as new szTemp[33], but less hardcoded, if it's value depends on szCommands lenght.

What if i don't declare the length? It'd look something like this.

PHP Code:

new const szCommands[][] = {
    
"/glowmenu",
    
"/gm",
    
"/glow",
    
"!glowmenu",
    
"!gm",
    
"!glow"
}; 

PHP Code:

new szTemp[sizeof(szCommands)];
    for(new 
isizeof(szCommands); i++) {
        
formatex(szTempcharsmax(szTemp), "say %s"szCommands[i]);
        
register_clcmd(szTemp[i], "checkSettings");
    } 


HamletEagle 11-07-2020 09:10

Re: Array Size
 
Quote:

The reason why i'm not testing it is because i'll need it a lot in the upcoming update for my glowmenu.
This "reason" doesn't make any sense to me. Just test your code, it's not hard and it's so much faster than waiting for someone else to test for you.
Make a little plugin with only the code you are testing and try it.

Black Rose 11-07-2020 09:52

Re: Array Size
 
I might have gotten a bit excited here... Sorry.

It works because sizeof is not a runtime function but a compiler function. So when the plugin is compiled it will be calculated and replaced with a hard value while the core code remains flexible.

However you cannot use it to create another variable that will dependant on the second dimension lenght because it is not defined, or more exactly the compiler won't be bothered to actually calculate it for you because it is not the same for all the array entries.

Code:
new szCommands[][] = {     "test1",     "test2!!!!!!!",     "test3" } //...     for ( new i ; i < 25 ; i++ )         server_print("%d: %c", i, ! szCommands[0][i] ? ' ' : szCommands[0][i]);
Code:

0: t
1: e
2: s
3: t
4: 1
5:

6: t
7: e
8: s
9: t
10: 2
11: !
12: !
13: !
14: !
15: !
16: !
17: !
18:

19: t
20: e
21: s
22: t
23: 3
24:

As you can see, it doesn't create 3 entries with 13 cells each because that would be a waste of memory. Instead everything is made to fit perfectly.

Arrays are illusions to increase the syntax readability and making it easier to work with data. It's basically memory in a row with pointers on where to start reading. And since pointers are of a defined lenght they could be kept in an array in exactly the same way but they won't need pointers to be accessed. They will always be kept in the same place (pointer position = cell lenght * index number).

If I would to mess up the data you can see that it will keep on reading the next entry as well since the function using it is searching the memory for NULL to stop reading it. This is how all string functions work in PAWN. All other forms of data have a defined lenght in some form.

Code:
new szCommands[][] = {     "test1",     "test2",     "test3" } //...     szCommands[0][11] = ' ';     for ( new i ; i < sizeof szCommands ; i++ )         server_print("%d: %s", i, szCommands[i]);
Code:

0: test1
1: test2 test3
2: test3


In the end, this is why PAWN is such an easy language to learn and use. EVERYTHING is either 1 cell (32 bits) or an array of multipe cells. All the tags (Float:, bool:, et.c.) doesn't actually change the contents. They are just there to help the compiler point out when memory is used in an incorrect way because the memory is read differently depending on what it contains or how you decide to read it.
Code:
    server_print("%d", true);     server_print("%d", false);     server_print("%d", 5.0);     server_print("%.1f", 1084227584);     server_print("%s", {104,101,108,108,111,0});
Code:

1
0
1084227584
5.0
hello


Your code will also not work because it's missing a dimension.
Either way it's completely irrelevant in this case. There's no need to create several variables for a temporary usage.
Code:
new szTemp[64] = "say "; for( new i ; i < sizeof(szCommands); i++ ) {     copy(szTemp[4], charsmax(szTemp) - 4, szCommands[i]);     register_clcmd(szTemp, "checkSettings"); }

Napoleon_be 11-07-2020 10:04

Re: Array Size
 
Quote:

Originally Posted by HamletEagle (Post 2724081)
This "reason" doesn't make any sense to me. Just test your code, it's not hard and it's so much faster than waiting for someone else to test for you.
Make a little plugin with only the code you are testing and try it.

Well, it's not top priority but i get your point.

@Black Rose; Thanks for the info, do you have any examples where it could be used to be efficient?

Black Rose 11-07-2020 10:54

Re: Array Size
 
Quote:

Originally Posted by Napoleon_be (Post 2724090)
@Black Rose; Thanks for the info, do you have any examples where it could be used to be efficient?

The bottom of my previous post contains what I would've used.

Or if you want to declutter the plugin_init() a bit and also include say_team:
Code:
public plugin_init() {     for( new i ; i < sizeof(szCommands); i++ )         register_clcmd_say(szCommands[i], "checkSettings");     //... } //... stock register_clcmd_say(szCommand[], szFunction[]) {     new szTemp[32] = "say_team ";     copy(szTemp[9], charsmax(szTemp) - 4, szCommand);     register_clcmd(szTemp, szFunction);         szTemp[5] = 's';     //szTemp[6] = 'a'; // This is already the 'a' in the word "team"     szTemp[7] = 'y';         register_clcmd(szTemp[5], szFunction); }

Napoleon_be 11-08-2020 12:22

Re: Array Size
 
Thanks for clearing things up @Black Rose, makes a lot more sense now. This will defenitly help me and others in the future.


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

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