I am working in C++ with BCC32. I initialized a map with about 1000 entries like this:
extern map<string, string> city ;
void region_init_0 (void) {
city["abc01"] = "Brussels" ;
city["xyz03"] = "Liege" ;
...
}
The length of the .cpp file is 40 KB. After compilation, I get a .obj file of 2.2 MB. After linking with other modules, the .exe file is also 2 MB longer than before I added the map. I don't understand why I get this ratio of 50 between the length of the object code and the total length of the ASCII strings.
How can I reduce that? I guess that there must be more clever ways to initialize a map that will remain constant during the execution of the program.
Thanks.
Have you enabled optimizations in your compiler?
You could perhaps have some code like
typedef std::pair<const char*,const char*> paircstr_t;
const paircstr_t initarr[] = {
{ "abc01", "Brussels" },
{ "xyz02", "Paris" },
/// etc...
{ (const char*)0, (const char*)0 } // terminating null placeholder
};
extern map<string, string> city ;
void region_init_0 (void) {
for (int i = 0;; i++) {
const char* curname = initarr[i].first;
const char* curcity = initarr[i].second;
if (!curname || !curcity) break;
map[curname] = curcity;
}
}
The object code size might be smaller, but the runtime heap size won't change.
Just a guess: Is operator[] inline? Hopefully you can change this by (un)defining some preprocessor constant. Is the release build larger or smaller than debug?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With