473,387 Members | 1,530 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,387 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 6084
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
4
by: dbuchanan | last post by:
Is the following behavior normal? Both the 'Protected sub' in the inherited form and the 'Private Shadows sub' in the derived form fires. My interpretation of MSDN help on the topic "Shadows"...
1
by: dBNovice | last post by:
Please help! I have 3 forms: Task, Subtask, Elements. Elements is a subform of Subtask and Subtask is a subform of Task. I am able to navigate from Task to Subform to Element and from Element to...
7
by: ILCSP | last post by:
Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows how to call a procedure using a variable? The variable will be equal to the name of the procedure. for example, if I have...
20
by: vitorjol | last post by:
Hello. I have 2 forms (Form1 and Form2). When i call the Sub placed in form1 from form2, i get the error "object reference not set to an instance of an object" ! What can i do to solve the...
6
by: Greg Strong | last post by:
Hello All, Is is possible to use an ADO recordset to populate an unbound continuous Subform? I've done some Googling without much luck, so this maybe impossible, but let me try to explain...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.