Search code examples
c++c

Is there a way to use pointers of constant strings as ids?


Is there a way to guarantee that a compiler uses the same pointer for same value string constants?

void *gettok() {
  ...
  return "if";
}
...
tok = gettok();
...
if (tok == "if") {...}

without going through some map and without overloading "=="? I just want to use the constant string pointer as an id.


Solution

  • You can, but you cannot expect the compiler (or the language implementation) to ensure that the same string literal will be unique in a program. This situation gets worse when you include dynamically constructed strings in the problem.

    A simple solution for that is to intern the strings. This is, create a module that warrants that the strings you use in your application (statically or dynamically created) are unique. A simple hash table, a red/blue tree, or an avl tree will give you with enough support for all strings.

    Instead of comparing the pointers as they are received, you proceed to intern them (you just use a function that returns the interned version of the string --intern(const char *s) should be an idempotent function) and compare the interned versions. Depending on your implementation it is easy to achieve, that your references to the same strings are always equal.

    Beware that you can run out of resources if you expose the creation of new strings to a bad behaved user.

    I will not go in the details on how to construct such implementation, but basically the use case is:

    void *gettok() {
      ...
      return intern("if");
    }
    ...
    tok = gettok();
    ...
    if (tok == intern("if")) {...}