6

I have the following function:

int Foo(string sentence);

I want to know what is the maximum string length I could pass? I think it should depend on the stack size allocated to the function as this string will be copied to the stack, is that true? or it depends on string::max_size value? I'm using C++ under VS2010, windows7

EDIT. I need to have a copy as the function modifies the string contents.

q-l-p
  • 4,304
  • 3
  • 16
  • 36
Ahmed
  • 7,148
  • 12
  • 57
  • 96
  • Is string::max_size a real thing? – sje397 Nov 29 '10 at 13:28
  • Is the only thing you can rely on. – Simone Nov 29 '10 at 13:36
  • 2
    Note that [usually you should take string argumenta per `const` reference](http://stackoverflow.com/questions/2139224/2139254#2139254). – sbi Nov 29 '10 at 13:43
  • Passing by value, since Ahmed said (in a comment on an answer) a copy is required anyway, is [recommended](http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/). – Fred Nurk Nov 29 '10 at 14:02
  • Please [accept](https://stackoverflow.com/help/someone-answers) the answer that solves your problem. Or answer your own question if you have found a solution. – q-l-p May 27 '18 at 16:38

6 Answers6

8

The std::string object will be copied onto the stack, but the string body will not - it will be allocated on heap. The actual limitation will depend on the system and program memory usage and can be something like from ten million to one billion charactes on a 32-bit system.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
2

It does not have anything to do with stack size - the string contents are assigned on the heap. So it depends either on string::max_size or how much RAM/virtual memory you have available in your computer.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
2

You can check std::string's max_size() member function.

EDIT

If your standard library implementation doesn't implement Copy On Write optimization, you should pass big strings by const reference. This is true also if you want to be independent from the STL.

Simone
  • 11,655
  • 1
  • 30
  • 43
  • max_size doesn't tell you anything useful. – Fred Nurk Nov 29 '10 at 13:45
  • max_size() gives the answer to OP's question, provided the STL's implementation is correct. – Simone Nov 29 '10 at 13:53
  • That would be true only if you can always create a copy of a string that is smaller than max_size(), but you can't. The latter part of your answer is only true when you don't always need a copy, but the OP has said that's not the case here. – Fred Nurk Nov 29 '10 at 14:05
  • The only safe assumption you can do is that strings greater than max_size() are impossible to create. You can only check if you can actually create a string of size *n* by do instantiating it. – Simone Nov 29 '10 at 14:26
  • @Simone: I'd be surprised about a std lib still doing COW. In an MT scenario (which is more or less common nowadays) this is a pessimization. (I think small-string optimization is the current state of the art.) – sbi Dec 03 '10 at 09:04
  • @sbi would a std lib that uses COW only for ST application be so suprising? – Simone Dec 03 '10 at 12:42
  • @Simone: Yes. COW is _very_ hard to get right with mutable strings (and when you have it right, there's little ROI) and it really wreaks havoc as soon as programmers start to [resort to hacking](http://stackoverflow.com/questions/1919626/can-i-get-a-non-const-c-string-back-from-a-c-string/1919654#1919654). SSO has a much better ROI. – sbi Dec 03 '10 at 12:57
  • I believe that GCC's std lib still uses COW. – alexk7 Jan 14 '11 at 21:56
1

It's much more likely that the maximum string length will be determined by the constraints of your system than the theoretical maximum value returned by max_size. max_size on Windows 32-bit is 4 GB, but you will never be able to construct a string that size in the real world on a 32-bit Windows box.

The maximum size you can use at any given juncture will vary, and will likely be (after accounting for string's object size) the same as the biggest contiguous heap allocation that's possible at that instant. This is affected by machine config (how much RAM, how much kernel reserved space, how many other programs and their memory profile). If you then try to do anything else that requires heap memory, you will get exceptions of one type or another. You are better off not getting near to this scenario in the first place.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

It will never be more than string::max_size, as that returns the maximum length the implementation supports.

If it also depends on very runtime-dependent things like stack sizes, you probably won't be able to find out.

Needless to say, it's best to pass (long) strings as constant references, to avoid any copying.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    I know this, but I'm going to modify this string in the function so I need to have a copy – Ahmed Nov 29 '10 at 13:37
0

It can't be more than max_size() which is implementation dependant.

However best practice would be to pass a reference to it.

int Foo(string& sentence);
graham.reeds
  • 16,230
  • 17
  • 74
  • 137