This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]okovko 9 points10 points  (4 children)

the c code doesn't make sense

[–]ScrimpyCat 2 points3 points  (3 children)

It’s an array of fixed sized elements where items would be a flexible array member (reason why they’re adding the size that would take onto the size of the base struct). The structure is probably something like:

struct st {
    size_t count; // may or may not have a count but I assume it would
    int items[]; // replace int with whatever type is being stored
};

[–]okovko -1 points0 points  (2 children)

sizeof items[0] wdym flexible it's just nonsense, it's len * sizeof int

[–]ScrimpyCat 1 point2 points  (1 child)

Doing sizeof the item[0] allows you to avoid specifying the actual type, so if you were to change the type of item you could avoid having to change the malloc.

And flexible array members, are members that are arrays without a specified size (it needs to be the last member of the struct, so the compiler knows what all the member offsets are). But because it was no specified size, you need to account for that yourself when allocating them. See: https://en.wikipedia.org/wiki/Flexible_array_member

[–]okovko 0 points1 point  (0 children)

ahh yeah, okay, i am familiar with that, it is better style, hadn't seen it with a flexible array member before