473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4385
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William Djaja Tjokroaminata | last post by:
Hi, As I am interfacing my code in C++ with a scripting language which is written in C, I need to store pointers to objects in a class hierarchy: ParentClass *obj1; obj1 = new ParentClass...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
10
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
2
by: hykim | last post by:
I want to call a unmanaged dll's function returning some STRUCT's pointer. the next is definition of a STRUCT. ----------------------------------------------------------------------- typedef...
3
by: Piecu | last post by:
Hello. I have some class written in C++ and I'm trying to use it in C#. So I've created new Managed C++ Library in Visual Studio .NET and copied that code there. But one method takes one parameter...
4
by: Abra | last post by:
I have an application where I need to send a inter-process message (a data stream) that contains among other the address of a function (member of a class). For that, I need to serialize it, so I...
4
by: eking | last post by:
//Thanks for any help,thank you!лл¡£ public override void FloodFill(Bitmap bmp, Point pt) { int ctr=timeGetTime(); //Debug.WriteLine("*******Flood Fill******"); //get the color's int...
9
by: Russell Mangel | last post by:
Can someone show me how to speed this up? 1. Whats the fastest way for Unsafe C#? 2. What the fastest way for Safe C#? public static Int64 ToInt64(Int32 low, Int32 high) { Byte lowBytes =...
30
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.