2

Recently I found it was annoyed to deal with array in c language.
I have to realloc() frequently to increase the size.
And there is no standard data structure like vector in C++ or Arraylist in java

I have got to known that in linux kernel, there is some data structure, such as
kfifo, we could use this by kfifo_in(), kfifo_out() function.

But this means the user would define kfifo *pointer; to record the array, and this variable does not contain any info about the type contained in the structure.
The user have to remember that when he try to use the dynamic array by kfifo pointer.
I think it may be a little confusing.
Is there any better way to deal with the problem? What's the common solution in linux c programing?

3 Answers 3

3

realloc is not that bad, as long as you do not spread it all over your code, and use a reasonable strategy to grow your dynamic array.

Rolling your own dynamic arrays in C is a matter of implementing a handful of easy functions. Numerous short articles walk you through this exercise - here is one for an example. The article defines a struct that represents your dynamic array, along with the currently used and the allocated size. It also provides functions for initializing, growing, and de-allocating the array represented by the structure. There is no explicit initialization function in the library - you initialize by passing NULL as the first parameter. This is a valid approach, but you could also opt for a more traditional separation of init and grow.

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

4 Comments

Thanks for your suggestion. I plan to do this, just feel a little weird: user have to remember the element type when they use this dynamic array. By the way, the example you mentioned seems to use void * pointer to store the element. I think it is a little dangers. what if the void * object is out of reach? Maybe copying the memory is safer.
@bluesea They use void* rather than a specific type to make their code reusable for any type the user may want. That is an unfortunate consequence of C lacking means for generic programming, such as C++ templates.
I mean they could define struct array{int len; int capacity; int each_element_size; void *data;} and copy the bytes of element, put at the end of the data.
@bluesea Oh, I see. That would be interesting! malloc already puts some info in the bytes before the address that it returns; your code could pull the same exact trick!
2

I'd use Glib arrays. It's a very well known library in Linux and other OSes, used in projects like Gnome.

There is no standard for dynamic arrays in C.

1 Comment

Oh, Glib seems to be useful if many projects, even gnome, use it. Thanks!
0

@bluesea

I mean they could define struct array{int len; int capacity; int each_element_size; void *data;} and copy the bytes of element, put at the end of the data. – bluesea Jun 29 at 3:04

This is already taken care of in the library under discussion. See the macro's that it comes with and the examples in the main.c file. Depending on the macros's being used, you would either end up with an array of pointers to the original data, or an array of pointers to a copy of the data.

FWIW, I'm the author of the library, and I'll be the first to admit that it comes without airbags, so you have to be sure to use it safely (as with anything else in C).

Comments

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.