I have a lot of string literals that are bigger than 65535 bytes. I'm not allowed to save these strings to seperate files, how can I workaround with the string limit?
https://stackoverflow.com/a/11488682/7821462
MSVC: 2048
GCC: No Limit (up to 100,000 characters), but gives warning after 510 characters:
String literal of length 100000 exceeds maximum length 509 that C90 compilers are required to support
The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.
You can construct a string with the capacity to hold 50 characters by using: std::string str(50, '\0'); However, unlike C arrays, it is possible to increase its size by adding more data to it.
While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.
The only difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a C-style string will be just as happy to accept string literals unless they modify the string (in which case your program will crash).
These large strings seem more like resources than code, and I would use the resource section of a windows binary (FindResource/LoadResource) and the answer to the SO: embedding resources into linux executable to insert the same data into linux.
You can split the text string into multiple strings. Following code worked on Visual Studio 2017:
const char* p1 = "1234567890..."; // Very long (length > 65000)
const char* p2 = "abcdefghij..."; // Very long (length > 65000)
string s = p1;
s += p2;
cout << s.size() << endl;
You have to write the text string in multiple lines like:
const char* p = "This is a "
"very long string...";
Actually the maximum limit in Visual C++ is 65535. Here is the compiler error message:
fatal error C1091: compiler limit: string exceeds 65535 bytes in length
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