Raised This Month: $ Target: $400
 0% 

Is it ok to declare static variable inside a loop?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
11922911
Senior Member
Join Date: Dec 2011
Location: Yuen Long Country
Old 05-07-2016 , 23:49   Is it ok to declare static variable inside a loop?
Reply With Quote #1

For example:
PHP Code:
new maxClients get_maxplayers()
for (new 
1<= maxClientsi++)
{
    if (!
is_user_connected(i))
        continue;
    
    static 
name[32];
    
get_user_name(inamecharsmax(name));
    
server_print("#%d ^"%s^""iname);

Since the memory isn't created every time it declared, so it will be the same performance as if you create it outside a loop?

And:
PHP Code:
for (...) {
    new 
12;
}

for (...) {
    static 
a12;

They are the same?
__________________
youtube:
@holla16

Last edited by 11922911; 05-08-2016 at 00:08.
11922911 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-08-2016 , 03:39   Re: Is it ok to declare static variable inside a loop?
Reply With Quote #2

There's no reason to do it inside the loop anyways. Just declare the variable before the loop.
__________________
fysiks is online now
11922911
Senior Member
Join Date: Dec 2011
Location: Yuen Long Country
Old 05-08-2016 , 04:43   Re: Is it ok to declare static variable inside a loop?
Reply With Quote #3

for scope
__________________
youtube:
@holla16
11922911 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 05-08-2016 , 04:55   Re: Is it ok to declare static variable inside a loop?
Reply With Quote #4

You can either create your own scope:

Code:
func() {     {         new a;         for(...) {}     } }

or declaring the variable in the for().

Code:
func() {     for(new i, a; i < 42; ++i) {} }

Note with 1.8.3-dev, to get name, you can use directly %n with player's index as specifier in the format
__________________

Last edited by Arkshine; 05-08-2016 at 04:58.
Arkshine is offline
11922911
Senior Member
Join Date: Dec 2011
Location: Yuen Long Country
Old 05-08-2016 , 09:17   Re: Is it ok to declare static variable inside a loop?
Reply With Quote #5

Quote:
Originally Posted by Arkshine View Post
You can either create your own scope:

Code:
func() {     {         new a;         for(...) {}     } }

or declaring the variable in the for().

Code:
func() {     for(new i, a; i < 42; ++i) {} }

Note with 1.8.3-dev, to get name, you can use directly %n with player's index as specifier in the format
That's good to know. Thanks.
__________________
youtube:
@holla16

Last edited by 11922911; 05-08-2016 at 09:19.
11922911 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 14:17.


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