Search code examples
c++reinterpret-cast

C++ reinterpret_cast, making unique number


Recently, i'm using a code to make unique int number for my classes.

I used reinterpret_cast<int>(my_unique_name) where my_unique_name is a char [] variable with unique value. Something like below:

const char my_unique_name[] = "test1234";

int generate_unique_id_from_string(const char *str)
{
  return reinterpret_cast<int>(str);
}

My question is, is the generated int really unique for all entry strings ?


Solution

  • No, it is not. You are casting the address of the string, not its contents.

    To create a numeric value based on string input, use a hash function. However, this doesn't create a truly unique number, because of the so-called pigeonhole principle.