What is a faithful alternative to a linked list in C?

若如初见. 提交于 2019-12-09 19:00:55

问题


This question might be too broad, or opinion-biased, but I know this site to be full of experienced programmers, and I think it might encourage a good discussion.

I am implementing an embedded application in C, in which I use a linked list, containing structures:

struct my
{
    uint16_t x;
    uint16_t y;

    char *text;

    struct my *next;
    struct my *prev;

};

It worked fine in general, however in the project right now I'm shifting towards the MISRA-C programming guidelines. MISRA precludes the use of any dynamic data structures, as it might cause unspecified behavior in an embedded system with limited memory.

What first came to my mind, is of course, a classical static array of structures, with a fixed size. There can never be more than 30 instances of this struct, and we are still only using less than 5% of our available memory, so even if not all of this memory is used, it will not affect our program performance. Like this:

extern struct my arr[30];

There are, however, certain difficulties in this approach, for example, sometimes I need to remove an element from the list, and then it would leave an empty element, forcing me to rewrite all further elements by one index.

Is there any clean, elegant way, to achieve a functionality similar to a linked list, without using them?


回答1:


You can use a linked list over a static array. The only difference is that instead of pointer to a dynamically allocated chunck of memory, you use a pointer to an elementet inside the array. If you prefer, you can use just the array index instead of a pointer. Of course, the implementation would have some differences: - You don't have to (un)alloc memory when adding or removing elements - Instead, you'll have to manage free slots by yourself, using another structure, like a queue or even another list of empty elements




回答2:


You can use array index in place of pointers. You can then build linked lists and use the index value to access the next or previous widget.




回答3:


You could use another bool array to mark which entries are used.

There are plenty of existing APIs that provide this functionality of reserving memory at compile time for the maximum number of entities, and the provide functions to allocate and free them. See Contiki memb API and implementation for an example.




回答4:


There are plenty of ways to achieve that but here's one : you can create an array of struct widget + a array of booleans to mark them free or not. Then you can replace your malloc by a function that find the first struct widget available. This is similar to a slab allocator as found in various kernels.




回答5:


Its better to use The static Array approach, instead of the pointers you can have the Indexes of the element need to be accessed.

While you will be facing some common problenms like;

  • Fixed lenght of array (Max. limit need t ob e decided,)
  • Array using a memory will not be used for other perpose.
  • it has all the same problems as pointers.

Advantages are;

  • Simpler/ faster as the location is known,
  • Problems detection will be easy, as memory allocation will be adjucent.
  • entries in your list are more likely to be closer (in memory) to each other (unlike for malloc() where your entries are scattered among everything else you allocate), and this can improve performance (cache locality).


来源:https://stackoverflow.com/questions/29164110/what-is-a-faithful-alternative-to-a-linked-list-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!