tags:

views:

243

answers:

1

What are the string limits for the Standard Template Library in C++?

+16  A: 
#include <iostream>
#include <string>

int main() {
    std::cout << std::string().max_size() << std::endl;
    return 0;
}
Alex B
yea, interesting that `std::string::allocator_type().max_size()` gives a different (larger) answer. Not sure why that is..
Evan Teran
The specific semantics of `max_size()` for allocators is rather vague.
Pavel Minaev
@Pavel: agreed, but std::allocator<T> is supposed to return "how many of T can I theoretically allocate contiguously". On my system this is `size_type(-1)`...But `std::string::max()` is about a quarter of that which seems odd that it would be so conservative. I mean, if the allocator says it might be able to, why not try and let it fail with a `std::length_error` (as it would anyway)?
Evan Teran