Feb 20, 2012

[How to] Memory Allocate

If we want to dynamic allocate N bytes of memory.
And then, reallocate it to X bytes. After all, release it.


In the case of C/C++:
char* bp = malloc(sizeof(char) * N);
bp = realloc(bp, sizeof(char) * X);
free(bp);



C#:
using System.Runtime.InteropServices;

IntPtr
bp = Marshal.AllocHGlobal(sizeof(byte) * N);
bp = Marshal.ReAllocHGlobal(bp, (IntPtr)(sizeof(byte) * N));
Marshal.FreeHGlobal(bp);


Notice the casting (IntPtr).

No comments:

Post a Comment