I'm dealing with pointer in C# using fixed{}
phrases.
I placed my code inside the brackets of the fixed statement and want to know if the Garbage collection will handle the pointer freeing after the fixed statement
fixed{int * p=&x}
{
// i work with x.
}
if not how can I free it?
Deallocation Of Allocated Memory With freeThe function free takes a pointer as parameter and deallocates the memory region pointed to by that pointer. The memory region passed to free must be previously allocated with calloc , malloc or realloc . If the pointer is NULL , no action is taken.
The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.
“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.
free() is a library function, which varies as one changes the platform, so you should not expect that after passing pointer to this function and after freeing memory, this pointer will be set to NULL.
Your pointer points to a 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 pointee x
itself is managed by the GC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With