473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

interop memory allocation

I would like to ask a question that is obvious to all people porting
applications from the "traditional" C\VB6 interop scheme choosing C# vs
VB.NET.

We have a math library in C which ubiquitously takes a float* which
represents an array of floats. If the memory is allocated by the client,
in C# or VB.NET, I assume it must be "fixed" so that the GC does not
move the pointer during the C algorithm. Given that we are choosing
between C# and VB.NET, how is this done in either language? Is it even
possible in VB.NET. In terms of C#, I have heard that using the keyword
"fixed" is much faster then Marshal.AllocHGlobal. Is there a way to
offload this step into the C dll.

The question centers around these points:

1) Given a float* in the signature of a C dll is it required to fix that
pointer for the C function to be reliable when the memory is allocated
in managed code?

2) If (1) is true, is Marshal.AllocHGlobal the only way to do that in VB.NET

3) If (2) is there a way to offload that step to the C dll.

4) C# seems to lean more toward operating with interop easier, what
about this particular scenario?

I assumed that this question's answer would be easy to find using
google, but have not found this the case.

Thank you.
Jan 16 '08 #1
5 3252
1) Given a float* in the signature of a C dll is it required to fix that
pointer for the C function to be reliable when the memory is allocated
in managed code?
Yes. You would probably get away with it a lot of the time, but to be
robust the buffer should be pinned (fixed) for the duration of the
call [but not forever].
2) If (1) is true, is Marshal.AllocHGlobal the only way to do that in VB.NET
Possibly; VB forums may know more...
3) If (2) is there a way to offload that step to the C dll.
Almost certainly not. And theoreticaly, even if there was, it could
conceivably be too late? (unlikely, though)
4) C# seems to lean more toward operating with interop easier, what
about this particular scenario?
Well, in C# you can use "fixed" etc, which seems fairly easy (although
it requires unsafe mode). However, VB has some other advantages re
late binding (Option Explicit Off), which can be useful in a few
scenarios, and is harder to do in C# - so you need to know what you
need to do. If you are working with pointers, then probably C#;
unpredictable/expando objects (kinda duck typing)? possibly VB. You
can mix and match, but you need an assembly per language.

Marc
Jan 16 '08 #2
"John" <no@spam.comwrote in message
news:em**************@TK2MSFTNGP04.phx.gbl...
>I would like to ask a question that is obvious to all people porting
applications from the "traditional" C\VB6 interop scheme choosing C# vs
VB.NET.

We have a math library in C which ubiquitously takes a float* which
represents an array of floats. If the memory is allocated by the client,
in C# or VB.NET, I assume it must be "fixed" so that the GC does not move
the pointer during the C algorithm. Given that we are choosing between C#
and VB.NET, how is this done in either language? Is it even possible in
VB.NET. In terms of C#, I have heard that using the keyword "fixed" is
much faster then Marshal.AllocHGlobal. Is there a way to offload this step
into the C dll.

The question centers around these points:

1) Given a float* in the signature of a C dll is it required to fix that
pointer for the C function to be reliable when the memory is allocated in
managed code?
The array of floats will be pinned by the interop (PInvoke) layer for the
duration of the call, there is no need for you to pin explicitely.
2) If (1) is true, is Marshal.AllocHGlobal the only way to do that in
VB.NET
No, it's not, VB.NET uses the same PInvoke layer as all other managed
languages.
3) If (2) is there a way to offload that step to the C dll.

4) C# seems to lean more toward operating with interop easier, what about
this particular scenario?
No, no really , C# supports has some limited pointer support through the use
"unsafe" constructs, but there is seldom a need for this in interop
scenarios.
Willy.
Jan 16 '08 #3
The array of floats will be pinned by the interop (PInvoke) layer for the
duration of the call, there is no need for you to pin explicitely.
Sorry for adding confusion, then; my mistake.

Marc
Jan 16 '08 #4
What happens in the case of the Marshal.AllocHGlobal? Is the compiler or the
run-time smart enough to optimize it away or ignore the call or is that just
unnecessary overhead - performance implications?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
"John" <no@spam.comwrote in message
news:em**************@TK2MSFTNGP04.phx.gbl...
>>I would like to ask a question that is obvious to all people porting
applications from the "traditional" C\VB6 interop scheme choosing C# vs
VB.NET.

We have a math library in C which ubiquitously takes a float* which
represents an array of floats. If the memory is allocated by the client,
in C# or VB.NET, I assume it must be "fixed" so that the GC does not move
the pointer during the C algorithm. Given that we are choosing between C#
and VB.NET, how is this done in either language? Is it even possible in
VB.NET. In terms of C#, I have heard that using the keyword "fixed" is
much faster then Marshal.AllocHGlobal. Is there a way to offload this
step into the C dll.

The question centers around these points:

1) Given a float* in the signature of a C dll is it required to fix that
pointer for the C function to be reliable when the memory is allocated in
managed code?
The array of floats will be pinned by the interop (PInvoke) layer for the
duration of the call, there is no need for you to pin explicitely.
>2) If (1) is true, is Marshal.AllocHGlobal the only way to do that in
VB.NET

No, it's not, VB.NET uses the same PInvoke layer as all other managed
languages.
>3) If (2) is there a way to offload that step to the C dll.

4) C# seems to lean more toward operating with interop easier, what about
this particular scenario?

No, no really , C# supports has some limited pointer support through the
use "unsafe" constructs, but there is seldom a need for this in interop
scenarios.
Willy.

Jan 17 '08 #5
"Alvin Bruney [ASP.NET MVP]" <www.lulu.com/owcwrote in message
news:7D**********************************@microsof t.com...
What happens in the case of the Marshal.AllocHGlobal? Is the compiler or
the run-time smart enough to optimize it away or ignore the call or is
that just unnecessary overhead - performance implications?

--

Regards,
Alvin Bruney [MVP ASP.NET]

Not sure what you mean ......

If you call a C function passing a float array as argument, you are relying
on PInvoke to marshal the managed array to native code.

[DllImport("gffg")]
extern static void Foo(float[] myFloats);

float[] fa = new foat[] {2.1, 6.0};
Foo(fa);
In above, CLR interop (PInvoke layer) will pin the array referenced by fa ,
take the address of the array and pass this address to the callee, when the
call returns PInvoke un-pins the fa object. The problem with this, is that
the object remains pinned for the duration of the call, which can negatively
impact the GC's performance.
Note that this is subject to JIT implementation details, for instance, in an
attempt to reduce the impact of pinning, the JIT 64 will pin the float[],
copy it's contents to an internal buffer, un-pin the float array before
passing the address of the internal buffer to unmanaged.

When using Marshal.AllocHGlobal, you are obviously marshaling the float
array yourself, so you must pass the pointer to the unmanaged buffer.

[DllImport("gffg")]
extern static void Foo(IntPtr myFloats);

IntPtr addressOfUnmanagedHeapBuffer =
Marshal.AllocHGlobal(sizeofFloatArray);
// copy floats to unmanaged addressOfUnmanagedHeapBuffer.
....
Foo(addressOfUnmanagedHeapBuffer );

Here you are marshaling the float[] to unmanaged and you pass the address of
the unmanaged array of floats to unmanaged. Obviously there is no need to
pin the float[] here.
Willy.
Jan 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.