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

Array Size


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 11-06-2020 , 17:44   Array Size
Reply With Quote #1

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.
__________________

Last edited by Napoleon_be; 11-06-2020 at 17:45.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 11-07-2020 , 03:26   Re: Array Size
Reply With Quote #2

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.
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 11-07-2020 , 08:28   Re: Array Size
Reply With Quote #3

Quote:
Originally Posted by Shadows Adi View Post
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");
    } 
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-07-2020 , 09:10   Re: Array Size
Reply With Quote #4

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.
__________________
HamletEagle is online now
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
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 11-07-2020 , 10:04   Re: Array Size
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
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?
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 11-07-2020 , 10:54   Re: Array Size
Reply With Quote #7

Quote:
Originally Posted by Napoleon_be View Post
@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); }
__________________

Last edited by Black Rose; 11-08-2020 at 05:57.
Black Rose is offline
Old 11-07-2020, 15:39
fysiks
This message has been deleted by fysiks. Reason: Clearly I misread something
Old 11-08-2020, 05:45
Black Rose
This message has been deleted by Black Rose. Reason: Misunderstanding
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 11-08-2020 , 12:22   Re: Array Size
Reply With Quote #8

Thanks for clearing things up @Black Rose, makes a lot more sense now. This will defenitly help me and others in the future.
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
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 06:59.


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