Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
You could malloc an array of pointers to structs instead of an array of structs, and set the last pointer to null
-
Well you just memset the last array element to 0 of course. Not that this would be really useful.
-
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.
-
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 -
SomeNone7135yIf 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. -
SomeNone7135y@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!
-
@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. -
@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. -
SomeNone7135y> 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. -
@Lor-inc It is guaranteed if you allocate statically.
And heap allocated objects are never arrays anyway. -
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.
Related Rants
What's the best way to null-terminate a struct array?
question
struct array
c
null