Thread: Malloc?
View Single Post
  #3   Spotlight this post!  
Unread 02-02-2007, 00:10
Orborde Orborde is offline
Registered User
FRC #1747
Team Role: Mentor
 
Join Date: Apr 2004
Rookie Year: 2003
Location: Indianapolis, IN
Posts: 44
Orborde has a spectacular aura aboutOrborde has a spectacular aura about
Send a message via AIM to Orborde
Re: Malloc?

Some thoughts:

Are you defining constants as globals? You should really use #defines in header files for values that don't change, or alternatively:
Code:
rom const int my_constant = 6;
"rom" means "put this in read-only memory"; "const" means "I'm not going to change this, so do optimizations like putting it in read-only memory". I'm not sure if "const" is sufficient to get it out of RAM, but you get the picture.

You might also look into storing things in EEPROM (there is some sample code on Kevin Watson's site: http://kevin.org/frc ) if you want to "remember" variables between sessions. Or you could go completely hog-wild and implement swapping stuff to EEPROM... yeah, I'm getting carried away.

If you absolutely NEED a "dynamic" array, maybe you could allocate one on the stack, as follows:
Code:
int sum_numbers(int numbers)
{
  int arr[numbers];
  /* etc. Arr is only useful in this scope, though! */
}
The downside to this is that you only get to use the array as long as it's in scope, and I'm not sure if MCC18 supports that kind of array allocation, anyway.

As Mike said, if you need malloc(), your design needs to be modified. Kudos to you for being so good at C, but embedded programming is a different animal than a PC.

Last edited by Orborde : 02-02-2007 at 00:15.