Re: [Vala] Array of structs OR Array of classes



On Sat, Nov 07, 2009 at 11:34:22 +0100, JM wrote:
I heard that structs are stack-allocated and classes are
heap-allocated. 

You heard right. And that applies to arrays as well. Array of structs
contains those structs directly, array of classes contains pointers.

Would there be an advantage/disadvantage for me to use classes instead
of structs? I need no additional features of these classes. 

Depends on the size of the structures and number of reallocations (due to
extending the array). If the structure is small (yours seems to be), the copy
will be faster than the allocation, so structures will be better. If they get
bigger, the copy time will start to grow and classes start to be better.

The exact definition of "bigger" will depend on the relative frequency of
element insertions/deletions vs. access to them and also on the compiler and
platform, but given that allocating memory is fairly complicated business,
I expect structs to be faster until tens of fields at least.

The array also uses up to twice the memory you need, because it's allocated
in powers of two. Using classes has it's own overhead, but not as big, so
memory-wise, classes will be more efficient over some 4 or 5 (word-sized)
members.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]