sub-array references?

K

Kresimir Spes

Is the following C code possibile in C# (without the pointers ofcourse)

int a[1000];
int* b=&a[100]; // or a+100
b[0]=0;


what I would like to do is create a new int[] array object and have it
point at the beginning of the 100th element of array "a" and set its
size to 100 elements, so I can pass that reference array to another
function that does a foreach loop. I only want to perform operations on
those 100 elements, not the whole array.


// Yes, I can rewrite the foreach block, but that's not the point.
foreach was just an example.


Thanks in advance,
 
B

Bob

Hi Kresimir
Is this close to what you are after?
int[] a;

a = new int[1000];

//Stuff here to fill a

int b = 100; //cursor

int max = 199; //boundary

for (int i = b; i < max; i++)

a = SomeFunction(a);

regards

Bob
 
B

Bruce Wood

No, you would have to write your own wrapper class that implemented
appropriate interfaces, and then have your function take any object
that implemented the particular interface you needed, or you would have
to write your own wrapper class that inherits from System.Array and
then have your method take an Array rather than any specific type of
array.

The reason this worked in C is that in C arrays contained no
meta-information. In C#, an array knows how big it is and what type it
is. So, even if you could get a pointer into the middle of the array,
it still wouldn't work as an object reference because the correct
meta-information wouldn't be there.
 
B

Bruce Wood

Sorry... I should clarify.

The wrapper class would contain a reference to the array, a start
index, and an artificial length. Any functionality that the wrapper
were to export would act as though the array started with the indicated
element and were limited to the fake length.

Unfortunately, you couldn't then pass it off as an int[], for example,
because you can't inherit from "int[]"... at least, not as far as I
know. Your functions would have to be declared so that either a real
array or the wrapper were acceptable arguments.
 
S

Scott C

Bruce said:
Sorry... I should clarify.

The wrapper class would contain a reference to the array, a start
index, and an artificial length. Any functionality that the wrapper
were to export would act as though the array started with the indicated
element and were limited to the fake length.

Unfortunately, you couldn't then pass it off as an int[], for example,
because you can't inherit from "int[]"... at least, not as far as I
know. Your functions would have to be declared so that either a real
array or the wrapper were acceptable arguments.


Just an idea, but couldn't you make an explicit or implicit (heaven
forbid) conversion from this wrapper class to an int[] or whatever, and
make a new int[] when the conversion is needed? sucks for performance,
but seems like it might work.

Scott
 
P

Peter N Roth

Kresimir - Is it that maybe you're trying to do your own
'memory management'? I.e., allocate one array, and use
subarrays as individual arrays?

If that is the case, I opine it would be better practice to
allocate each subarray individually with the appropriate name.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
 
B

Bruce Wood

Yes, it would work, but I inferred from the original post that the idea
was to save the cost of creating a new array, which is why my solution
was so complicated. :)
 
C

Carl

If you're using .NET 2.0 the ArraySegment<> structure may do what
you're looking for,
http://msdn2.microsoft.com/en-us/library/1hsbd92d.aspx. From the
documentation:

<quote>
ArraySegment is a wrapper around an array that delimits a range of
elements in that array. Multiple ArraySegment instances can refer to
the same original array and can overlap.

The Array property returns the entire original array, not a copy of the
array; therefore, changes made to the array returned by the Array
property are made to the original array.
</quote>

If you're not using 2.0, and you _really_ need to use this approach,
you could create an "unsafe" method and use pointers. Of course that
introduces a lot of issues (GC complications, need to fully trust the
assembly, etc), w/c will probably outweigh any benefit you'd get by
avoiding array copies.

Carl
http://zumbano.com/blog
 
K

Kresimir Spes

thanks guys! "ArraySegment" was what I was looking for :D

As a former C++ programmer, it's hard to let go of memory management :))
 

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