Reputation: 421
I'm writing an application on an embedded system. I need some type of associative container to be able to access a certain pointer based on a string. Currently I'm using maps (i.e. std::map<char*, SomeType*, CustomComparator>
). I'm using char* as the key type, because I'm communicating with other libraries which have to support for std::string and I do not want to keep casting back and forth.
But i'm running into an unexpected problem. Every instance (i.e. new type) of std::map takes up about 10Kb in the resulting binary (with no optimizations, for debugging purposes). Since I'm limited something around 500Kb of ROM and I'd possibly need a few dozens, this is a pretty significant drawback (I want to do the debugging on the actual hardware, which I can't if the program does not fit). I'd like to be able to access these items in logarithmic time, if possible. I do not want to resort to using arrays and looping through every item till I find the one I'm looking for. Inserts do not have to be fast, since that's done only during startup. Deletes are executed only during shutdown, so they aren't an issue either.
Does anyone have an alternative idea I could use (preferably STL)?
Note: I'm restricted to C++03.
Upvotes: 3
Views: 1993
Reputation: 44238
Obvious solution could be to use std::map<const char *, void *, CustomComparator>
under the hood and wrap typecasting under template interface with inline functions. That should eliminate generation of different type of std::map
for every different pointer type.
Upvotes: 5