HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

A null integer value

01-20-2009, 03:35 AM#1
chobibo
Can any of you please test this script and tell me if the result was same for you? Thanks.

Collapse 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
Ammorth
locals are not defined, therefore a thread crush.
01-20-2009, 03:51 AM#3
chobibo
But the local integer array works. Also, global uninitialized integers don't also work:

Collapse 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
Rising_Dusk
Arrays are preallocated in powers of 2 by the system upon declaration, non-arrays are not.
01-20-2009, 04:09 AM#5
chobibo
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
Ammorth
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:
Originally Posted by Rising_Dusk
Arrays are preallocated in powers of 2 by the system upon declaration, non-arrays are not.

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
DioD
Empty vars return out of memory error.

no value == no memory for var.
01-20-2009, 10:50 AM#8
chobibo
Quote:
Originally Posted by Ammorth
Not sure what Dusk meant by this, but Array[n] for 0 < n < 8192 are initialized to null/0.

Collapse 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
Anitarf
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
Rising_Dusk
Quote:
Originally Posted by Anitarf
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.
That. I had Griffen explain it to me a few hours before you made this topic, so it was fresh in my memory. The indexes are preallocated in powers of two. So if you use 50 of the first indexes, the system will preallocate 64 with the safety Anitarf describes. If you use 100, it preallocates 128 and so forth. This is why it's bad to use high indexes for no good reason (say, 2000), because it will preallocate 4096 in memory.
01-20-2009, 01:18 PM#11
Flame_Phoenix
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
Rising_Dusk
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
chobibo
Okay, Thanks for all the information, what I really needed was a null value for integers. :)
01-20-2009, 11:51 PM#14
DioD
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
chobibo
Don't worry I won't be using an invalid array index, I just told Ammorth that even arrays with negative indexes return null.