Log in

View Full Version : Dynamic memory MC18


Cadyyan
18-08-2009, 14:54
How exactly do you dynamically allocate memory for an array in C? "New" is not a keyword and malloc does not seem to be included in the IFI library. There has to be a way to allocate memory. Isn't there?
~Cadyyan~

Jared Russell
18-08-2009, 16:27
To the best of my recollection, the PIC18 did not support dynamic memory allocation. You always have the option of declaring a big array at global scope and using that area as "scratch" for dynamic memory operations.

You could also dig up some EEPROM code and use that as your "dynamic" memory (Kevin Watson had written a nifty utility to read and write EEPROM). Just be aware that EEPROM is typically much slower than using the stack.

EDIT: I have checked and confirmed; the PIC does not support dynamic memory allocation.

Dave Flowerday
18-08-2009, 16:47
To the best of my recollection, the PIC18 did not support dynamic memory allocation.
It's not that the PIC18 won't support it, it's simply not implemented in any of the default libraries. It's not implemented because of limited system resources and the fact that it almost never makes sense to use dynamic allocation on a processor as small as a PIC. With that in mind, you might be able to find a 3rd party implementation, or you could certainly write your own.

To the original poster: I'd ask what you're trying to do that would use dynamic memory. Anything related to robot control should be well-enough understood in advance that you shouldn't need to dynamically allocate anything. You can likely rework your design to not need dynamic memory and be more efficient in the process.
You could also dig up some EEPROM code and use that as your "dynamic" memory (Kevin Watson had written a nifty utility to read and write EEPROM). Just be aware that EEPROM is typically much slower than using the stack.
Yikes, I wouldn't do that. EEPROM has a limited number of write cycles before it wears out. It's meant for long-term storage of data, not random-access while a program is running. Plus, "typically much slower" is a large understatement. Writing to EEPROM can easily be 1,000,000x slower than writing to RAM (yes, that's 1 million times slower).

Cadyyan
18-08-2009, 17:15
Well thanks. I think the best I could do would be to either make an array that "should" be large enough or else write a bunch more code for different scenarios. Thanks again for the responces.