|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: Want Malloc on FIRST?
Sorry, I exaggerated when I said that dynamic allocation would be 100% necessary, as dynamic allocation can be written in terms of static allocation (as here). On the other hand, having a dynamic allocation system for certain things can result in cleaner code than without.
For example, lets say that in your autonomous, you want to watch out for nearby obstacles such as robots. When the rangefinder picks them up, you could store their location relative, a time, etc. Then, as objects are no longer detected for a certain amount of time, pass out of range, etc., you delete them. Now, you could have a stack, find the first unused "object", store in it, loop through them, make sure they are within range, mark them as unused, etc. Of course, now you just wrote a local dynamic allocator for that section of code, and when another part of your code works upon semi-similar principles, you just have to rewrite it again. Yet another chance for duplication, mistakes, and horrors. On the other hand, you could have a generic allocator which works consistently, and use it everywhere that it results in cleaner code than the custom written stack. Don't use this everywhere, but I'm sorry, static variables everywhere and custom stacks don't necessarily result in more bug- free code. |
|
#2
|
||||
|
||||
|
Re: Want Malloc on FIRST?
Quote:
In the end, there are other ways to do it that are just as simple (or simpler) and just as elegant, but don't carry the negatives of attempting to use dynamically allocated memory in a situation where you have extremely limited resources. |
|
#3
|
|||
|
|||
|
Re: Want Malloc on FIRST?
What you outline is a simple way implementing dynamic memory... why hack together something like that everytime you need to do something.
What he has done is basically provide a general way of doing what you just outlined that works for any datatype. |
|
#4
|
||||
|
||||
|
Re: Want Malloc on FIRST?
This a cool academic exercise but since FIRST is about real-world practices, I'll add in another 2 cents.
"For example, lets say that in your autonomous, you want to watch out for nearby obstacles such as robots. When the rangefinder picks them up, you could store their location relative, a time, etc. Then, as objects are no longer detected for a certain amount of time, pass out of range, etc., you delete them. Now, you could have a stack, find the first unused "object", store in it, loop through them, make sure they are within range, mark them as unused, etc." Such a data structure would be fixed in size. In a real-time embedded environment, this would never be dynamically allocated (except maybe the entire memory area needed and then only during startup). One would set up an array of these objects and build a simple library to "allocate" them and "return" them to the pool. This would be a constant order operation. Non-deterministic functions (like malloc) are not a good idea in time sensitive software/firmware. Random-sized dynamic allocations can't be done deterministically (easily) because any algorithm to traverse the free-list is O(f(n)) and must be protected using mutual exclusion. This is the case in every OS I've ever used (and that is a long list). HTH |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| malloc? | Jake M | Programming | 5 | 21-06-2007 21:24 |
| Malloc? | interfect | Programming | 6 | 05-02-2007 09:15 |
| Do you want to Volunteer with FIRST? | dez250 | General Forum | 13 | 18-11-2004 10:03 |
| Colleges Want FIRST Students | Brandon Martus | Announcements | 1 | 11-01-2004 23:50 |
| Malloc, etc... | rwaliany | Programming | 6 | 05-01-2004 02:18 |