• 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.

Default values of struct arrays

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

I am a bit confused by the behavior of struct arrays.

I have long believed that since they are integer arrays their default values are always 0, which also means that there's no struct in that position.

But this behavior I've observed suggests otherwise, which brings about a very strange contradiction, because I've been doing array struct[0] == 0 as a test if a position is empty (and it's never failed, until now that is).

I have a struct which has an array of structs inside it, and this part of the code loops over them only if their are actually structs inside them.

JASS:
        loop
            exitwhen i == MAX_QUADS
                if subQuads[i] != 0 then
                    call subQuads[i].flush()
                    call subQuads[i].destroy()
                endif
            set i = i + 1
        endloop

But, for whatever reason, the if statement actually fails. Even if subQuads never had a struct put there, it still runs the next calls. How do I know this?

My Quad structs require a JASS rect to be created--there's no other way to create one. So they have to be created like this: Quad q = Quad.create(someRect).

Now, when I did this check
JASS:
if subQuads[i].rect == null then
  call print("got a null rect when we shouldn't have"
endif

it turns out on every faulty struct, the rect was also null. Meaning it was never ever created or initialized. Then how on earth could subQuads returns a value not equal to 0 if I never put a struct there?!

So now I have this for my loop, which seems to work.

JASS:
        loop
            exitwhen i == MAX_QUADS
                if subQuads[i].r != null then
                    call subQuads[i].flush()
                    call subQuads[i].destroy()
                endif
            set i = i + 1
        endloop

But I've used checking if a struct is equal to 0 in all my other code, and it's never failed me once. So this is a very (annoying) contradiction.
 
I can tell you why this is the case.

This won't occur the first time through. However, the second time through, the struct has members containing values from the previous time. Do you ever 0 out these values?

And Adiktuz, please focus on answering the questions. Try not to put the OP in a defensive position by questioning why they are doing something. Just tell them what's wrong ;). Don't dodge the question with another question. If you don't know the answer, just don't reply ;). I can't begin to express how frustrating it is to get replies like yours. I used to get them all the time a long time ago. People would never answer my questions, they'd just attack why I was doing what I was doing, as if I was some insane idiot that didn't know anything. It is very counterproductive. If you don't see the use in something, unless you have a better alternative, that's your problem, not the OP's.

Your answer also doesn't have anything in it, so why did you reply in the first place? If you have no idea, then just don't reply ^)^. All you do is cause confusion when you answer with nothing but frivolity.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
I can tell you why this is the case.

This won't occur the first time through. However, the second time through, the struct has members containing values from the previous time. Do you ever 0 out these values?

Yes that explains it. The first time my calculations always worked. Then every other time after they reported false results.

When do I need to zero out the values? When I create my struct?

And why is this the case? It's been driving me off the wall.
 
When do I need to zero out the values? When I create my struct?

When you destroy the struct or destroy the member structs. I'd opt for the latter in your case.

And why is this the case? It's been driving me off the wall.

As I continue to say, look at the code generated by jasshelper. There are initializers, but there is nothing in the manual about automatically nulling members.
 
Structs are just arrays of integers, and they are globals... so they keep any data that they previously have...

then what about initializers?

JASS:
struct o
endstruct

struct b
    o test = o.create()
endstruct

You need to consider that many languages have both auto initialization and auto destruction. Unless you are intimately familiar with the code output of jasshelper, some of the behavior can be confusing.

If he was just playing with an integer array, then sure. However, seth here is playing with magical hidden code. He can't see the code, so he doesn't know what it's doing. This is why I keep telling him to look at the code jasshelper generates -.-.
 
Status
Not open for further replies.
Top