Post details: Hack for globally unique values (error codes, enumerations, etc.)

03/01/06

Permalink 01:17:30 pm, Categories: Programming, 607 words   English (US)

Hack for globally unique values (error codes, enumerations, etc.)



Programmers often have a problem with globally unique values, but there are a solution:

[More:]

There are infinite quantity of natural numbers and they can be used as unique values:

const int value1 = 1;
const int value2 = 2;
const int value3 = 3;
/* ... */

But there are the need of administrative development coordination for two programming teams to not assign the same numeric value to two different values which should be distinct. Even if there are only one programmer in a project, this indeed is a problem as he needs to coordinate different parts of the project (e.g. different derived classes of a common base class). There are also the need to coordinate different versions of the software.

Maybe even worse, it makes hard to write software which can automatically generate globally unique values. This little detail is a big obstacle on the way of development of compilers, programming languages and programming tools in general.

But there are indeed a solution! A simple an elegant solution:

Declare an one byte variable (or better constant) for each distinct value you need:

const char cValue1 = 0;
const char cValue2 = 0;
const char cValue3 = 0;
/* ... */

Then you can use pointers to these variables as globally unique values:

const char *error = &cValue2;

Globally unique values will be pointers (addresses) which are automatically generated by the compiler or linker and are automatically ensured to be unique (distinct)!

Many modern compilers (such as GNU C compiler) will allocate the addresses (that is pointer values) on the stage of linking. This has the implication that despite of the actual numeric values (addresses) of the pointers will be different for every executable (program), thanks to the linker doing linking based on names rather than addresses of variables, different versions of your software (or libraries) will be binary compatible (compatible on binary level, not only source compatible!) with each other provided that you do not change the names of the variables.

Great?! Linker automatically does for you the job of unique values allocation! Yeah!

This unique values allocation may be done either statistically (if the program is linked statistically), or even dynamically (e.g. if the error codes are in a shared library (DLL) and linking is done dynamically). Note that in the case of dynamic linking the unique values (e.g. error codes) may be different between different program runs (dependently on which shared libraries (DLLs) are dynamically loaded).

Even in the case of static linking the values will be different in different compilations of the same program. So you can't use the autogenerated unique values to be stored externally, if you need to store externally (or otherwise export) the values, my technique is not suitable for you.

Finally, if a byte of disk space is important for you, you may ensure that your compiler does not unnecessarily allocate space for fake constants (zero constants cValue1, cValue2, etc. above) used only as destinations for pointers, not as actual values. (Some linkers may do this optimization in the case if the values are zero, as in my example above.) Also in the case of dynamic linking the executables (and DLLs) size will increase as these need to contain the symbolic names of the unique value (speaking more exactly, of the objects pointed by the unique values that is pointers). However, it is better to lose a byte for every error code than to have your project failed or the same value used for different error code.

Note that the speed of dynamic linking will be decreased only a little as modern linkers a capable to link many values (a whole block of memory, i.e. a whole object or even whole library) at once.

Comments, Trackbacks, Pingbacks:

No Comments/Trackbacks/Pingbacks for this post yet...

Software Blog

See also my free software. This weblog will contain information about:
  • software developed by me;
  • my software patches (for others' software);
  • my software related reviews, comments, suggestions, ideas
and other misc software related things.

Recently ||

Last comments

Search

Syndicate this blog XML

Add to MyYahoo

What is RSS?

powered by
b2evolution