By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

byte[] Test2=&Test1[66];

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
> 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.