0

I didn't think it fair to post a comment on Fredrik Mörk's answer in this 2 year old post, so I thought I'd just ask it as a new question instead...

NB: This is not a critiscm of the answer in any way, I'm simply trying to understand this all before delving into memory management / the marshal class.

In that answer, the function GetByteArray allocates memory to each object within the given array, within a loop.

Would the GetByteArray function on the aforementioned post have benefited at all from allocating memory for the total size of the provided array:

Dim arrayBufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(<arrayElement>) * <array>.Count)

I just wonder if allocating the memory, as shown in the answer, causes any kind of fragmentation? Assuming there may be fragmentation, would there be much of an impact to be concerned with? Would allocating the memory in the way I've shown force you to call IntPtr.ToInt## to obtain pointer offsets from the overall allocation pointer, and therefore force you to check the underlying architecture to ensure the correct method is used*1 or is there a better way? (ToInt32/ToInt64 depending on x86/64?)

*1 I read elsewhere that calling the wrong IntPtr.ToInt## will cause overflow exceptions. What I mean by that statement is would I use:

Dim anOffsetPtr As New IntPtr(arrayBufferPtr.ToInt## + (loopIndex * <arrayElementSize>))

I've read through a few articles on the VB.Net Marshal class and memory allocation; listed below, but if you know fo any other good articles I'm all ears!

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx

http://www.dotnetbips.com/articles/44bad06d-3662-41d3-b712-b45546cd8fa8.aspx

My favourite so far: http://www.codeproject.com/KB/vb/Marshal.aspx

Community
  • 1
  • 1
Smudge202
  • 4,689
  • 2
  • 26
  • 44

1 Answers1

2

It is possible to allocate unmanaged memory for the whole array, and then copy every array element with SizeOf(arrayElement)*loopIndex offset. It is better to use appropriate ToInt32/ToInt64 method, according to the current platform, like:

Dim anOffsetPtr 
if arrayBufferPtr.Size = 4 then
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt32() + (loopIndex * arrayElementSize)) 
else
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt64() + (loopIndex * arrayElementSize)) 
endif
Alex F
  • 42,307
  • 41
  • 144
  • 212
  • Ohhh, of course. I hadn't thought to check the size of the IntPtr and hadn't actually come across code showing that, was already contemplating the best way to determine platform. +1 (and probably answer in due course). I don't suppose you know of any good articles as well? - there must be some more out there that, for example, mention the IntPtr.Size trick...? – Smudge202 Jun 14 '11 at 14:23
  • Note; C# examples are probably fine too - I figure its more your preference by the double equals above. Finding workarounds for C# code that doesn't directly translate to VB is good fun too. ^^ – Smudge202 Jun 14 '11 at 14:25
  • @Smudge202 - I use only Marshal class reference, it is absolutely clear for everybody having experience in both managed and unmanaged programming. Sorry for syntax errors, VB is not my mother language :) – Alex F Jun 14 '11 at 14:31