string in Stack or Heap ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: string in Stack or Heap ?

  1. #1
    Join Date
    Mar 2007
    Posts
    155

    string in Stack or Heap ?

    I understand string is reference type.
    string s;
    Is "s" pointing a stack or heap object ?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: string in Stack or Heap ?

    All reference types live on the heap. Only value types live on the stack, though value type will live on the heap if you box it (cast it to object).
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Mar 2007
    Posts
    155

    Re: string in Stack or Heap ?

    Quote Originally Posted by Mutant_Fruit View Post
    All reference types live on the heap. Only value types live on the stack, though value type will live on the heap if you box it (cast it to object).
    Thanks Mutant_Fruit

    But we won't use new to create this string object.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: string in Stack or Heap ?

    You will be implicitly :

    Code:
    string s = "hello";
    compiles to (in IL)

    Code:
    .locals init (
        [0] string s)
    
    L_0000: nop 
    L_0001: ldstr "hello"
    L_0006: stloc.0
    If you look in the docs it says that

    ldstr
    Pushes a new object reference to a string literal stored in the metadata.
    So "hello" is stored in the metadata of the assembly, but the ldstr instruction creates a reference to a string object (i.e. on the heap) from this metadata.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Mar 2007
    Posts
    155

    Re: string in Stack or Heap ?

    Quote Originally Posted by darwen View Post
    You will be implicitly :

    Code:
    string s = "hello";
    compiles to (in IL)

    Code:
    .locals init (
        [0] string s)
    
    L_0000: nop 
    L_0001: ldstr "hello"
    L_0006: stloc.0
    If you look in the docs it says that



    So "hello" is stored in the metadata of the assembly, but the ldstr instruction creates a reference to a string object (i.e. on the heap) from this metadata.

    Darwen.
    Thanks Darwen.

  6. #6
    Join Date
    Aug 2008
    Posts
    18

    Re: string in Stack or Heap ?

    I have the question in same line
    then why reference of string is not passed to any of my function
    ie

    code:
    public static void swap(string s1, string s2)
    {
    // If Pass strings from main to this function i will get local copies of s1 and s2
    //instead of their references
    }

    code:
    Any help is appreciated
    Thanks in advance.

  7. #7
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: string in Stack or Heap ?

    if you want to pass a reference as paramater, you need to add the 'ref' modifier.

    Code:
    public static void swap(ref string s1, ref string s2)
    {
       ...
    }

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: string in Stack or Heap ?

    // If Pass strings from main to this function i will get local copies of s1 and s2
    //instead of their references
    How do you know you're getting local copies ?

    Actually you're not getting local copies, you're getting references. The strings are passed by reference. Because System.String is a class which are always passed by reference in .NET - that's why they are called 'reference types'.

    But System.String is peculiar in that it is an immutable class i.e. it exposes no methods which allow you to change the contents of the class once it's been created. Hence it behaves like a value type, but in fact is a reference type.

    That's probably why you think you're getting a local copy - you aren't, you are just getting references to instances of string which you can't change.

    Darwen.
    Last edited by darwen; July 13th, 2009 at 02:32 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  9. #9
    Join Date
    May 2007
    Posts
    1,546

    Re: string in Stack or Heap ?

    Quote Originally Posted by dannystommen View Post
    if you want to pass a reference as paramater, you need to add the 'ref' modifier.

    Code:
    public static void swap(ref string s1, ref string s2)
    {
       ...
    }
    Strings are reference types so you don't 'copy' them by passing them to anther function. The ref parameters are unnecessary for this case.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: string in Stack or Heap ?

    Quote Originally Posted by dannystommen View Post
    if you want to pass a reference as paramater, you need to add the 'ref' modifier.

    Code:
    public static void swap(ref string s1, ref string s2)
    {
       ...
    }
    Well, you need to use ref if you want the original reference instead of a copy. Reference types are always passed by reference, but that a copy of that reference is passed in by default (i.e., when you don't use the 'ref' keyword.)

  11. #11
    Join Date
    Mar 2007
    Posts
    155

    Re: string in Stack or Heap ?

    Code:
                string s1 = "first string";
                string s2 = "second string";
    
                s1 = s2;
    So, what happens in s1 = s2 ?

    - Is that the address of s2 copied to s1 (Shallow copy)?
    - Copy the entire contents (deep copy) ?

    - or What ?

  12. #12
    Join Date
    Jun 2008
    Posts
    2,477

    Re: string in Stack or Heap ?

    They are references, so the reference is copied to s1;

  13. #13
    Join Date
    Mar 2007
    Posts
    155

    Re: string in Stack or Heap ?

    Thank you BigEd781.

    May I know what makes it immutable.

  14. #14
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: string in Stack or Heap ?

    may be the keyword "sealed" which dont allow you to inherit the string class further ?

  15. #15
    Join Date
    Jun 2008
    Posts
    2,477

    Re: string in Stack or Heap ?

    Quote Originally Posted by vcdebugger View Post
    may be the keyword "sealed" which dont allow you to inherit the string class further ?
    No, it is immutable simply because the class gives you no way to change its member data. Look at this class:

    Code:
    class Foo
    {
        public readonly int Id;
        public readonly string Name;
    
        public Foo( int id, string name )
        {
            Id = id;
            Name = name;
        }
    }
    That class is immutable. You pass some data into the constructor, the data is set, and now there is no way that it can be changed by the outside world.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center