Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing a pointer from memory in c#

Tags:

c#

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?

like image 712
Sara S. Avatar asked Jun 06 '11 12:06

Sara S.


People also ask

How do I free up my pointer memory?

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.

What does freeing memory do C?

The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.

When should I use free () in C?

“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.

Does freeing a pointer make it NULL?

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.


1 Answers

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.

like image 181
Konrad Rudolph Avatar answered Oct 12 '22 11:10

Konrad Rudolph