Raised This Month: $ Target: $400
 0% 

When to use global variables?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drak
Veteran Member
Join Date: Jul 2005
Old 11-07-2006 , 19:16   When to use global variables?
Reply With Quote #1

Doesn't global variables take up a lot more memory then local ones to?
For example:
Code:
#include <amxmodx> #include <amxmisc> #define MAX_LIMIT 100 new gMax_limit  = MAX_LIMIT public plugin_init() {      register_plugin("Somthing","Something","Sure") } public a_function(id) {      client_print(id,print_chat,"Max limit is: %i", gMax_limit) }

Would it be better to register the variable in the function 'a_function' ?
Drak is offline
Send a message via MSN to Drak
SweatyBanana
BANNED
Join Date: Sep 2005
Location: LOL
Old 11-07-2006 , 19:20   Re: When to use global variables?
Reply With Quote #2

Global variables are stored in memory throughout the existance of the plugin. (meaning until plugin_end I think, could be till the comp is turned off), so a global variable is better for using something many times because you are calling the same spot in memory over and over instead of creating new spots in memory every time you create a new variable.

So yes, if you are only using the variable once, make it within your function.
SweatyBanana is offline
Send a message via AIM to SweatyBanana Send a message via Yahoo to SweatyBanana
The Specialist
BANNED
Join Date: Nov 2006
Old 11-07-2006 , 19:29   Re: When to use global variables?
Reply With Quote #3

yes local veriables = only using once

global variables = more then once

stock varaibles = can be passed by compiler if not used
The Specialist is offline
Send a message via AIM to The Specialist
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 11-07-2006 , 20:00   Re: When to use global variables?
Reply With Quote #4

Quote:
Originally Posted by SixTwin View Post
Doesn't global variables take up a lot more memory then local ones to?
For example:
Code:
#include <amxmodx> #include <amxmisc> #define MAX_LIMIT 100 new gMax_limit = MAX_LIMIT

public plugin_init() { register_plugin("Somthing","Something","Sure") } public a_function(id) { client_print(id,print_chat,"Max limit is: %i", gMax_limit) }



Would it be better to register the variable in the function 'a_function' ?
Totally wrong, they take up the same amount of memory.

Global variables are much faster though, as they're not deleted/created/filled each time they're needed.

Like SweatyBanana said, however, the storage is retained throughout the plugin's execution (clearing it impossible, even setting it to 0 leaves it as unusable to other applications).
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
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:52.


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