tags:

views:

170

answers:

4

Is there a maximum number of characters allowed in a string? If so, what is the limit on the number of characters?

A: 

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];

Cameron
-1: Why would there be such a tiny maximum?
John Saunders
I remember trying to declare a character array larger than this once (in VC++ 6, so this was a few years ago) and I remember the limit being 32768 elements in the array.
Cameron
If you were allocating an array using the syntax "char myArray[32768]", you were allocating it on the stack. Your string was limited by the stack size of the platform you were using. However, putting such large structures on the stack is a bad idea. If you'd put "char* myArray = new char[32768]" you'd have found that your array could be as large as you wanted it to be, within the confines of the size of an integer on your platform.
Ant
Yes, you're right; I assumed (wrongly, now that I look back at the question) that the OP was asking about the maximum when declaring an array on the stack.
Cameron
Still, that isn't a real, fixed limit, it depends at least from the size of the stack.
Matteo Italia
And in any case it's implementation-dependent. Venus might not be using VC++ 6. Probably you hit a limit on the maximum *stack frame* size, btw, not the maximum *stack* size as Matteo says.
Steve Jessop
+3  A: 

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.

Skurmedel
How it depends on the size of the buffer is the question. std::string doesn't store the actual string on the stack when the string is large.
Hans Passant
nobugz: I don't quite see what you mean, are your first sentence about std::string or a regular buffer?
Skurmedel
@Skurmedel The storage for std::string will never be limited by the size of the stack.
anon
@Neil Butterworth, @nobugz: Thanks, fixing it.
Skurmedel
+6  A: 

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

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

Goran Rakic
Interesting... I guess max_size is like the maximum of the integer it uses for indexing or something?
Skurmedel
max_size returns size_t which is unsigned integer, but it can be less depending on implementation I guess. There is a SO question about length_error exception http://stackoverflow.com/questions/1183700/what-is-the-meaning-of-this-c-error-stdlength-error
Goran Rakic
Note that `max_size` is generic to any STL container. Also on most computers you are limited not by the integer limit but by the maximum size of the chunk of memory that can be allocated at once (windows does not support more than 2GB per process, if my memory serves me right) and the memory you've left on the machine.
Matthieu M.
On windows 32 bit it will be 2GB but on Windows 64 I think that this limit is bigger.
lollinus
+1  A: 

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.

Thomas Matthews