In dotNET we could write unmanaged code, where can I allocate memory dynamically (by keywords: stackalloc, new), use pointers etc.
It's possible to free memory (for example by something like delete in C++)?
Asked
Active
Viewed 812 times
0

Jacek
- 11,661
- 23
- 69
- 123
-
5Please show an example. This is kind of vague at the moment. – Yuck Aug 01 '12 at 13:33
-
http://stackoverflow.com/questions/2648560/allocating-unmanaged-memory-in-c-sharp the mashler will free memory when it leaves scope. – Travis Aug 01 '12 at 13:36
2 Answers
7
It depends how you allocate memory.
For example, if you allocate memory with AllocHGlobal :
double* vertices = (double*)Marshal.AllocHGlobal(
3 * count * Marshal.SizeOf(typeof(double)));
You allocate the array of doubles of a given size.
To free that memory after you have to call FreeHGlobal
Marshal.FreeHGlobal((IntPtr)vertices);
There are also other functions for allocating COM task memory, like
AllocCoTaskMem and relative FreeCoTaskMem

Tigran
- 61,654
- 8
- 86
- 123
-
4Five answers in five minutes and only one of them understand the question? :) +1 for you. – Anders Arpi Aug 01 '12 at 13:40
-1
try with this code in order to free
Marshal.FreeHGlobal((IntPtr)vertices);

Aghilas Yakoub
- 28,516
- 5
- 46
- 51
-
6
-
-
I know Henk but i tell that's possible, Thank's for your help that's nice – Aghilas Yakoub Aug 01 '12 at 13:43
-
2Don't reuse your "answer" to write a completely different answer. Instead remove your old answer and write a new one if you change your mind. – Zano Aug 01 '12 at 13:45
-
Zano I'don't know that's prohibited to correct answer, i'am sorry – Aghilas Yakoub Aug 01 '12 at 13:46
-
Don't be sorry :-) It's not prohibited to correct an answer, but you changed the answer completely, which invalidates all the comments. In that case it's more practical to write a new question. – Zano Aug 01 '12 at 13:48