469,621 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

why can't create a string pointer in unsafe code?

Hi, I want to ask some basic question about the string type. When compiling
the unsafe code:
---------
string a = "wori";
string* b = &a;
---------
it pops up errors saying both 'string*b' and '&a' are illegal because string
is a managed type, while in the same case the 'int' type is available.
Why can't i get the point or the address of a string type? Is that because
string is immutable and every time you change it a new address will be given?
Thx in advanced
Nov 12 '08 #1
4 4225
"fairyvoice" <fa********@discussions.microsoft.comwrote in message
news:BB**********************************@microsof t.com...
Hi, I want to ask some basic question about the string type. When
compiling
the unsafe code:
---------
string a = "wori";
string* b = &a;
---------
it pops up errors saying both 'string*b' and '&a' are illegal because
string
is a managed type, while in the same case the 'int' type is available.
Why can't i get the point or the address of a string type? Is that because
string is immutable and every time you change it a new address will be
given?
Thx in advanced
I believe it is because managed types can be moved in memory by the garbage
collector so a pointer is not always going to remain valid. What are you
trying to do?

Michael
Nov 12 '08 #2
Thank you Michael and Rossum.
And Michael, you say it is because of the garbage collection, then most
type in .net are managed type and garbage collection will deal with all of
them, then why only string is illegal to get a pointer?

and Rossum, i know i can change the value in this way now, but i still want
to know why i can not get a pointer to the string type, thanks.
"fairyvoice" wrote:
Hi, I want to ask some basic question about the string type. When compiling
the unsafe code:
---------
string a = "wori";
string* b = &a;
---------
it pops up errors saying both 'string*b' and '&a' are illegal because string
is a managed type, while in the same case the 'int' type is available.
Why can't i get the point or the address of a string type? Is that because
string is immutable and every time you change it a new address will be given?
Thx in advanced
Nov 13 '08 #3
fairyvoice <fa********@discussions.microsoft.comwrote:
>
And Michael, you say it is because of the garbage collection, then most
type in .net are managed type and garbage collection will deal with all of
them, then why only string is illegal to get a pointer?
The key is that there are two different kinds of "types" in C#: value
types, and reference types. The types that hold ordinals (byte, char,
bool, short, int, long, etc.) and the floating types (float, double) are
value types. You can get a pointer to those. Object and string are
reference types. You can't get a pointer to those.
>and Rossum, i know i can change the value in this way now, but i still want
to know why i can not get a pointer to the string type, thanks.
Basically, because that's just not how C# works. C# is not C++; you need
to think about the problem differently.

I assume you are trying to use the pointer for efficiency, but it's a false
economy. Remember that copying a string variable doesn't actually copy the
string:

string s = "Testing";
string t = s;

There's only one string there, with two references to it.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 13 '08 #4
On Wed, 12 Nov 2008 18:06:07 -0800, fairyvoice
<fa********@discussions.microsoft.comwrote:
>and Rossum, i know i can change the value in this way now, but i still want
to know why i can not get a pointer to the string type, thanks.
Look in the C# documentation about 'fixed' and 'moveable' variables.
Put simply, value types are fixed and reference types are moveable.
Fixed variables always stay in the same place in memory and are not
moved about by the garbage collector. Hence it is easy to have a
pointer to them. Moveable types can be moved around by the garbage
collector so any pointer to their old position will no longer be valid
after they have been moved. This is a recipe for program crashes and
all sorts of bad stuff.

Using the 'fixed' statement tells the garbage collector not to move
that variable during the time that the pointer is in scope, and so the
pointer is always pointing to the correct location.

Strings are moveable and so require use of the 'fixed' statement if
you want to get a valid pointer to them.

rossum

Nov 13 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William Djaja Tjokroaminata | last post: by
7 posts views Thread by Alan Bashy | last post: by
3 posts views Thread by Piecu | last post: by
30 posts views Thread by nano2k | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.