-
July 12th, 2009, 10:23 AM
#1
string in Stack or Heap ?
I understand string is reference type.
string s;
Is "s" pointing a stack or heap object ?
-
July 12th, 2009, 11:09 AM
#2
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.
-
July 12th, 2009, 11:20 AM
#3
Re: string in Stack or Heap ?
Originally Posted by Mutant_Fruit
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.
-
July 12th, 2009, 11:52 AM
#4
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.
-
July 12th, 2009, 11:06 PM
#5
Re: string in Stack or Heap ?
Originally Posted by darwen
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.
-
July 13th, 2009, 12:33 AM
#6
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.
-
July 13th, 2009, 01:58 AM
#7
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)
{
...
}
-
July 13th, 2009, 02:30 AM
#8
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.
-
July 13th, 2009, 05:47 AM
#9
Re: string in Stack or Heap ?
Originally Posted by dannystommen
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.
-
July 13th, 2009, 12:23 PM
#10
Re: string in Stack or Heap ?
Originally Posted by dannystommen
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.)
-
July 29th, 2009, 02:03 AM
#11
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 ?
-
July 29th, 2009, 03:06 AM
#12
Re: string in Stack or Heap ?
They are references, so the reference is copied to s1;
-
July 29th, 2009, 03:54 AM
#13
Re: string in Stack or Heap ?
Thank you BigEd781.
May I know what makes it immutable.
-
July 29th, 2009, 05:30 AM
#14
Re: string in Stack or Heap ?
may be the keyword "sealed" which dont allow you to inherit the string class further ?
-
July 29th, 2009, 12:22 PM
#15
Re: string in Stack or Heap ?
Originally Posted by vcdebugger
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|