Max length of a string literal?
Asked Answered
S

1

6

I am trying to create a long string literal, which I store inside a std::string. If I create a literal up to approximately 2600 characters, everything prints fine. If I go beyond that number of symbols, I only get some random garbage characters printed.

I have been using the C standard as guidance, environmental limits are specified as "4095 characters in a string literal (after concatenation)". But the code is written in C++.

So my question is, what is the minimum amount of characters in a C++ string literal?

(The problem might possibly be elsewhere in the code, but I would like to ensure that I don't pass a limit set by the standard. The text is printed in a RichEdit control, so I doubt that one is the culprit.)

Seldon answered 29/10, 2012 at 16:10 Comment(7)
The linked question is about std::string, not string literals.Seel
Because Windows will be converting the single-byte string to Unicode, it might indeed have its own limit independent of C++. Try looking at the string in the debugger.Potluck
Can you print the string to the console instead of the RichEdit control?Townsman
@aschepler, This question is about std::string, first sentence.Sterilant
@Seel Indeed, it does me no good if I can fit one billion characters in a std::string, if it is initialized from a constant string literal with a more narrow limit. The "possible duplicate" does not answer my question in the slightest. I'll flag this for moderator attention.Seldon
@Sterilant Yes but string literals are stored in memory before placed inside a std::string, so the limits of std::string are rather irrelevant.Seldon
@Mark Ransom: The (MSVC) debugger truncates strings for display to a rather short maximum, much much less than what the WinAPI ...A functions will be able to handle. I would suggest using strlen() to determine the length of the literal and then asserting that the last few characters have the expected value.Pazice
F
11

The minimum is specified in

Annex B
Implementation quantities [implimits]

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

But note that:

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

2) 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. (emphasis mine)

Your implementation should provide you with this number though.

Floc answered 29/10, 2012 at 16:13 Comment(4)
Does this make any program that uses anything constrained by these limits implementation defined?Pattern
@sftrabbit I think this part alltogether is implementation-defined.Floc
Ah, Annex B is marked as "informative", which according to the ISO/IEC directives means it is non-normative. Limits are only mentioned in Annex B, so as far as the author of a program is concerned, the limits do not exist.Pattern
Thank you, this answers my question (unlike the "possible duplicate"). I have already checked compiler docs but they are next to useless for this particular compiler (Embarcadero C++ Builder). I'll start a support errand with them I guess.Seldon

© 2022 - 2024 — McMap. All rights reserved.