开发者

Maximum number of characters in a string

开发者 https://www.devze.com 2022-12-21 01:40 出处:网络
Is there a maximum number of characters allowed in a string? If so, what is the limit on开发者_开发问答 the number of characters?For std::string str you can get maximum size as str.max_size().

Is there a maximum number of characters allowed in a string? If so, what is the limit on开发者_开发问答 the number of characters?


For std::string str you can get maximum size as str.max_size().

To get currently allocated size use str.capacity().


Are we talking "C string" or std::string... the former depends entirely on the size of your buffer. The latter should only be restricted by the amount of available memory.


My understanding is that the maximum number of characters in a C-style string is the capacity of the size_t type. The size_t is defined by the standard to be able to handle the largest size on the given platform. There may be lesser constraints such as the memory available to store the text (as either read only or writable).

As far as std::string (C++ string) goes, the limit is specified by the maximum value that the std::string::size_type type can accommodate. This varies among platforms and translators. Again, this quantity may be reduced by the platforms ability to store the string.

Some newbies have been able to declare 10 MB strings for processing files.


Assuming you are talking about an array of characters (and not something like std::string), then I believe the limit is 32768, depending on the compiler.

UPDATE:
As has been pionted out to me, this limit only applies when declaring an array on the stack like so:
char str[32768];

This limit does not apply when declaring the array on the heap like this:
char *str = new char[32769];

0

精彩评论

暂无评论...
验证码 换一张
取 消