c++stringsizetruncated

C++ string too big, trailing characters truncated


I am making a program that when started, will write a predefined string into a file:

string file_hex("huge_a**_string_goes_here_or_in_separate_cpp");

ofstream file_out;
file_out.open("tools\\c.exe", ios::binary | ios::trunc);
string res;
res.reserve(file_hex.size() / 2);
for (int i = 0; i < file_hex.size(); i += 2)
{
    std::istringstream iss(file_hex.substr(i, 2));
    int temp;
    iss >> std::hex >> temp;
    res += static_cast<char>(temp);
}

file_out << res;
file_out.close();

The file is about 5 mb in size so filling the string with 5 mb of data in hex is a big variable. When I try to compile it, I get an error saying the string is too big. Is 5mb really THAT big? I split the string into 4 sections but each section is still too big. :/ what can I quickly and easily do to fix this situation?


Solution

  • The Standard does not specify a maximum limit of string literals but does suggest a minimum size. From Annex B - Implementation quantities

    The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

    • Characters in a string literal (after concatenation) [65 536].

    However the same section in the standard also states the following

    Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

    It's safe to assume that a 5MB string literal is going to exceed maximum limits imposed by the compiler. You should refer to the documentation of your tool chain to determine what limits are imposed for string literals.

    If you are using the Visual C++ compiler the maximum size of a string literal is 16,384 bytes. From the MSDN documentation

    • Visual Studio prior to 2022 version 17.0: the maximum length of a string literal after concatenation is 65,535 bytes. This applies to both narrow and wide string literals.
    • From Visual Studio 2022 version 17.0 onwards: the maximum length of a string literal after concatenation is only limited by available memory. However, the size limit before concatenation is still 16,384 bytes

    "After concatenation" is referring to concatenating strings "like" "this".