On 19/08/11 17:53, Sergio Campamá wrote:
> Thanks for the insight into the problem. The thing is that I have a task
> scheduler, a linked list that, well, lists the tasks to perform... Since
> linked lists are dynamic in nature, I need malloc to create the new "task"
> and append it to the list.. And after the task is done, I free the task and
> go on to the next one...  The other place I use linked lists is to maintain
> a list of timers that the application uses to signal certain events... after
> the timer activates, it also gets freed...
>
> So that is why I need malloc and can't (or don't know how) to do it with
> other types of dynamic storage, or static. (On that subject, what other
> types of dynamic storage exist?)
>

You don't need dynamic memory for linked lists.

You need some sort of dynamic memory if you need to create and destroy 
list nodes at run-time in a non-deterministic way, and have these nodes 
live longer than the current function.

But if you just want to put nodes onto the list, or take them off, then 
you can use statically allocated nodes.

For example, you may have code that sets up a task like this:

void addTestTask(void) {
        node * pNode = malloc(sizeof(node));
        pNode->function = runTest;
        pNode->priority = 10;
        ...
        addToTaskList(pNode);
}

If there is only one test task, you can replace that with:

void addTestTask(void) {
        static node testNode;
        node * pNode = &testNode;
        pNode->function = runTest;
        pNode->priority = 10;
        ...
        addToTaskList(pNode);
}

It's almost the same code, but the memory is allocated statically and 
not dynamically.

If you need to have a set of similar list nodes, try making an array of 
nodes and using them.


You should consider malloc() as dangerous in embedded code, and free() 
is positively evil.





------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, 
user administration capabilities and model configuration. Take 
the hassle out of deploying and managing Subversion and the 
tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to