c# string size limit? length of string limit?

J

Jon Skeet [C# MVP]

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 :)
 
M

Michael C

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?
 
J

Jon Skeet [C# MVP]

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
 
W

Willy Denoyette [MVP]

| > | > > 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top