Re: [Vala] Array as big as an enum



On Tue, 2016-08-30 at 18:14 +0000, Gergely Polonkai wrote:
That is actually the best approach if you have holes in your enum
(which
may bring in other problems)


If you have holes in your enum this is definitely not appropriate for
what rastersoft wants.  Your array will not include entries for the
holes, so accessing blah[Foo.BAR] will likely result in accessing out-
of-bounds memory (assuming BAR comes after a hole).

If you don't have holes (or they are small and rare), then allocating
n_values allows you to use the enum values as array indices to access
the relevant value.

Note that, if there is no 0 value in your enum, you'll want to either
allocate n_values + 1 entries or subtract one whenever you want to
access the relevant entry.


On Tue, Aug 30, 2016, 18:11 Evan Nemerson <evan coeus-group com>
wrote:


On Mon, 2016-08-29 at 23:47 +0200, rastersoft wrote:

Sorry, I found how to do that:

int[] blah = new int[LAST_ELEMENT];

If you want to avoid having a LAST_ELEMENT value, something like
this
will also work:

    ((GLib.EnumClass) typeof(Foo).class_ref ()).n_values

I'm not necessarily advocating it, but if you really want to keep
your
API clean it's an option.



El 29/08/16 a las 23:45, rastersoft escribió:


Hi all:

I have an enum with several elements, and I need to create an
array
with
the same number of elements. In C I define a last element with
a
known
name (eg: "LAST_ELEMENT"), and use "int blah[LAST_ELEMENT];" to
create
the array. How can I do that in Vala?

Thanks.

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list

Attachment: signature.asc
Description: This is a digitally signed message part



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