c# string size limit? length of string limit?

  • Thread starter Thread starter Daniel
  • Start date Start date
Michael C said:
Limit of available contiguous free memory block or 2^31 chrs.

Not quite - I believe the top bits of the length are taken for extra
flags, and there's always an extra null terminator, so it's 2^30-1
chars. Not that I expect it'll make a difference to anyone :)
 
Jon Skeet said:
Not quite - I believe the top bits of the length are taken for extra
flags, and there's always an extra null terminator, so it's 2^30-1
chars. Not that I expect it'll make a difference to anyone :)

Does C# use a null chr?
 
Michael said:
Does C# use a null chr?

C# itself doesn't need it - nor does most managed code, I believe.
(Unsafe managed code may use it for convenience, if it's sure there are
no null chars genuinely in the string.)

However, for interop reasons it's really handy to have the null char at
the end anyway - a pointer can be passed to any unmanaged code which
expects a null-terminated UTF-16-encoded string, without copying
anything.

Jon
 
| > | > > c# string size limit? length of string limit?
| >
| > Limit of available contiguous free memory block or 2^31 chrs.
|
| Not quite - I believe the top bits of the length are taken for extra
| flags, and there's always an extra null terminator, so it's 2^30-1
| chars. Not that I expect it'll make a difference to anyone :)
|
| --
| Jon Skeet - <[email protected]>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too

The CLR (currently) limits the size of an object to 2^31 bytes. That means
that the string buffer size is limited to (2^31 - size of the object header)
bytes, or 2^31-16 bytes or 2^30-8 = 1073741816 characters in .NET.

There is only a trailing null terminator if the buffer is large enough, in
the maximum case there isn't a null terminator.
Note that such string cannot be created on Windows 32 bit.

Willy.
 
Back
Top