473,403 Members | 2,338 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,403 software developers and data experts.

byte[] Test2=&Test1[66];

I have another problem, maybe it is simple to fix.

I have this:

byte[] Test=new byte[100];

But I now want to have a second pointer Test2 to point to a location inside
this Test.
But with no copying.
Something like this.
byte[] Test2=&Test[66];

The intentions is that Test2[0] == Test[66]

I need this to find a record position inside a memory block that is a array
of bytes.
And I need this reference to modify the memory directly.

Any idea how to do this?

--
http://www.skyscan.be

Nov 16 '05 #1
5 1586
Hi Olaf,

what are you referring to is called unsafe code in C#. To use it, you need
to compile with the /unsafe compile option and mark the method or class in
which you want to use unsafe constructs as 'unsafe'.

To do exactly what you are saying:

unsafe void Test()
{
byte[] test = new byte[100];

test[66] = 20;

fixed (byte* test2 = &test[66])
{
Console.WriteLine("test[66] = {0}, test2[0] = {1}", test[66],
test2[0]);
test2[0] = 100;
Console.WriteLine("test[66] = {0}, test2[0] = {1}", test[66],
test2[0]);
}
}

But I would recommend not to use unsafe code unless you really need it.
First, it doesn't necessarily increase performance, as it prevents the
garbage collector from moving the fixed object. Secondly, your code becomes
unverifiable and thus requires full trust to execute. For non performance
critical scenarios, it could be better to write a wrapper class around the
byte array, for example to access bytes with offset (really just an
example):

public class MyWrapper
{
byte[] data;
int offset;

public MyWrapper(byte[] data, int offset)
{
this.data = data;
this.offset = offset;
}

public byte this[int index]
{
get { return data[index + offset]; }
set { data[index + offset] = value; }
}
}

HTH,
Stefan

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
I have another problem, maybe it is simple to fix.

I have this:

byte[] Test=new byte[100];

But I now want to have a second pointer Test2 to point to a location
inside
this Test.
But with no copying.
Something like this.
byte[] Test2=&Test[66];

The intentions is that Test2[0] == Test[66]

I need this to find a record position inside a memory block that is a
array
of bytes.
And I need this reference to modify the memory directly.

Any idea how to do this?

--
http://www.skyscan.be

Nov 16 '05 #2
"Olaf Baeyens" wrote:
I have another problem, maybe it is simple to fix.

I have this:

byte[] Test=new byte[100];

But I now want to have a second pointer Test2 to point to a location inside
this Test.
But with no copying.
Something like this.
byte[] Test2=&Test[66];

The intentions is that Test2[0] == Test[66]
That is impossible to do in C#.
I need this to find a record position inside a memory block that is a array
of bytes.
And I need this reference to modify the memory directly.

Any idea how to do this?


As you write it, you can use pointers in unsafe code blocks or unsafe methods.

byte[] b = new byte[ 100 ];
unsafe {
fixed( byte* pBuffer = &b[ 66 ] ) {
// ...
}
}

To avoid using "fix"-ed statements, you can use GCHandle to pin arrays and
have a pointer to it.

byte[] b = new byte[ 100 ];
GCHandle hData = GCHandle.Alloc( b, GCHandleType.Pinned );
try {
unsafe {
byte* pBuffer = ((byte*)hData.AddrOfPinnedObject().ToPointer()) + 66;
// ...
}
}
finally {
hData.Free();
}

HTH,
Tom.
Nov 16 '05 #3
> what are you referring to is called unsafe code in C#. To use it, you need
to compile with the /unsafe compile option and mark the method or class in
which you want to use unsafe constructs as 'unsafe'.


Well I need this as a function result so that other classes can point to
this series of bytes and process it on their own.
I am porting my C++ code to C# and these are techniques used in the original
C++ classes.

I do realize that the managed way is different than unmanaged.
Below is part of the orignal C++ code that I want to port

assume char *m_pBuffer=new byte[126];

public: BYTE *RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
BYTE *pPos=m_pBuffer+iBufferOffset;
return pPos;
};

So I was hoping to have something like this in C#:

assume byte[] m_pBuffer=new byte[126];

public byte[] RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
byte [] pPos=m_pBuffer+iBufferOffset;
return pPos;
};

But I can imaging that the GC will have trouble keeping track of the
m_pBuffer.
The bad news is that it is performance critical.
Nov 16 '05 #4
Olaf,

Instead of passing an array, why not create a collection and pass that
around? Or rather, create a wrapper of some sort that references the
original array, but each wrapper can have a different offset, and when you
perform operations (through the wrapper), it works on the offset, something
like this:

public class ArrayWrapper
{
// The offset.
private int offset;

// The array.
byte[] array;

public ArrayWrapper(byte[] array, int offset)
{
// Store the array and offset.
this.array = array;
this.offset = offset;
}

// This is where the important stuff takes place.
public byte this[int index]
{
get
{
// Get the value, offset by the index.
return array[index + offset];
}
set
{
// Set the value, offset by the index.
array[index + offset] = value;
}
}
}

This would allow you to point to the same array, and have offsets into
the array, and not change the indexes in other areas.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
what are you referring to is called unsafe code in C#. To use it, you
need
to compile with the /unsafe compile option and mark the method or class
in
which you want to use unsafe constructs as 'unsafe'.


Well I need this as a function result so that other classes can point to
this series of bytes and process it on their own.
I am porting my C++ code to C# and these are techniques used in the
original
C++ classes.

I do realize that the managed way is different than unmanaged.
Below is part of the orignal C++ code that I want to port

assume char *m_pBuffer=new byte[126];

public: BYTE *RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
BYTE *pPos=m_pBuffer+iBufferOffset;
return pPos;
};

So I was hoping to have something like this in C#:

assume byte[] m_pBuffer=new byte[126];

public byte[] RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
byte [] pPos=m_pBuffer+iBufferOffset;
return pPos;
};

But I can imaging that the GC will have trouble keeping track of the
m_pBuffer.
The bad news is that it is performance critical.

Nov 16 '05 #5
Olaf,

Instead of passing an array, why not create a collection and pass that
around? Or rather, create a wrapper of some sort that references the
original array, but each wrapper can have a different offset, and when you
perform operations (through the wrapper), it works on the offset, something
like this:

public class ArrayWrapper
{
// The offset.
private int offset;

// The array.
byte[] array;

public ArrayWrapper(byte[] array, int offset)
{
// Store the array and offset.
this.array = array;
this.offset = offset;
}

// This is where the important stuff takes place.
public byte this[int index]
{
get
{
// Get the value, offset by the index.
return array[index + offset];
}
set
{
// Set the value, offset by the index.
array[index + offset] = value;
}
}
}

This would allow you to point to the same array, and have offsets into
the array, and not change the indexes in other areas.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
what are you referring to is called unsafe code in C#. To use it, you
need
to compile with the /unsafe compile option and mark the method or class
in
which you want to use unsafe constructs as 'unsafe'.


Well I need this as a function result so that other classes can point to
this series of bytes and process it on their own.
I am porting my C++ code to C# and these are techniques used in the
original
C++ classes.

I do realize that the managed way is different than unmanaged.
Below is part of the orignal C++ code that I want to port

assume char *m_pBuffer=new byte[126];

public: BYTE *RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
BYTE *pPos=m_pBuffer+iBufferOffset;
return pPos;
};

So I was hoping to have something like this in C#:

assume byte[] m_pBuffer=new byte[126];

public byte[] RecGetRecAt(const _int64 aiIndex) {
_int64 iBufferOffset=m_iRecSize*aiIndex;
byte [] pPos=m_pBuffer+iBufferOffset;
return pPos;
};

But I can imaging that the GC will have trouble keeping track of the
m_pBuffer.
The bad news is that it is performance critical.

Nov 16 '05 #6

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

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
13
by: Ray Z | last post by:
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...
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.