Releasing a pointer from memory in C #

I am dealing with a pointer to C # using fixed{} phrases.

I put my code in brackets of a fixed statement and want to know if the Garbage collection will handle releasing a pointer after a fixed statement

 fixed{int * p=&x} { // i work with x. } 

if not, how can I free him?

+6
source share
2 answers

The pointer points to the managed object ( x ), so there is nothing to worry about: the pointer does not need to be freed (or rather, it goes out of scope at the end of the fixed block) and the pointer x controlled by GC.

+5
source

Some similar code in C will be more clear:

 { int x; // allocates space for x { int *p=&x; // does not allocate for something p points to (only for p) // ... } // leaves inner scope, p vanishes, *p is not deallocated // ... } // leaves outer scope, x is deallocated 

C # code does the same.

0
source

Source: https://habr.com/ru/post/889864/


All Articles