Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase string literal length limit

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
    
like image 498
MiP Avatar asked Jul 24 '17 08:07

MiP


People also ask

Is it possible to modify a string literal?

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.

How do I limit the length of a string in C++?

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.

What is the maximum possible length of a string?

While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.

Is it possible to modify a string literal in C?

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).


2 Answers

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.

like image 111
mksteve Avatar answered Oct 16 '22 09:10

mksteve


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

like image 23
JazzSoft Avatar answered Oct 16 '22 10:10

JazzSoft