HashMap library

Hi everyone,
I am using the HashMap library and I am getting unknown output. The following is my setup. I have verified the hash map that I have created using the debug function and the following are the results of the serial prints. temp: 1010, lookUpvar: 1010, strcmp prints 0, but LookUpResult prints "' ? ? ?" (inverted question marks) rather than printing the value in the hash map. Anyone knows why is this happening?

const byte hashsize = 16;
HashType<char*,char*> hashArray[hashsize];
HashMap<char*,char*> Map = HashMap<char*,char*>( hashArray , hashsize );

void setup(){
  // HashMap Initializations
  Map[0]("0000","11110");
  Map[1]("0001","01001");
  Map[2]("0010","10100");
  Map[3]("0011","10101");
  Map[4]("0100","01010");
  Map[5]("0101","01011");
  Map[6]("0110","01110");
  Map[7]("0111","01111");
  Map[8]("1000","10010");
  Map[9]("1001","10011");
  Map[10]("1010","10110");
  Map[11]("1011","10111");
  Map[12]("1100","11010");
  Map[13]("1101","11011");
  Map[14]("1110","11100");
  Map[15]("1111","11101");
}

void loop(){
   String temp;
   char lookup_var[4];
   char* lookup_result;

    temp = "1010";
    Serial.print("temp:");Serial.println (temp);
    strcpy(lookup_var, temp.c_str());
    Serial.print("lookUpvar:");Serial.println (lookup_var);
    Serial.println(strcmp(lookup_var,"1010"));
    lookup_result = Map.getValueOf(&lookup_var[0]);
    Serial.print("LookUpResult:"); Serial.println (lookup_result);
}
1 Like

Why are you using

lookup_result = Map.getValueOf(&lookup_var[0]);
[\code]

Check your operator precedence.  First, you take the subscript (which I'm assuming returns a char, but you didn't define that in your sketch) and then you take the address of that.  Probably not what you want.

just just the variable as-is
[code]
lookup_result = Map.getValueOf(lookup_var);
[\code]

Hey sorry, I just added the declarations. Really sorry, it is an excerpt from a really long code. That was why I missed it. And I tried that too. It did the same thing; printed the same random output. Also, adding one more point, when hardcoding the variable as being char* temp_1 = "1010", It returned the appropriate output when being called as Map.getValueOf(temp_1). Anyone knows why?

Three things:

  • char lookup_var[4]; is too small to hold "1010" - you forgot the C-string null termination character.
  • Looking at the library code here, it is not a piece of well polished software. There are functions that are supposed to return values, but don't or don't always.
  • You are trying to use a low level C-string when a higher level construct is required. Basically, you cannot use a simple char* as a hash key because when you use the equality operator (==) you are comparing pointers, not the content of the C-string. That causes the getValueOf() function to fail.

Function edited with your template types:

	char* getValueOf( char* key ){
		for (byte i=0; i<size; i++){
			if (hashMap[i].getHash()==key){ // ERROR: The strings may be equal, but the pointer values are compared here!
				return hashMap[i].getValue(); // So we sometimes don't return a value here.
			}
		}
		// And now a missing return statement means undefined results.
	}

I guess the HashMap may be usable with a String class key, but use of String is frowned upon here.

Hi there, I have tried increasing the size to 5 to account for the null-termination character. The code still displays the same output as above (gibberish). Also, somehow I am not allowed to use type string when declaring the HashMap. The IDE gave me an error saying, error compiling for Arduino/Genuino Uno.

Hi, thank you everyone! I ended up solving the problem by changing the code in the HashMap library to use strcmp instead of the == sign. Thank you so much for your time!