| 07-24-2009, 06:14 PM | #1 |
Just like the title says, I'm kind of embarrassed to ask this question ![]() How is it better to attach all* handles to a variable and then null them in the end, than just using a funciton/native? I know that if we do make a variable out of a handle, it's required for recycling the handle's index and the 4 byte the pointer consumes (and of course destroy them first), but why not just use for example GetTriggerUnit() directly? I don't doubt that it is better, just that I'm curious about why... *Yeah yeah, not triggers and players... Another embarassing question; Why does handle-pointers leak, while direct-pointer (integer, real, boolean, string, code etc) doesn't? I've been told that local versions of these direct-pointers are cleaned up in the end of the function, but I fail to see how global variables are cleaned later on. Yes and one more while I'm at it; How much data does one of these direct-pointers consume? |
| 07-24-2009, 06:28 PM | #2 | ||||
Quote:
You don't attach them. You set the variable to point to them. Variables are set, whereas you only attach objects to other objects. (Just terminology.) Quote:
GetTriggerUnit() vs. u - u is far faster to type. Use it a dozen times in a function, and it really adds up. Plus it's a tiny bit faster, probably, but GetTriggerUnit is lightning fast anyway. If I only use GetTriggerUnit() a very small number of times, I'll just use that. Quote:
There is no such thing as a direct pointer. A pointer is something that points to an object. Integer, real, boolean are not pointers. The variables hold the data in 4 bytes directly, but do not have any objects they point ot. Code and string do, but that's handled differently to handles (strings don't have reference counters and are always cached, and code is obviously never recycled but loaded at map init). Anyway, the data of the locals is cleaned up, where that is the pointer or the integer/real/boolean data. Note that the object that the pointer is pointing to is not cleaned up. Due to a bug in WC3's code, when this clean up is done, handles that are pointed to by local variables do not have their reference counters decremented properly, so you have to manually set them to null. Quote:
All variables have 4 bytes for the data or the address (for pointers) and then consume a little bit more for accessing the variable. |
| 07-24-2009, 06:57 PM | #3 |
In conclusion: 1: We set a variable to a handle because it's easier to type and and helps you organize your code (it's also a tiny bit faster, but not much) 2: In difference to handle-pointers, integers reals etc contains data and arn't just pointers to a memmory index (or memmory stock or whatever...) 3: All variables consume 4 bytes until nulled or automaticall trashed (???) N.o 3 led me to another question: Are integers, reals etc really removed from memmory when not used anymore, or do they keep consuming 4 bytes of data? |
| 07-24-2009, 09:15 PM | #4 |
to no. 3: whenever a integer/real etc 'leaves scope', that means e.g. a local variable, and the function ends, they are removed, and consume no more space. of course, global integers/reals etc. consume their space until the end of the game. but thats more or less intentional, and you can ignore that |
| 07-24-2009, 10:27 PM | #5 | |
Quote:
And another two questions were spawned from this... 1: Is there a way to null integers, reals, booleans, strings and code? 2: What's the 'common name' for these variable types. I know that for example unit variables are called handle-pointers, but not these 'direct' variables I'd like to give you rep for the help, but I can't (have to spread first) |
| 07-25-2009, 01:37 AM | #6 | |
Quote:
1. Because the former three are not pointers, the terminology "null" doesn't even apply. However, the latter two are pointers. I haven't tested with code, but strings can be nulled. As they are immutable and not ref-counted, nulling will have no effect though. 2. They could be called scalar types or value types. Strictly speaking, string and code are pointer types or reference types in terms of definition. |
| 07-25-2009, 01:38 AM | #7 |
You can null strings by setting them ="", but the others you cannot do anything about, nor do you need to. They're collectively called "Primitives." |
| 07-25-2009, 02:21 AM | #8 | |||
Quote:
'All' variables' means all variables (at least after initialisation; arrays are a bit odd and init in blocks of powers of two, and no one has ever tested non-arrays because there's no damned point). A nulled variable just has a data value of 0. Only local variables are dynamically destroyed (out of variables), and they are always destroyed at the end of the function, regardless of what you do. Quote:
This question makes no sense. They are not used....they are not objects. The variable has the integer/real/boolean value from the bits of the 4 bytes, which are dealt with as above. Quote:
Integers/reals cannot 'leave scope'. See above - they are not objects. Integer/etc variables can, and only in the case of local variables. |
| 07-25-2009, 01:57 PM | #9 |
Thanks guys, this really helped me. No further questions have been spawned from your answers. Reputation to you all |
