For a program written in C++, I need two huge arrays of strings that contain data.
They are defined in a header file as follows:
#include <string>
static const string strdataA[30000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
down to
"last line."};
//second array strings
static const string strdataB[60000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
down to
"last line."};
But when I compile this with g++, it takes so long that I have not seen it complete. It also uses about two GB of virtual memory. So I commented out strdataB[], and then the program did compile, but still after a long while. The executable was only about 8 Mb and worked fine.
What can I do in order to speed up the compiling process? I don't mind if I have to change the code, but I don't want to use an external file to load from. I would like an array because it works extremely well for me inside the program.
I read on the net somewhere that "static const" should do the trick, but I learned by experience that it doesn't.
Thanks a lot in advance for any suggestions!
You should not use std::string
for that. Use instead plain old const char*
:
const char * const strdataA[30000] = {
"one",
"two",
//...
};
The static
keyword shouldn't make much of a difference here.
This way, the strings themselves will be stored in the read-only data section as simple literals, and the array itself will be simply an array of pointers. Also, you avoid running the strings constructors/destructors at runtime.
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