471,325 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

sub-array references?

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,
Feb 23 '06 #1
8 5936
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[i] = SomeFunction(a[i]);

regards

Bob

"Kresimir Spes" <kr***@cateia.com> wrote in message
news:dt**********@ss405.t-com.hr...
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,

Feb 23 '06 #2
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.

Feb 23 '06 #3
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.

Feb 23 '06 #4
Bruce Wood wrote:
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
Feb 23 '06 #5
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
"Kresimir Spes" <kr***@cateia.com> wrote in message
news:dt**********@ss405.t-com.hr...
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,

Feb 23 '06 #6
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. :)

Feb 23 '06 #7
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

Feb 23 '06 #8
thanks guys! "ArraySegment" was what I was looking for :D

As a former C++ programmer, it's hard to let go of memory management :))
Feb 23 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kathy Burke | last post: by
20 posts views Thread by vitorjol | last post: by
6 posts views Thread by Greg Strong | last post: by
reply views Thread by rosydwin | last post: by

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.