Unsafe code
-
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:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
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:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
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 )
-
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 )
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:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
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 )
Will GC clean up unsafe code too?
Regards, Arun Kumar.A
-
Will GC clean up unsafe code too?
Regards, Arun Kumar.A
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 )
-
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 )
Thank U very much for clearing my doubt.
Regards, Arun Kumar.A
-
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:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
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]
-
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]
*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 )
-
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]
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;