Thread: Embedded system link list

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    22

    Embedded system link list

    Hello Everyone

    I am working on an embedded system wherein I have limited available space. I know there is a dynamic memory allocation available for link list creation but I want to create a link list using static memory allocation. Dynamic memory allocation leads to or may lead to leaks if multiple dynamic allocations takes place. I am not sure, however there is a big possibility that if memory management is not handled in a static manner, may lead to leaks and crush especially in embedded system.
    So I wanted to know if anyone has any suggestion as to how to create a link list using static memory management. The system needs to create c link list by adding an element to the link list(if the element is already present then discard the element).

    Code:
    typedef struct node;
    struct node
    {
       uint8_t id;
       struct node *next;
    }
    Thanks to everyone.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How big do you expect your data to grow? Perhaps it is feasible to simply use an array of fixed size.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The first thing you should consider if you're on a memory constrained embedded system is a linked list the best "container" for the job. Even if you have perfect memory allocation/de-allocation a dynamic list can still consume all available memory in a memory constrained system because it will grow to the size needed. You need to constrain this growth to a known limit.

    Perhaps a simple statically allocated array is all you need.


    Jim

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Hello Matticus and jimblumberg,

    Thank you for your reply.
    Yes my embedded system is under memory constraints. I am looking for max #(uint8_t id)=200.

    Code:
    typedef uint8_t RecvId[8];typedef struct node;
    
    struct node
    
    {
    
       Recv id;
    
       struct node *next;
    
    }
    
    add_id()
    {
    //
    }
    
    search_id()
    {
    //
    }
    
    int main()
    {
    add_id();//adds an id to the link list
    search();//returns a node if an id already exist
    
       return 0;
    }
    This is a base for my code without malloc. Thanks again

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why do you think you need a linked list?

    What exactly are you trying to accomplish?

    I really really suggest you reconsider you approach. Think about a statically allocated array instead.



    Jim

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're just using a static array, you don't need a "next" pointer.

    In fact, if the only data you need is "id", then you don't even need a struct.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    jimblumberg My goal is to receive an id which is of 8byte ,prepended with a "key" of 3 byte ,received in a buffer. once i receive this buffer, I add this id info into my link list . I have to also check if the id already exists. If it does exist, i drop the id and wait for another Id. as there are other struct members , therefore, i used struct. And I thought link list would serve my purpose. If there is any better suggestion i am open to it as long as i dont have to worry about malloc replacement for the embedded system.

    Matticus Would you like to elaborate on your point. Thankkss

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    My goal is to receive an id which is of 8byte ,prepended with a "key" of 3 byte ,received in a buffer. once i receive this buffer, I add this id info into my link list . I have to also check if the id already exists. If it does exist, i drop the id and wait for another Id. as there are other struct members , therefore, i used struct. And I thought link list would serve my purpose. If there is any better suggestion i am open to it as long as i dont have to worry about malloc replacement for the embedded system.
    O_o

    I'm not going to do your work for you, but I am curious. Why do you imagine you need a linked list for the project? Searching a linked list on an embedded system without `malloc'/`free' seems like choosing to start with problems.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Hello Soma

    Yes I have gone through the search function for link list without malloc and some other related docs on google. But it doesnt give a clear picture. As also mentioned above, I am under memory constraints so link list would be more efficient than to have an array(chuck).Do you have any suggestions??
    Last edited by amaturequestion; 04-08-2015 at 10:10 AM.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Do you have any suggestions?
    O_o

    I suppose that would depend on your perspective. I either have several or none.

    I've been long spoiled by quality implementations and beefier systems too long to be afraid of `malloc'/`free' problems real or imagined, but the cost of inserting a node in a sorted link list could be too costly for any given message making the allocation irrelevant.

    *shrug*

    What is the context?

    [Edit]
    As also mentioned above, I am under memory constraints so link list would be more efficient than to have an array(chuck).
    Just to shim the discussion, the suggestion to use a strategy built around an array is offered in the context of a controlled limited memory environment where the cost of the `next' pointer is wasted space if you don't actually need the functionality a linked list offers. In such context, the array is more memory efficient.
    [/Edit]

    Soma
    Last edited by phantomotap; 04-08-2015 at 10:27 AM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and Embedded system
    By san_crazy in forum C Programming
    Replies: 6
    Last Post: 09-01-2008, 06:01 PM
  2. embedded menu system
    By v_dave in forum C Programming
    Replies: 26
    Last Post: 03-14-2008, 02:27 PM
  3. Embedded system board
    By ssharish2005 in forum Tech Board
    Replies: 1
    Last Post: 08-12-2007, 03:03 PM
  4. link error with MS embedded VC++ 4.0
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2006, 05:57 AM
  5. Embedded system design
    By andyhunter in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-19-2005, 07:10 AM

Tags for this Thread