Raised This Month: $ Target: $400
 0% 

Creating and Deleting Variables at run time?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
pRED*
Join Date: Dec 2006
Old 12-20-2006 , 19:45   Creating and Deleting Variables at run time?
Reply With Quote #1

Hey

Is there any way to create and delete variable at run time? Similar to malloc in C/C++

The plugin I am working on makes two different query_client_cvar calls and then prints the results to console on the same line.
As far as I can tell this means that I have to store the results from the first call in a global variable until the second one has been done and then print both results at the same time.

I would prefer to find a way to avoid using global variables since they will only be used very rarely and will just waste space the rest of the time.

Thoughts?
pRED* is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 12-20-2006 , 22:48   Re: Creating and Deleting Variables at run time?
Reply With Quote #2

Global Variables take up about 4 bytes more of memory then local variables. The only reason to use global over local is because global variables are 0'd on load of the plugin , while local's have to be 0'd during running. (correct me someone if im wrong )

ADDED : which meens that using global variables are less CPU exspensive.

Last edited by The Specialist; 12-20-2006 at 23:03.
The Specialist is offline
Send a message via AIM to The Specialist
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 12-20-2006 , 23:50   Re: Creating and Deleting Variables at run time?
Reply With Quote #3

pRED* | NZ: you can't remove a global variable from memory once it has been allocated. There's nothing horrible about global variables, they're much more hospitable than the plague even, get used to them.

The Specialist: from experience, I would say that every variable, when created, if not assigned a value, is assumed a value of 0 (or false). You don't need to assign it anything specifically, even if it's local.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
pRED*
Join Date: Dec 2006
Old 12-21-2006 , 02:12   Re: Creating and Deleting Variables at run time?
Reply With Quote #4

I just thought having about 6 global variables that are only used rarely, permanently taking up memory space was a waste.

But if it's not really an issue i'll just leave it as is. Works fine as it is.

Thanks
pRED* is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 12-21-2006 , 02:13   Re: Creating and Deleting Variables at run time?
Reply With Quote #5

First of all , HI .

2nd , Thats not what i was saying

http://wiki.amxmodx.org/index.php/Optimizing_Plugins

I was correct , global variables are 0'd on loading , and local variables are 0'd dynamical . read the global variable VS local variable part.
The Specialist is offline
Send a message via AIM to The Specialist
pRED*
Join Date: Dec 2006
Old 12-21-2006 , 02:23   Re: Creating and Deleting Variables at run time?
Reply With Quote #6

Quote:
Originally Posted by The Specialist View Post
First of all , HI .
Hi!

Quote:
Originally Posted by The Specialist View Post
global variables are 0'd on loading , and local variables are 0'd dynamical
Yea that's the exact reason I wanted to use locals. Since they are dynamic, the memory is released back to amxx/server once the function has finished exectuing.
Globals permanently occupy memory space as long as the plugin is running (so 24/7 for most servers) and may only be in use for about 30 seconds total per day..
pRED* is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 12-21-2006 , 02:26   Re: Creating and Deleting Variables at run time?
Reply With Quote #7

but your missing the point , read the link i just posted here is a qoute from it . Dyamicly is a bad thing for varaibles , its slow an tedious (quote)

Code:
 
It is important to realize that every variable 
in Pawn is automatically zeroed. For global 
variables, they are static and permanent, 
thus they are zeroed when your plugin is loaded. 
Variables in functions, however, must be zeroed 
dynamically. This is a slow and tedious operation, 
and you should not only avoid relying on it when necessary,
 but you should keep that fact in mind when using arrays.
POINT : Dont use local variables unless you have to , to avoid assignemnt loss etc etc etcc .

Last edited by The Specialist; 12-21-2006 at 02:29.
The Specialist is offline
Send a message via AIM to The Specialist
pRED*
Join Date: Dec 2006
Old 12-21-2006 , 02:34   Re: Creating and Deleting Variables at run time?
Reply With Quote #8

Yes I understand that.

Local Variables = More cpu time when the function is run due to allocating memory, 0'ing it and then releasing it after the function is completed.

Global Variables = More memory permanently taken up when it isn't needed.

I was weighing up the two options and thought that locals would be preferable since the plugin would probably be called once an hour or most likely less.
But don't worry since it isn't possible since query_client_cvar always returns the cvar value to a separate function.

Thanks for the help though.
pRED* 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 22:19.


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