2

I have a array of somestruct_t I'm adding to but don't know in advance how many elements it will need. So I malloc 5 slots of space to start with but I need to encapsulate it in another struct that saves the array boundary and whats used so far so I don't overflow. I'll realloc if it looks like it will overflow.

typedef struct {
  somestruct_t[] *data;
  uint32_t max_size;
  uint32_t used;
} something_box_t;

Is this the idiomatic way to do this?

5
  • Use std::vector, assuming you didn't tag the question with both C and C++, but only wanted C. Commented Oct 11, 2012 at 19:59
  • 2
    Are you using C or C++? Choose one as the solution will be different. Commented Oct 11, 2012 at 20:01
  • In C, i didn't think it'd be different for C++. Commented Oct 11, 2012 at 20:07
  • 2
    @BreezyChick89: Very different in C++. While any C solution will likely be legal and valid in C++, there are much better ways of doing this in C++ that you can't do in C. Commented Oct 11, 2012 at 20:08
  • 1
    Do you have a practical maximum number of somestruct_t objects that will be in a something_box_t? How large are these somestruct_t objects? Commented Oct 11, 2012 at 20:11

2 Answers 2

2

One thing that's done in C sometimes is to use a zero length array at the end of a struct and to allocate extra space for a variable number of elements. I'm not sure if zero length arrays are legal in C (they're not in C++) but it's commonly supported.

struct something_box_t {
  uint32_t max_size;
  uint32_t used;
  somestruct_t data[];
};

something_box_t *box = malloc(sizeof(something_box_t) + N*sizeof(somestruct_t));
box.max_size = N;
box.used = 0;

box.data[0] = ...;
box.data[1] = ...;

Of course, if you can switch to C++ you won't have to hack together a solution on your own. You could just do std::vector<somestruct_t> box; and have a solution far easier to use, as performant if not more performant, and more reliable than what you're likely to write in C.

Sign up to request clarification or add additional context in comments.

1 Comment

This question has been re-tagged [C] only.
2

You can see this often done by linked list (in C, for example in kernel mode windows drivers), here's an example how to implement your own.

C++ offers you templates to make lists more flexible.

And C++ also offers you stl containers (like std::vector) but bames53 already suggested that one.

5 Comments

A linked list is NOT the common practice. It's much more common to use dynamic arrays (such as std::vector). You only want linked lists when there will be many insertions/deletions in the middle of the list.
@EmileCormier I've spent a lot of time in Windows kernel drivers recently and the only thing you saw there was a linked list. Do you have any reference for good dynamic array implementation for C?
I was assuming C++ when I wrote my comment. I can see how linked lists would be easier to implement in C.
@EmileCormier I've added explict (in C), I meant linked lists only for C implementation. Anyway thank you for pointing out that my answer can be misunderstand.
Please revise your answer to say "a common practice," this is a perfectly valid way depending on application requirements but by no means is the most standard solution and the OP should not be led to think as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.