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

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

    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
  • Michael C

    #2
    Re: why can't create a string pointer in unsafe code?

    "fairyvoice " <fairyvoice@dis cussions.micros oft.comwrote in message
    news:BB40ED81-65A0-4E21-A659-997AE54C6F86@mi crosoft.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


    Comment

    • =?Utf-8?B?ZmFpcnl2b2ljZQ==?=

      #3
      RE: why can't create a string pointer in unsafe code?

      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

      Comment

      • Tim Roberts

        #4
        Re: why can't create a string pointer in unsafe code?

        fairyvoice <fairyvoice@dis cussions.micros oft.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, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • rossum

          #5
          Re: why can't create a string pointer in unsafe code?

          On Wed, 12 Nov 2008 18:06:07 -0800, fairyvoice
          <fairyvoice@dis cussions.micros oft.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

          Comment

          Working...