Coding without globals on Palm OS

Palm OS system libraries cannot have global or static variables. This is a limitation of Palm OS system libraries. You can define static constants (const), because these go into the code resource. However, if you are using CodeWarrior, static constants do not always go into the code resource.

Note

To determine if you are using global or static variables, check the size of each segment after you build your project. If you are using the Metrowerks IDE, you can check the segment sizes in the Project Inspector window. If you are using GNU tools, use the objdump tool. The size of the .data and .bss segments should be 0 for a Palm OS shared library. If they are not 0, check your code for global or static data.

Avoiding global data on Palm OS can be difficult. CodeWarrior for Palm OS may store any structure or array declared globally in the global data segment, even if the structure or array is declared const. Furthermore, both CodeWarrior and the GCC compiler in the prc-tools distribution store any structure in the global data segment if it contains pointers. For example, consider the following structure definition:

const struct foo {
  int x;
  const char *s;
  const char *t;
} myfoo = { 4, "mahir", "cagri" };

Both CodeWarrior and GCC store this structure in the global data segment, which means that you cannot use this definition in a POD on Palm OS. If you want to use a structure such as this one in a POD on Palm OS, you must generate it from code. The simplest way to do this is to use in-line fixed-size arrays:

struct foo {
  int x;
  const char s[8];
  const char t[8];
} myfoo = { 4, "mahir", "cagri" };

An alternate way to generate this structure is:

typedef struct MyPod {
  PODSPod* pod;
  struct foo myfoo;
} MyPod;

void init(MyPod* pod)
{
  pod->myfoo->x = 4;
  pod->myfoo->s = "mahir";
  pod->myfoo->t = "cagri";
}