Thread: Array Size
View Single Post
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 11-07-2020 , 09:52   Re: Array Size
Reply With Quote #5

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"); }
__________________

Last edited by Black Rose; 11-07-2020 at 10:53.
Black Rose is offline