3
lorentz
5y

What's the best way to null-terminate a struct array?

Comments
  • 6
    But this isn't stack overflow...
  • 0
    Yes but

    1. this isn't about how to make it work, but how to make it good. Of course I can put in an is_null field but that might not be the best way

    2. This would probably be tagged opinion based on stackoverflow

    3. I don't want a single correct answer. I want to make sure there aren't ways I haven't thought of.
  • 2
    You could malloc an array of pointers to structs instead of an array of structs, and set the last pointer to null
  • 0
    Talk null into standing at the end of the array. No need for violence you know!
  • 1
    Well you just memset the last array element to 0 of course. Not that this would be really useful.
  • 1
    I'm assuming you mean a linked list of struct type, because arrays dont need to be null terminated as they are finite in statically typed languages, in which case just make the final element in the list null and then check for that.
  • 2
    You can use one of the fields of your struct as the "Terminator"
    If you're crazy and the struct is small you can load it in a 32, 64, 128, 256 or even 512 bits register and quickly test if it's zeroed out
    Or just keep the length of your array stored somewhere
  • 0
    If you must have a null-terminated array:

    struct X xarr[XARRLEN + 1];

    memset(&xarr[XARRLEN], 0, sizeof(struct X));

    If you are using less elements (say i), repeat memset() for the (i + 1)'th element.
  • 0
    @arcsector C has no sure-fire way to determine the length of an array; basically a C array is just a pointer to a location in memory. No safeguards unless the programmer implements them!
  • 0
    @SomeNone didn't know we were talking about C lmao
  • 0
    @SomeNone that's wrong, a C array has the length information via sizeof. The element count is sizeof(my_arr)/sizeof(my_arr[0]). Also, an array is not a pointer, but rather the start address (plus type and length).

    Only when handing over an array to a function, it decays to a pointer. The differences between arrays and pointers are subtle but notable.
  • 0
    @Fast-Nop Especially for smaller structs if I do

    struct mystruct arr[i];

    then

    sizeof(arr)/sizeof(arr[0]) == i

    isn't guaranteed, let alone if I allocate it on the heap.

    I was thinking of memsetting it to zero, but it's also possible that it happens to contain only zero data, which isn't the same.

    Linked lists are slow and the array is big, I need O(1) random index access.

    I'm still unsure whether adding an is_null field or turning it into an array of pointers is the less bad solution, but now I remembered that I can also use a FAM to store the length.

    For the specific problem I ended up using an is_null field since a lot of code was already written and I didn't want to rewrite all of it to use pointers.
  • 0
    > I was thinking of memsetting it to zero, but it's also possible that it happens to contain only zero data, which isn't the same.

    @Lor-inc You asked how to null-terminate a struct array. Null-termination means the last array element is all bits null.

    If that is, however, a possibly valid array entry, then null-termination is not what you are looking for, and you will have to invent your own guaranteed-invalid marker.
  • 0
    @Lor-inc It is guaranteed if you allocate statically.

    And heap allocated objects are never arrays anyway.
  • 1
    I came across this today and I just want to point out that the answer I needed is that you don't generally null-terminate arrays unless the elements are very small and there's an obvious null value, it's much easier and cleaner to store the length somewhere.
Add Comment