Marshal.AllocHGlobal

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following question

The Marshal class contains a function to allocate a block of memory
1. Marshal.AllocHGlobal ( int cb
2. Marshal.AllocHGlobal ( IntPtr cb

The first version I can understand, because the integer indicates the size of the memory block that is required
But the second version I do not understand. If I want a memoryblock of 20 bytes could I use

IntPtr p = Marshal.AllocHGlobal ( new IntPtr ( 20 ) )

Another reason for asking is the function Marshal.ReAllocHGlobal. This function only accepts an 'IntPtr' as a parameter

Can anyone tell me how to use the IntPtr parameter version of the function

Thanks
Stefan.
 
I could not find any hints in the MSDN library, but I did manage to test both versions of the functio
using the Win32 function 'GlobalSize'

And indeed the functioncalls
IntPtr p = Marshal.AllocHGlobal ( 20 )
an
IntPtr p = Marshal.AllocHGlobal ( new IntPtr ( 20 ) )

both allocate a memory block of 20 bytes.
 
Stefan,
Can anyone tell me how to use the IntPtr parameter version of the function?

You're using it right. The IntPtr overload could be useful on a 64-bit
implementation if you want to allocate more than 2GB of memory.



Mattias
 
Back
Top