How strings are stored ?

  • Thread starter Thread starter Navaneeth.K.N
  • Start date Start date
N

Navaneeth.K.N

Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

2 - Is there any way to resize an array size in .NET ? I guess it's not
possible to do that.

Thanks
 
1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

It doesn't create a separate array object - the string data is stored
within the string itself. AFAIK, arrays and strings are the only .NET
types whose size is not known statically.
Note that string literals are interned, so if you have "Hello"
elsewhere that will use the same string reference.
2 - Is there any way to resize an array size in .NET ? I guess it's not
possible to do that.

Nope. You have to create a new array of the right size and copy the
data. IIRC, there are methods in Array which make that easier.

Jon
 
Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

In short no, it's not an array. In reality you do not need to know
that exactly, you can access each element of a string using a array
like notation: myString[1].

For a deeper explanation of where in memory each string is stored take
a look at Skeet article at http://www.yoda.arachsys.com/csharp/strings.html
 
Navaneeth.K.N said:
Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

Most likely, but that underlying array is part of the System.String object
and not really accessible to you, nor is there any guarantee that this is
what the data representation is. What matters is that System.String exposes
an indexer "operator []" that takes a zero-based integer and returns the
corresponding character.
2 - Is there any way to resize an array size in .NET ? I guess it's not
possible to do that.

Not directly, no more so than you could do that with an array in C/C++.
That is, you can allocate a new array with the desired new size, copy the
elements from the old array to the new, and then store the reference to the
new array in whatever variable was referencing the old.

Both the original, object based collections and the new Generic based
collections support what are essentially growable arrays (ArrayList or
List<T>).

In the particular case of strings, see the StringBuilder class.

Regards,
-- TB
 
Hello Thomas,

Thanks for the reply. It's much appreciated.
Most likely, but that underlying array is part of the System.String object
and not really accessible to you,

As you know strings are immutable, so this also will be immutable right ? I
am wondering when we take a character like mystring[index], are we getting a
new copy or just pointing to a location where the real data is kept ?

Thanks
 
Hello John,

Your reply was great and helpful. Thanks.
It doesn't create a separate array object - the string data is stored
within the string itself.

So are you telling the string "hello" will be kept as it is ?

Thanks again
 
So are you telling the string "hello" will be kept as it is ?

What do you mean by "as it is"? The data is stored directly in the
string object - if you examine the heap, you'll find the string
reference is pointing to a bit of data about the length of the string,
and then the character data itself (in UTF-16).

Jon
 
As you know strings are immutable

Well, publicly anyway. Internally they're mutable - that's how
StringBuilder works. It just makes sure that after it's made a string
publicly available, it will never be changed thereafter.
, so this also will be immutable right ? I
am wondering when we take a character like mystring[index], are we getting a
new copy or just pointing to a location where the real data is kept ?

Characters are value types, so there's no pointer/reference involved
here anyway (in terms of the value returned).

Jon
 
Thanks again John,
What do you mean by "as it is"? The data is stored directly in the
string object

Ok - I understood. But when I read your article on strings, you say it keeps
a character array along with the length. But in the previous message, you
said it won't create a separate array object. That made me again in
confusion. Any help will be appreciated.

Thanks
 
Navaneeth.K.N said:
Thanks again John,


Ok - I understood. But when I read your article on strings, you say it keeps
a character array along with the length. But in the previous message, you
said it won't create a separate array object. That made me again in
confusion. Any help will be appreciated.

Well, I say it's essentially as if the string is the character array -
which is accurate. Basically an array is a block of data with a length
and ways of indexing into it - which is all true for strings as well.
The important thing is that the data is "inline" - it's part of the
object. Compare this with Java, where the String type references a
separate char[].
 

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

Back
Top