 |
Sunday, September 16, 2001 |
 |
|
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.
|
|
| September 2001 |
| Sun |
Mon |
Tue |
Wed |
Thu |
Fri |
Sat |
| |
1 |
| 2 |
3 |
4 |
5 |
6 |
7 |
8 |
| 9 |
10 |
11 |
12 |
13 |
14 |
15 |
| 16 |
17 |
18 |
19 |
20 |
21 |
22 |
| 23 |
24 |
25 |
26 |
27 |
28 |
29 |
| 30 |
|
|