C ++中字符串大小的限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3649639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
limit on string size in c++?
提问by aks
I have like a million records each of about 30 characters coming in over a socket. Can I read all of it into a single string? Is there a limit on the string size I can allocate?
我有一百万条记录,每条记录大约有 30 个字符通过套接字输入。我可以将所有内容读入一个字符串吗?我可以分配的字符串大小是否有限制?
If so, is there someway I can send data over the socket records by record and receive it record by record. I dont know the size of each record until runtime.
如果是这样,是否可以通过某种方式通过套接字记录发送数据并通过记录接收数据。直到运行时我才知道每条记录的大小。
回答by Goutham
To answer your first question: The maximum size of a C++ string is given by string::max_size
回答您的第一个问题:C++ 字符串的最大大小由string::max_size 给出
回答by usta
std::string::max_size()
will tell you the theoretical limit imposed by the architecture your program is running under. Other than that, as long as you have sufficient RAM and/or disk swap space, you can have std::strings
of huge size.
The answer to your second question is yes, you can send record by record, moreover you might not be able to send big chunks of data over a socket at once - there are limits on the size of a single send operation. That the size of a single string is not known until runtime is not a problem, it doesn't need to be known at compile time for sending them over a socket. How to actually send those strings record by record depends on what socket/networking library you are using; consult the relevant documentation.
std::string::max_size()
会告诉您程序运行所在的架构所施加的理论限制。除此之外,只要您有足够的 RAM 和/或磁盘交换空间,您就可以拥有std::strings
巨大的尺寸。您的第二个问题的答案是肯定的,您可以逐条记录发送,而且您可能无法一次通过套接字发送大块数据 - 单个发送操作的大小有限制。直到运行时才知道单个字符串的大小不是问题,因此不需要在编译时知道通过套接字发送它们。如何逐条记录实际发送这些字符串取决于您使用的套接字/网络库;查阅相关文档。
回答by BeWarned
The size of a string is only limited by the amount of memory available to the program, it is more of a operating system limitation than a C++ limitation. C++/C strings are null terminated so the string routines will happily process extremely long strings until they find a null.
字符串的大小仅受程序可用内存量的限制,与其说是 C++ 限制,不如说是操作系统限制。C++/C 字符串以空值终止,因此字符串例程将很乐意处理极长的字符串,直到找到空值为止。
On Win32 the maximum amount of memory available for data is normally around 2 Gigs.
在 Win32 上,可用于数据的最大内存量通常约为 2 Gigs。
You can read arbitrarily large amounts of data from a socket, but you must have some way of delimiting the data that you're reading. There must be an end of record marker or length associated with the records that you are reading so use that to parse the records. Do you really want read the data into a string? What happens if your don't have enough free memory to hold the data in RAM? I suspect there is a more efficient way to handle this data, but I don't know enough about the problem.
您可以从套接字读取任意大量的数据,但是您必须有某种方法来分隔您正在读取的数据。必须有与您正在阅读的记录相关联的记录结束标记或长度,以便使用它来解析记录。您真的要将数据读入字符串吗?如果您没有足够的可用内存来将数据保存在 RAM 中,会发生什么?我怀疑有一种更有效的方法来处理这些数据,但我对这个问题的了解还不够。
回答by Oliver Bock
The only practical limit on string size in c++ is your available memory. That being said, it will be expensive to reallocate your string to the right size as you keep receiving data (assuming you do not know its total size in advance). Normally you would read chunks of the data into a fixed-size buffer and decode it into its naturally shape (your records) as you get it.
C++ 中字符串大小的唯一实际限制是您的可用内存。话虽如此,随着您不断接收数据(假设您事先不知道其总大小),将您的字符串重新分配到正确的大小将是昂贵的。通常,您会将数据块读取到固定大小的缓冲区中,并在获得时将其解码为自然形状(您的记录)。
回答by Daniel Daranas
There is no official limit on the size of a string. The software will ask your system for memory and, as long as it gets it, it will be able to add characters to your string.
字符串的大小没有官方限制。该软件将要求您的系统提供内存,只要它得到它,它就能够向您的字符串添加字符。
The rest of your question is not clear.
你的问题的其余部分不清楚。
回答by Alexander Rafferty
In theory, no. But don't go allocating 100GB of memory, because the user will probably not have that much RAM. If you are using std::strings then the max size is std::string::npos.
理论上,没有。但是不要分配 100GB 的内存,因为用户可能没有那么多内存。如果您使用的是 std::strings,则最大大小为 std::string::npos。
回答by Alexander Malakhov
If we are talking about char*
You are limited with smth about 2^32 on 32-bit systems and with 2^64 on (surprise) 64-bit ones
如果我们在谈论char*
您在 32 位系统上的限制约为 2^32,而在(令人惊讶的)64 位系统上限制为 2^64
Update:This is wrong. See comments
更新:这是错误的。看评论
回答by ironhead
How about send them with different format?
用不同的格式发送它们怎么样?
in your server: send(strlen(szRecordServer)); send(szRecordServer);
在您的服务器中: send(strlen(szRecordServer)); 发送(szRecordServer);
in you client: recv(cbRecord); alloc(szRecordClient); recv(szRecordClient);
在你的客户端: recv(cbRecord); 分配(szRecordClient);接收(szRecordClient);
and repeat this million times.
并重复此百万次。