• 💀 Happy Halloween! 💀 It's time to vote for the best terrain! Check out the entries to Hive's HD Terrain Contest #2 - Vampire Folklore.❗️Poll closes on November 14, 2023. 🔗Click here to cast your vote!
  • 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 HD Level Design Contest #1 is OPEN! Contestants must create a maze with at least one entry point, and at least one exit point. The map should be made in HD mode, and should not be openable in SD. Only custom models from Hive's HD model and texture sections are allowed. The only exceptions are DNC models and omnilights. This is mainly a visual and design oriented contest, not technical. The UI and video walkthrough rules are there to give everyone an equal shot at victory by standardizing how viewers see the terrain. 🔗Click here to enter!

[vJASS] Variable in struct

Not open for further replies.
Level 15
Nov 30, 2007
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:
method init_bTypeCount takes nothing returns nothing
            local integer i = 1
                exitwhen i == MAX_BUILDING_TYPES
                set .bTypeCount[i] = 0
                set i = i + 1

to use this method:

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

Dr Super Good

Spell Reviewer
Level 63
Jan 18, 2005
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
Apr 16, 2012
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)
Not open for further replies.