0
std::string src;
cout<<"maximum length of string : "<<src.max_size();

Output I get is:

maximum length of string : 1073741820

I need to create a large array of strings, like string input[100000], but I get run time error when I use array indices more than 80,000. Length of my string variables are less, average length is 15 characters. So I need to limit the length of the string variable.

Following are my questions:

  1. What factors are considered for deciding the larget index of the string array?

  2. How do I reduce the max_size() of string variable?

5
  • 4
    I think you're confusing the concept of the current maximum allowable size and the current size of the string, just because you have an array of thousands of strings does not mean they are all of max_size Commented Apr 24, 2015 at 10:27
  • 4
    The max_size has absolutely nothing to do with this. It's not contributing to your error and it's not taking up memory. The entire premise of the question is broken. Commented Apr 24, 2015 at 10:27
  • How can i create large array of strings? Commented Apr 24, 2015 at 10:29
  • @CE: That is not a "correction". The singular of "indices" is "index", not "indice" (unless you are speaking French or are from the distant past). Commented Apr 24, 2015 at 10:30
  • Short story: use a std::vector<std::string>. Commented Apr 24, 2015 at 10:31

1 Answer 1

7

You have jumped to a wrong conclusion. std::string::max_size() is not representative of current memory usage and it is not contributing to your runtime error. It is an implementation quantity that tells you the maximum possible size of a string on your system: you cannot change it, and you do not need to.

You are smashing your stack at 80,000 std::strings because stack space is typically quite limited. It is up to your compiler to decide how much stack space will be available in your program. Typically, for an array this large, you would use dynamic or static allocation instead.

A good way to do that is to use the standard containers, e.g. std::vector<std::string>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.