Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof empty string in C#

Tags:

string

c#

In Java, an empty string is 40 bytes. In Python it's 20 bytes. How big is an empty string object in C#? I cannot do sizeof, and I don't know how else to find out. Thanks.

like image 916
Dervin Thunk Avatar asked Apr 17 '11 01:04

Dervin Thunk


People also ask

What is the size of empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all.

Why sizeof null is 8?

sizeof(NULL) is 8 in C in a 64 bit system. The difference is because NULL in C is a variable of a void pointer type with a value zero. In C++,NULL is a variable with a value zero that gets deduced to an int type.

Is empty string null in C?

The string exists in memory, so it's not a NULL pointer. It's just absent any elements. An empty string has a single element, the null character, '\0' . That's still a character, and the string has a length of zero, but it's not the same as a null string, which has no characters at all.

How big is null?

In all modern character sets, the null character has a code point value of zero. In most encodings, this is translated to a single code unit with a zero value. For instance, in UTF-8 it is a single zero byte. However, in Modified UTF-8 the null character is encoded as two bytes: 0xC0, 0x80.


3 Answers

It's 18 bytes:

16 bytes of memory + 2 bytes per character allocated + 2 bytes for the final null character.

Note that this was written about .Net 1.1.

The m_ArrayLength field was removed in .Net 4.0 (you can see this in the reference source)

like image 167
SLaks Avatar answered Oct 04 '22 20:10

SLaks


The CLR version matters. Prior to .NET 4, a string object had an extra 4-byte field that stored the "capacity", m_arrayLength field. That field is no longer around in .NET 4. It otherwise has the standard object header, 4 bytes for the sync-block, 4 bytes for the method table pointer. Then 4 bytes to store the string length (m_stringLength), followed by 2 bytes each for each character in the string. And a 0 char to make it compatible with native code. Objects are always a multiple of 4 bytes long, minimum 16 bytes.

An empty string is thus 4 + 4 + 4 + 2 = 14 bytes, rounded up to 16 bytes on .NET 4.0. 20 bytes on earlier versions. Given values are for x86. This is all very visible in the debugger, check this answer for hints.

like image 42
Hans Passant Avatar answered Oct 04 '22 22:10

Hans Passant


Jon Skeet recently wrote a whole article on the subject.

On x86, an empty string is 16 bytes, and on x64 it's 32 bytes

like image 34
Thomas Levesque Avatar answered Oct 04 '22 20:10

Thomas Levesque