Hi Ray,
So far as my research, I think copying data is the only way of handling
this issue. I think you can do some carefully copying, so that the
performance is better.
Thanks for your understanding.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "Ray Z" <
[email protected]>
| References: <
[email protected]>
<
[email protected]>
<#
[email protected]>
<
[email protected]>
<#
[email protected]>
<
[email protected]>
| Subject: Re: covert between byte[] and int []
| Date: Tue, 4 Nov 2003 10:23:31 -0500
| Lines: 197
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196610
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks, now I get a clear idea from you. I know the problem Teb point out.
| He is right. And I know there is no way to conver a unmanaged pointer to a
| managed array. If the COPY is the only way, I get no choice. So I need
| carefully design my archtechture to avoid copy as best as I can.
|
| When you find any solution, please let me know. I will keep watching this
| topic.
|
| Thanks for all guy.
|
| | >
| > Hi Ray,
| >
| > Thanks for posting in this group.
| > I have reviewed your post. Just as Ted points out, your approach of
using
| > union is almost a good workaround, but it has some problem.
| > You can find the detailed problems in Ted's post.(It is mainly due to
Net
| > maintains the readonly length property of array).
| >
| > While the unmanaged approach many people suggested also has one problem,
| we
| > can not find a way to convert umanaged byte pointer to managed byte[].
| >
| > At this time, I think the only way of doing this is copying memory which
| > you do not like.
| >
| > I will do some research for this issue, I will reply to you as soon as I
| > research out.
| > Thanks for your understanding.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! -
www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <
[email protected]>
| > | References: <
[email protected]>
| > <
[email protected]>
| > <#
[email protected]>
| > <
[email protected]>
| > | Subject: Re: covert between byte[] and int []
| > | Date: Mon, 3 Nov 2003 16:57:53 -0500
| > | Lines: 115
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#
[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196416
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I do not like "bad code" although it works. So I want to make some
thing
| > | clear. In your second approach, you will use a BlockCopy. After I call
| > this
| > | function, the system will COPY all the data from one place to another
| > place
| > | (so, we will get two copy of the data after it) or just give the
| reference
| > | to another array (so we have only one copy of data but have two
refernce
| > of
| > | it.)? I need make this clear because I can not afford the copy in my
| > | application. I am working on a buffer size more the 2M, the COPY again
| and
| > | again will terribly reduce performance. And, in your first approach, I
| did
| > | not get the way to cast a pointer (byte*) back to byte[].
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <
[email protected]>
wrote
| > in
| > | message | > | > Ray,
| > | >
| > | > Your approach would work fine. The only reason I don't like it
is
| > | > because it associates a copy of the array in question with the
field.
| > | From
| > | > a design point of view, I would rather have two separate variables
| which
| > | > indicate that one is a different representation of the same thing.
| > | >
| > | > Also, I don't like having to define a new type explicitly for
this
| > | > purpose, when there are methods to do so.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - (e-mail address removed)
| > | >
| > | > | > | > > Thanks.
| > | > > Then, what's wrong with my approach?
| > | > > In my case, I can not use copy for sure because I am talking about
| > | buffer
| > | > > size at least 2M bytes.
| > | > > About the unsafe code, how can I cast a byte* to a byte[]? I need
| > byte[]
| > | > > because the stream reader (I need read image data from file some
| > times)
| > | > need
| > | > > byte[] as it's parameter.
| > | > >
| > | > > Again, go back my approach, is there something wrong?
| > | > >
| > | > >
| > | > > "Nicholas Paldino [.NET/C# MVP]" <
[email protected]>
| > wrote
| > | > in
| > | > > message | > | > > > Ray,
| > | > > >
| > | > > > You have two options here. The first is to use unsafe code,
| and
| > | > then
| > | > > > cast the pointer to the byte array (which is really a pointer
to a
| > | > > location
| > | > > > in memory to a byte) to a pointer to an integer, and then work
| from
| > | > there.
| > | > > >
| > | > > > If you want a purely managed solution, then I would use the
| > static
| > | > > > BlockCopy method on the Buffer class to copy the bytes from the
| byte
| > | > array
| > | > > > to an appropriately-sized integer array.
| > | > > >
| > | > > > Hope this helps.
| > | > > >
| > | > > >
| > | > > > --
| > | > > > - Nicholas Paldino [.NET/C# MVP]
| > | > > > - (e-mail address removed)
| > | > > >
| > | > > > | > | > > > > So far, I get the idea that if I want to use both the
unmanaged
| > and
| > | > > > managed
| > | > > > > memory, I can not avoid memory copy. But I DO need to avoid
it.
| I
| > | get
| > | > a
| > | > > > idea
| > | > > > > that maybe I could use "union" to convert byte[] to int[] and
so
| > on.
| > | > > Here
| > | > > > is
| > | > > > > my source code, I wonder if this will work with GC?
| > | > > > >
| > | > > > > [StructLayout(LayoutKind.Explicit)]
| > | > > > >
| > | > > > > struct MyUnion
| > | > > > >
| > | > > > > {
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public int[] a;
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public byte[] b;
| > | > > > >
| > | > > > > }
| > | > > > >
| > | > > > > After I define this "union". I can use the following code to
| alloc
| > | and
| > | > > > > access the data buffer:
| > | > > > > MyUnion mu = new MyUnion();
| > | > > > > mu.a = new int[640*480];
| > | > > > > //if I like access it as byte[]
| > | > > > > byte[] bp = mu.b;
| > | > > > >
| > | > > > > will this work?
| > | > > > >
| > | > > > >
| > | > > >
| > | > > >
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|