Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unsafe code

Unsafe code

Scheduled Pinned Locked Moved C#
question
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xibeifeijian
    wrote on last edited by
    #1

    Just look this test method: public unsafe void Test() { int x=23; int* px=&x; *px+=1; Console.Write(*px); } I want to know when will x and px distory? :doh: thanks!

    :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

    C L 2 Replies Last reply
    0
    • X xibeifeijian

      Just look this test method: public unsafe void Test() { int x=23; int* px=&x; *px+=1; Console.Write(*px); } I want to know when will x and px distory? :doh: thanks!

      :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      destroy ? On the next GC cycle, as they exist for far too short a time to be marked to survive it.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      X A 2 Replies Last reply
      0
      • C Christian Graus

        destroy ? On the next GC cycle, as they exist for far too short a time to be marked to survive it.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        X Offline
        X Offline
        xibeifeijian
        wrote on last edited by
        #3

        Thanks! You are right,its destory:-O You say ------------- On the next GC cycle, ------------- but there are unmanaged variables,does GC collect unmanged variables?

        :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

        1 Reply Last reply
        0
        • C Christian Graus

          destroy ? On the next GC cycle, as they exist for far too short a time to be marked to survive it.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          A Offline
          A Offline
          Arun Immanuel
          wrote on last edited by
          #4

          Will GC clean up unsafe code too?

          Regards, Arun Kumar.A

          C 1 Reply Last reply
          0
          • A Arun Immanuel

            Will GC clean up unsafe code too?

            Regards, Arun Kumar.A

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            GC collects all allocated memory. Unsafe doesn't really come into it.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            A 1 Reply Last reply
            0
            • C Christian Graus

              GC collects all allocated memory. Unsafe doesn't really come into it.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              Thank U very much for clearing my doubt.

              Regards, Arun Kumar.A

              1 Reply Last reply
              0
              • X xibeifeijian

                Just look this test method: public unsafe void Test() { int x=23; int* px=&x; *px+=1; Console.Write(*px); } I want to know when will x and px distory? :doh: thanks!

                :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, these value types reside on the stack, they disappear when the method returns. There is no new, no object, no collection involved. :)

                Luc Pattyn [My Articles]

                C G 2 Replies Last reply
                0
                • L Luc Pattyn

                  Hi, these value types reside on the stack, they disappear when the method returns. There is no new, no object, no collection involved. :)

                  Luc Pattyn [My Articles]

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  *sigh* I thought so, but I wasn't sure. Certainly, I knew that unsafe wasn't going to change anything.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, these value types reside on the stack, they disappear when the method returns. There is no new, no object, no collection involved. :)

                    Luc Pattyn [My Articles]

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    It should also be mentioned that the code isn't guaranteed to work if the variables hadn't been allocated on the stack. If it uses a real object that is allocated on the heap:

                    public unsafe void Test() {
                    string x = 42.ToString();
                    string* px = &x; // Dangerous!
                    *px += 1;
                    Console.Write(*px);
                    }

                    The variable x is still allocated on the stack, but it is a reference to a string object that is allocated on the heap. If you copy the reference to a pointer, you will get the current location of the string in memory, but as the garbage collector can move objects in the heap, there is no guarantee that the string will stay in that location. It can be moved to another location at any moment, and the garbage collector will update the reference x to point to the new location, but it will not update the pointer px.

                    --- single minded; short sighted; long gone;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups