Go to Post now now, there is no such thing as reading the manual too much. I read it every night just before I go to bed. - 663.keith [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-02-2007, 21:52
interfect interfect is offline
Registered User
FRC #2022
 
Join Date: Jan 2007
Location: Illinois
Posts: 18
interfect will become famous soon enough
Malloc?

Hello,
Does anyone have an implementation of malloc? We really need some way to dynamically allocate memory, as we're running up against the 256 byte global per file limit.
Thanks,
Interfect
  #2   Spotlight this post!  
Unread 01-02-2007, 22:00
MikeDubreuil's Avatar
MikeDubreuil MikeDubreuil is offline
Carpe diem
FRC #0125 (Nu-Trons)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Boston, MA
Posts: 967
MikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond repute
Send a message via AIM to MikeDubreuil
Re: Malloc?

I don't have an implementation for you but I will caution you against using malloc. In general, using malloc in an embedded software design is perilous at best and just plain wrong at worst. Mainly because in an embedded application you should keep a tight check on how much RAM your design uses. When there's not a lot available you have to ration the goods. If I were you I would try a different design to accomplish the task.
__________________
"FIRST is like bling bling for the brain." - Woodie Flowers
  #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.
  #4   Spotlight this post!  
Unread 02-02-2007, 01:01
Ken Streeter's Avatar
Ken Streeter Ken Streeter is offline
Let the MAYHEM begin!
FRC #1519 (Mechanical Mayhem)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Team: Milford, NH; Me: Bedford, NH
Posts: 471
Ken Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond reputeKen Streeter has a reputation beyond repute
Re: Malloc?

Quote:
Originally Posted by interfect View Post
Does anyone have an implementation of malloc? We really need some way to dynamically allocate memory, as we're running up against the 256 byte global per file limit.
There are a few different alternatives for getting around the 256 byte global per file limit.

If you need additional storage for variables, one option is to use the "#pragma" directive to explicitly put data into different memory "sections". Earlier this season, there was a previous Chief Delphi thread which discussed this approach. Another posting describing this technique can be found in this thread from 2006. Whether you use the "idata" or "udata" declarations depends upon whether or not the variables you are using are "initialized data" or "uninitialized data."

For more information on the use of the "#pragma" directive, you'll need to look at the MPLAB C18 Users manual. I don't know where to find the manual for version 2.4 of the compiler, but the MPLAB website does have the manual for the newer 3.0 version.

In the above manual, see pages 20-26 for information on using the #pragma directive to be able to use more than one memory section for a given file.

If the storage you need is for "constant" data (for example, a large lookup table that is initialized to certain values, but then never changed) you can declare the "variables" (the ones that are constants that never change) with the "rom" qualifier so that these "variables" are actually stored in program memory. In that way, you can store much larger lookup tables, as long as the code never changes them dynamically. (The lookup tables would need to be changed by recompiling in that case.)

Hopefully one of these approaches will work for you!
__________________
Ken Streeter - Team 1519 - Mechanical Mayhem (Milford Area Youth Homeschoolers Enriching Minds)
2015 NE District Winners with 195 & 2067, 125 & 1786, 230 & 4908, and 95 & 1307
2013 World Finalists & Archimedes Division Winners with 33 & 469
2013 & 2012 North Carolina Regional Winners with teams 435 & 4828 and 1311 & 2642
2011, 2010, 2006 Granite State Regional Winners with teams 175 & 176, 1073 & 1058, and 1276 & 133
Team 1519 Video Gallery - including Chairman's Video, and the infamous "Speed Racer!"
  #5   Spotlight this post!  
Unread 03-02-2007, 16:56
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,535
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Malloc?

I'm curious guys -

I've read the white paper on the limits of the pic - 30k, 256 bytes, etc etc.

Can someone tell me HOW to measure those on the program I'm writing? How do I know how many bytes my variables will take, and how do I know whether I'm using the full 30k or not?

Do I go through and manually try to figure out how big it's going to be by counting variables and their sizes?
  #6   Spotlight this post!  
Unread 03-02-2007, 17:53
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,856
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Malloc?

If you're using MPLAB you can see that info. about your program at:

View -> Memory Usage Gauge

Note that the program memory is expressed in two-byte integers, but Data Memory is number of bytes.

You can also set a flag in MPLAB under Project -> Build Options -> Project, then the MPLINK Linker tab and click on "Generate map file." That'll create a text file in your project directory with the extension .map that shows you where your variables get pegged in the PIC memory.

The PIC we're using this year (and last year's '06 version as well) has more memory, e.g.128k of program memory.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 03-02-2007 at 17:58.
  #7   Spotlight this post!  
Unread 05-02-2007, 09:15
Metalgod4eva Metalgod4eva is offline
Registered User
None #0870
 
Join Date: Jan 2007
Location: Southold
Posts: 7
Metalgod4eva is an unknown quantity at this point
Re: Malloc?

got to go to class but you can print back the size of malloc of a file.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Malloc, etc... rwaliany Programming 6 05-01-2004 02:18


All times are GMT -5. The time now is 23:56.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi