[vJASS] Struct Allocation Question

Level 17
Joined
Feb 11, 2011
Messages
1,860
Hello guys,

I would like to know if it is possible to allocate a specific instance of a struct?
I'm not really sure how to explain this well; but I will try. Can I allocate, for example, struct instance 5 even though 1, 2, 3 and 4 are not allocated?

I thought of something like this:

JASS:
local thistype this = 5
// do stuff...

But I am afraid that it will not be allocated correctly.
Any solutions?

Thanks,

Mr_Bean
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Ofc it won't be, if you really need it use your own custom allocator.

Now you can use a struct as just a bunch of arrays, for example :

JASS:
struct S extends array

    static method operator [] takes integer which_instance returns thistype
        return thistype(which_instance)
    endmethod

    // struct members
    integer int1
    integer int2
    real r1
    real r2 
    ...
endstruct

...

local thistype this = S[5]
set this.int1 = 42

or if you don't use the method operator :

JASS:
local thistype this = S(5)

Struct which extends array have no allocators, but you can use your own, note that you can't use array members with struct extends array.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Hmm, wait i've never used such structs, but i suppose the method operator is not needed, for the syntax [], that would be just redundant.

And no i'm not saying it doesn't need an allocator, i said there is no allocator at all with such structs, no implicit allocate, create, destroy, deallocate methods.
So a struct which extends array is just a collection of methods/members.

Speedfreak people use struct extends array, because this way they could use their "more efficient" custom allocator, or even inline it.

Also since they control the allocators, the generated jass code can be smaller.

The default struct allocator is quite simple in fact, just check the outputwar3map.j
 
Level 31
Joined
Jul 10, 2007
Messages
6,307
The stack allocator is automatically generated for structs when you don't extend array. When you do extend array, no allocator is generated, you have to write them from scratch.

If you want to use a heap allocator, you will have to write it from scratch sadly.

There is a Binary Heap resource in the JASS section, but there is more to a heap allocator than just using a binary heap : ). You also have to deal with defragging the heap.
 
Level 5
Joined
Aug 27, 2010
Messages
79
'this' and any other integers used to reference struct objects are actually indexes in the arrays used for the members of struct right?
so u should be able to access it directly. the only problem will be that object '5' might already be used by another object. so if u dont extend array u can still access it, but will be buggy.
i guess u use in cases like, your struct extends array, you have unit indexer, u can use the index of unit directly
local thistype this = GetUnitUserData(u)
(i learned this trick yesterday!! i was using a hash with GetUnitHandle(u) as key till then)

please correct me if i am wrong, i havent done any testing this is all speculation.
 
Last edited:
Level 17
Joined
Feb 11, 2011
Messages
1,860
The stack allocator is automatically generated for structs when you don't extend array. When you do extend array, no allocator is generated, you have to write them from scratch.

If you want to use a heap allocator, you will have to write it from scratch sadly.

There is a Binary Heap resource in the JASS section, but there is more to a heap allocator than just using a binary heap : ). You also have to deal with defragging the heap.

Thanks man. Sounds a bit complicated; I think I will just stick to the normal allocator.

'this' and any other integers used to reference struct objects are actually indexes in the arrays used for the members of struct right?
so u should be able to access it directly. the only problem will be that object '5' might already be used by another object. so if u dont extend array u can still access it, but will be buggy.
i guess u use in cases like, your struct extends array, you have unit indexer, u can use the index of unit directly
local thistype this = GetUnitUserData(u)
(i learned this trick yesterday!! i was using a hash with GetUnitHandle(u) as key till then)

please correct me if i am wrong, i havent done any testing this is all speculation.

Thanks.

Can someone perhaps give me an example of a custom allocator for a struct that extends array?
 
Top