• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Variable in struct

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
if my structs contains a variable with an array, does every element have to be set to 0 for me to be able to increase it?

Is this required:
JASS:
method init_bTypeCount takes nothing returns nothing
            local integer i = 1
            loop
                exitwhen i == MAX_BUILDING_TYPES
                set .bTypeCount[i] = 0
                set i = i + 1
            endloop
        endmethod

to use this method:

JASS:
method inc_bTypeCount takes integer index returns nothing 
            set .bTypeCount[index] = .bTypeCount[index] + 1
        endmethod
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
all arrays are default-initialized in Jass, so no, they will contain 0 even without your init
Except if he ever de-allocates struct instances which are then recycled. In that case they will contain what ever garbage value they had when they were de-allocated.

For this reason ever time you use a new struct instance you should initialize all member variables to some value. This also applies to C++ and C since memory allocation may return non-zeroed memory.

Do note that struct arrays divide maximum instance account. For example if you have structs with 1024 sized array members then you can have only 8 instances without occurring the penalties needed for chaining arrays.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
well that is correct, but you should write default values into your member variables either in destroy or create methods regardless, so I just jumped into that assumption.

C has calloc which will return you zero-initialized values.

Also C and C++ allocation is a lot different thing than Jass allocation, since in Jass you never physically allocate anything(excluding dynamicness of arrays, since that is not really your intention)
 
Status
Not open for further replies.
Top