partial arrays???

O

ole.tetzschner

Hi

I'm just another guy moving from c++ to c# and got this issue:

Byte [] mydata = new Byte[48];
//... move some data to the array
Byte [] my_partial_data;

my question...

Is it possible to assign "my_partial_data" to a part of "mydata"? eg.
the part mydata[13] to mydata[21]... without allocating new memory...
something like a pointer in c++ where BYTE *p_my_partial_data = &mydata
[13].

Kind regards, Ole
 
D

David Anton

The 'CopyTo' method on arrays might be what you want.
Other than that, you could use 'unsafe' code in C#, but nobody really wants
that.
Your final option is to use a loop.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++
 
O

ole.tetzschner

The 'CopyTo' method on arrays might be what you want.
Other than that, you could use 'unsafe' code in C#, but nobody really wants
that.
Your final option is to use a loop.
--http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++

I'm just another guy moving from c++ to c# and got this issue:
Byte [] mydata = new Byte[48];
//... move some data to the array
Byte [] my_partial_data;
my question...
Is it possible to assign "my_partial_data" to a part of "mydata"? eg.
the part mydata[13] to mydata[21]... without allocating new memory...
something like a pointer in c++ where BYTE *p_my_partial_data = &mydata
[13].
Kind regards, Ole

Hi David

Thanks for your answer :)

No i don't want to use unsafe code.

My problem is that i dont want to waste cpu and ram, so the copy-
method is not what i'm looking for. I'm looking for a kind of
"reference" to the original array, but that "reference" should only be
a part of the original array... simply, they have to share the same
allocation. Is this possible in c#???


Kind regards, Ole
 
G

Göran Andersson

My problem is that i dont want to waste cpu and ram, so the copy-
method is not what i'm looking for. I'm looking for a kind of
"reference" to the original array, but that "reference" should only be
a part of the original array... simply, they have to share the same
allocation. Is this possible in c#???

No, that is not possible.

At least not without doing something like using unsafe code/reflection
to change the internal variables of an Array object to use part of
another array as data area. But then you would also have to pin both
arrays in memory, which isn't really good for memory management...

As usual with these kind of 'almost impossible' questions, you should
rather ask about what it is that you are trying to accomplish instead of
asking about how you think that it should be solved...
 
H

Hans Kesting

(e-mail address removed) expressed precisely :
Hi

I'm just another guy moving from c++ to c# and got this issue:

Byte [] mydata = new Byte[48];
//... move some data to the array
Byte [] my_partial_data;

my question...

Is it possible to assign "my_partial_data" to a part of "mydata"? eg.
the part mydata[13] to mydata[21]... without allocating new memory...
something like a pointer in c++ where BYTE *p_my_partial_data = &mydata
[13].

Kind regards, Ole

Does it need to be a byte[] or could you use another class?

If an other class is ok, then you could make a wrapper around a byte[]
that gives access to just the portion you want.
Supply the constructor with the the byte[], plus the start and length
of the area you want.
Then add an indexer that redirects a [0] to the start of the area you
want.
As you pass just the reference to the array, nothing of that array is
copied.

Something like:

public class Wrapper
{
byte[] _source;
int _start, _length;

public Wrapper (byte[] source, int start, int length)
{
_source = source; _start = start; _length = length;
}

public byte this[int idx]
{
get { return _source[idx+_start]; }
}
}

but then with some checking of limits etc.

Hans Kesting
 
J

Jeff Louie

Ole... Just to be sure you know that arrays in C# are objects and may
contain
references to objects or may contain actual structures, but not actual
objects so:

MClass[] arrayMC= new MyClass[10];
MClass[] partialArrayMC= new MyClass[2];

does not create any instances of MyClass, so that both arrays as written
contain
elements that are null. So you can now do:

MyClass mc= new MyClass();
arrayMC[0]= mc;
partialArrayMC[0]= arrayMC[0];

and there is only one instance of MyClass in memory. This is because C#
classes
use reference semantics while C++ classes use value semantics.

Regards,
Jeff
 
O

ole.tetzschner

Ole... Just to be sure you know that arrays in C# are objects and may
contain
references to objects or may contain actual structures, but not actual
objects so:

MClass[] arrayMC= new MyClass[10];
MClass[] partialArrayMC= new MyClass[2];

does not create any instances of MyClass, so that both arrays as written
contain
elements that are null. So you can now do:

MyClass mc= new MyClass();
arrayMC[0]= mc;
partialArrayMC[0]= arrayMC[0];

and there is only one instance of MyClass in memory. This is because C#
classes
use reference semantics while C++ classes use value semantics.

Regards,
Jeff

*** Sent via Developersdexhttp://www.developersdex.com***

Thanks guys :)

Yes of course, its an array of objects... my mistake. So my idea of
saving cpu/ram does not make sense, since i would have to create an
new array of objects.

Still just flirting with c# :) but still afraid of performing
issues...

Thanks, Ole
 
P

Pavel Minaev

Ole... Just to be sure you know that arrays in C# are objects and may
contain
references to objects or may contain actual structures, but not actual
objects so:
MClass[] arrayMC= new MyClass[10];
MClass[] partialArrayMC= new MyClass[2];
does not create any instances of MyClass, so that both arrays as written
contain
elements that are null. So you can now do:
MyClass mc= new MyClass();
arrayMC[0]= mc;
partialArrayMC[0]= arrayMC[0];
and there is only one instance of MyClass in memory. This is because C#
classes
use reference semantics while C++ classes use value semantics.

*** Sent via Developersdexhttp://www.developersdex.com***

Thanks guys :)

Yes of course, its an array of objects... my mistake. So my idea of
saving cpu/ram does not make sense, since i would have to create an
new array of objects.

Note that the byte[] array in your original example is _not_ an array
of referencesto objects - it's an array of bytes, and as bytes are
value types, they are stored inline within the memory block allocated
for the array.

..NET actually has a special type to reference a part of an array - see
System.ArraySegment<T>. Unfortunately, it doesn't really provide any
convenience methods - it's really just a struct that stores the
reference to the original array, the starting index, and the count.
It's up to you to actually use them:

byte[] b = { 1, 2, 3, 4, 5, 6 };
var sub = new ArraySegment<byte>(b, 2, 3); // {3, 4, 5}
for (int i = sub.Start; i < sub.Offset+ sub.Count; ++i) { // iterate
through the segment
Console.WriteLine(sub.Array);
}

as you can see, you have to do all the arithmetics manually. All that
it gives you is the standard type to pass the array + offsets around.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top