| 01-20-2009, 03:35 AM | #1 |
Can any of you please test this script and tell me if the result was same for you? Thanks. null comparison script:// This one does not work function Test_a takes nothing returns nothing local integer i if (i==null) then call BJDebugMsg("I is null") endif endfunction // This one works function Test_b takes nothing returns nothing local integer array i if (i[0]==null) then call BJDebugMsg("I is null") endif endfunction Integer comparisons to null only works for me if the integer variable is an array, otherwise the execution stops. |
| 01-20-2009, 03:42 AM | #2 |
locals are not defined, therefore a thread crush. |
| 01-20-2009, 03:51 AM | #3 |
But the local integer array works. Also, global uninitialized integers don't also work: global variable null comparison:scope nullTest initializer InitTest globals integer i endglobals private function DoTest takes nothing returns nothing if (i==null) then call BJDebugMsg("I is null") endif endfunction private function InitTest takes nothing returns nothing call TimerStart(CreateTimer(), 1, false, function DoTest) endfunction endscope The thread also stops when global variable i is compared. |
| 01-20-2009, 03:59 AM | #4 |
Arrays are preallocated in powers of 2 by the system upon declaration, non-arrays are not. |
| 01-20-2009, 04:09 AM | #5 |
so i[0]-i[2] is already allocated initially or i[2], i[4], i[8]? Could you please explain "in powers of 2", I don't understand it really clearly, and thanks Dusk. |
| 01-20-2009, 06:17 AM | #6 | |
Ya, sorry, I mean all single variables are uninitialized while arrays are. I would assume this is cause you can set the single vars to a value during declaration yet there is no syntax in Jass to do so in arrays. Therefore, Jass allocates the nulls/0s for arrays so you don't have to manually. Quote:
Not sure what Dusk meant by this, but Array[n] for 0 < n < 8192 are initialized to null/0. |
| 01-20-2009, 08:13 AM | #7 |
Empty vars return out of memory error. no value == no memory for var. |
| 01-20-2009, 10:50 AM | #8 | |
Quote:
a valid script that returns null:scope nullTest initializer InitTest private function DoTest takes nothing returns nothing local integer array i if (i[-1]==null) then call BJDebugMsg("i is null") endif endfunction private function InitTest takes nothing returns nothing call TimerStart(CreateTimer(), 1, false, function DoTest) endfunction endscope @ Diod: Is it like an error on c++ pointers? when you try to use an uninitialized pointer? Anyway, this is better than what I needed, I could use a negative index to set my variables to null. Also, are jass arrays structs? Thanks for all the information guys. |
| 01-20-2009, 12:01 PM | #9 |
Doing anything with an uninitialized variable causes a thread crash. Uninitialized arrays, however, have some safety built in so that an uninitialized index is treated as a null value instead of crashing everything. |
| 01-20-2009, 01:07 PM | #10 | |
Quote:
|
| 01-20-2009, 01:18 PM | #11 |
Mmmm, is there a change that "The safety mechanism" sets the array variables with some garbage value that would allow to make the comparison? Just wondering. |
| 01-20-2009, 01:20 PM | #12 |
It preallocates them to a 'null' in most cases. It's sufficient that a comparison to null will return true. |
| 01-20-2009, 01:53 PM | #13 |
Okay, Thanks for all the information, what I really needed was a null value for integers. :) |
| 01-20-2009, 11:51 PM | #14 |
chobibo Any array operation with "out of range index" cause memory error (cannot be read) if you attempt to load saved game. Set anything to -1 array index, save and load. note: this is not tested, but setting anythig to 8191 cause this problem. vjass structs have some security checks, if you follow manual, no errors will pop up. |
| 01-21-2009, 01:24 AM | #15 |
Don't worry I won't be using an invalid array index, I just told Ammorth that even arrays with negative indexes return null. |
