How strings are stored ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

    How strings are stored ?

    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
  • Jon Skeet [C# MVP]

    #2
    Re: How strings are stored ?

    On Jun 4, 4:08 pm, Navaneeth.K.N
    <Navaneet...@di scussions.micro soft.comwrote:
    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

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: How strings are stored ?

      On Jun 4, 11:08 am, Navaneeth.K.N
      <Navaneet...@di scussions.micro soft.comwrote:
      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
      2 - Is there any way to resize an array size in .NET ? I guess it's not
      possible to do that.
      NO
      Thanks

      Comment

      • =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?=

        #4
        RE: How strings are stored ?

        "Navaneeth. K.N" wrote:
        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

        Comment

        • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

          #5
          RE: How strings are stored ?

          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

          Comment

          • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

            #6
            Re: How strings are stored ?

            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

            Comment

            • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

              #7
              Re: How strings are stored ?

              Hi Ignacio,

              Thanks for the reply. Article is also great. Thanks


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: How strings are stored ?

                On Jun 4, 5:45 pm, Navaneeth.K.N
                <Navaneet...@di scussions.micro soft.comwrote:
                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 ?
                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

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: How strings are stored ?

                  On Jun 4, 5:41 pm, Navaneeth.K.N
                  <Navaneet...@di scussions.micro soft.comwrote:
                  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
                  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

                  Comment

                  • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

                    #10
                    Re: How strings are stored ?

                    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

                    Comment

                    • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

                      #11
                      Re: How strings are stored ?

                      Thanks Jon


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: How strings are stored ?

                        Navaneeth.K.N <NavaneethKN@di scussions.micro soft.comwrote:
                        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.
                        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[].

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Web site: http://www.pobox.com/~skeet
                        Blog: http://www.msmvps.com/jon.skeet
                        C# in Depth: http://csharpindepth.com

                        Comment

                        Working...