bartop

palmoswerks ARCHIVE
Palm OS programming tips from a (former) CodeWarrior insider

header

Navigation

Search
Home
About
Stories
Stuff I Like
Articles
PilRC
CodeWarrior
palmos.techwood.org
DevTools List
Palm OS Dev FAQ

Adding raw data to your program

Sometimes, you need a big chunk of data included with your program. If you're doing a map charting program, you need a database of coordinates; if you're writing a strategy game, you need tables of information about the different units and how they interact with each other.

Traditionally, one method often used was to embed this data as constant arrays in a C or C++ program. This method doesn't work well on Palm OS, because the data gets placed in the data space of your program. This data space is limited to 64K, and its allocated directly out of the dynamic heap, so it takes away space from MemPtrNew or MemHandleNew calls.

This technique can be used, if you go about it the right way. First, be sure you've declared your data as read-only. This is done using the const keyword, e.g.

const int foo[] = { 1, 2, 3, 4, 5 };

Once you have your data declared like this, you can activate the compiler pragma that puts this data into the program space, rather than the data space. This pragma is "pcrelconstdata", and its set on and off like this:

#pragma pcrelconstdata on
... code that defines constant data ...
#pragma pcrelconstdata off

The one caveat is that when you declare data this way, you cannot modify it and you cannot access it outside the current segment. There is a workaround for the second problem: you can define a small function that returns the address of the data, then use that to get to it from other segments. For example:

#pragma pcrelconstdata on
const int my_data[] = { 9, 0, 2, 1, 0 };
int *get_my_data(void) { return &my_data; }
#pragma pcrelconstdata off

Next time: how to place read-only data into resources using Rez files for better memory management and flexibility.

Followup

Six months later, I finally finished this story. Go here for all the details.

brought to you by weblogger.com


Send feedback to combee@techwood.org
Copyright © 2004 Benjamin L. Combee
Palm OS is a registered trademark of PalmSource, Inc.
Metrowerks and CodeWarrior are registered trademarks of Metrowerks Inc.

The views expressed on this website/weblog are those of mine alone and do not necessarily reflect the views of PalmSource or Metrowerks.

This is a Manila Site

qwertYAK / frobnovich