PC Review


Reply
Thread Tools Rate Thread

Declaring array of references.

 
 
rayreeves
Guest
Posts: n/a
 
      22nd Jan 2007
How do I declare an array of references to the elements of an array of
values?

Ray


 
Reply With Quote
 
 
 
 
igd
Guest
Posts: n/a
 
      22nd Jan 2007
To convert a value type to a reference type and vise versa use
boxing/unboxing.

int foo = 42;
object bar = foo; // boxing
int helloWorld = (int)bar; // unboxing

In your case:

int[] intArray = new int[...];
object[] boxedIntArray = new object[...];

for(int n = 0; n < intArray.Count; ++n)
boxedIntArray[n] = intArray[n];

rayreeves schrieb:

> How do I declare an array of references to the elements of an array of
> values?
>
> Ray


 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      22nd Jan 2007
Ray,

I think the easiest way is to have the reference array store the
indexes into the value array. Did I understand your question
correctly?

Brian

rayreeves wrote:
> How do I declare an array of references to the elements of an array of
> values?
>
> Ray


 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      22nd Jan 2007

rayreeves wrote:
> How do I declare an array of references to the elements of an array of
> values?


Do you mean an array of arrays? You can simply do this:

int[][] arrayOfArraysOfInts;

You then have to say how many arrays you want in the main array:

arrayOfArraysOfInts = new int[15][];

And you then have to create each array of values individually:

arrayOfArraysOfInts[0] = new int[12];
arrayOfArraysOfInts[1] = new int[23];
arrayOfArraysOfInts[2] = new int[52];
....

Is that what you were after?

 
Reply With Quote
 
rayreeves
Guest
Posts: n/a
 
      22nd Jan 2007
I don't want an array of arrays, I think "igd" has given me what I want.
I have the old problem of sorting efficiently. My values are structures,
and instead of shunting them about I want to shunt the references.
Thanks for all your help.

Ray

"rayreeves (E-Mail Removed)>" <ray<mond> wrote in message
news:T_5th.1954$dk1.95@trndny03...
> How do I declare an array of references to the elements of an array of
> values?
>
> Ray
>
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      22nd Jan 2007

rayreeves wrote:
> I don't want an array of arrays, I think "igd" has given me what I want.
> I have the old problem of sorting efficiently. My values are structures,
> and instead of shunting them about I want to shunt the references.
> Thanks for all your help.


1. Your structs should never be so big that shuffling them about on a
sort causes efficiency concerns.

2. The solution given will not be more efficient for sorting. In order
to get the structs into the array of objects, each struct will have to
be boxed. This means that an object will have to be allocated on the
heap and the contents of the struct copied into it. Then, in order to
compare the struct values (for sorting), you will require an additional
pointer dereference to unbox the value. Then, when it's all over, the
boxes will have to be garbage collected off the heap.

How are your structs defined? As a general rule they shouldn't be
bigger than 16 bytes or so.

 
Reply With Quote
 
rayreeves
Guest
Posts: n/a
 
      23rd Jan 2007
This self-appointed project is to produce the world's fastest prime number
generator, where speed is the only criterion.
For example, on my 1300M processor my C++ code found the 5 000 000 primes
less than 100 000 000 in 14 secs. Unfortunately, I lost my hard drive and
all my source so I have to rewrite it, and it seems like a good time to try
in C#.
I can see that I have been under some misapprehensions about arrays and
references but I now understand that if I declare a class PRIMEVALUE with
two int fields p2 and pn and then declare an array pq = new PRIMEVALUE[some
size] it will take a suitable space on the heap and I can then populate the
elements with pq[i] = new PRIMEVALUE(p2Value, pnValue) and those element
values will also be on the heap. Now I assume that when I interchange
elements of pq
with swap(pq[i], pq[j]) the references will be exchanged but the values
remain where they are.
I also assume that references are the size of one int, so moving them should
be faster than moving two ints. Every msec counts! Garbage collection, I
think, is irrelevant.

In C++ I understood perfectly well what was going on, but C# conceals so
much that I have some doubts that it is suitable for this purpose.

Ray

..
"Bruce Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>


> 1. Your structs should never be so big that shuffling them about on a
> sort causes efficiency concerns.
>
> 2. The solution given will not be more efficient for sorting. In order
> to get the structs into the array of objects, each struct will have to
> be boxed. This means that an object will have to be allocated on the
> heap and the contents of the struct copied into it. Then, in order to
> compare the struct values (for sorting), you will require an additional
> pointer dereference to unbox the value. Then, when it's all over, the
> boxes will have to be garbage collected off the heap.
>
> How are your structs defined? As a general rule they shouldn't be
> bigger than 16 bytes or so.
>



 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      23rd Jan 2007
If PRIMEVALUE really is a class then the swapping would involve the
references only. If PRIMEVALUE is a struct then it must be boxed and
unboxed.

rayreeves wrote:
> This self-appointed project is to produce the world's fastest prime number
> generator, where speed is the only criterion.
> For example, on my 1300M processor my C++ code found the 5 000 000 primes
> less than 100 000 000 in 14 secs. Unfortunately, I lost my hard drive and
> all my source so I have to rewrite it, and it seems like a good time to try
> in C#.
> I can see that I have been under some misapprehensions about arrays and
> references but I now understand that if I declare a class PRIMEVALUE with
> two int fields p2 and pn and then declare an array pq = new PRIMEVALUE[some
> size] it will take a suitable space on the heap and I can then populate the
> elements with pq[i] = new PRIMEVALUE(p2Value, pnValue) and those element
> values will also be on the heap. Now I assume that when I interchange
> elements of pq
> with swap(pq[i], pq[j]) the references will be exchanged but the values
> remain where they are.
> I also assume that references are the size of one int, so moving them should
> be faster than moving two ints. Every msec counts! Garbage collection, I
> think, is irrelevant.
>
> In C++ I understood perfectly well what was going on, but C# conceals so
> much that I have some doubts that it is suitable for this purpose.
>
> Ray
>
> .


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Jan 2007
Surely it will only box if you talk about it as an "object" or access
object methods; just talking to a (heap) array doesn't cause boxing as
far as I can see....

Marc


 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      23rd Jan 2007
Err...yes. The OP was using an array of PRIMEVALUE objects and not an
object array. Nice catch. So I suppose it's not the boxing that's an
issue, but the copying of the fields.

Marc Gravell wrote:
> Surely it will only box if you talk about it as an "object" or access
> object methods; just talking to a (heap) array doesn't cause boxing as
> far as I can see....
>
> Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring an array Fred Blair Microsoft VB .NET 4 17th Nov 2008 08:34 PM
declaring array Monte0682 Microsoft Excel Programming 1 23rd Mar 2007 06:05 PM
Declaring Array Fredriksson via OfficeKB.com Microsoft Excel Programming 10 2nd Mar 2007 07:04 PM
declaring an array (i think) Cindy Microsoft Excel Programming 2 15th Mar 2004 11:41 PM
Principle of Design: Declaring References to Consumer of a Class Charles Law Microsoft Dot NET 0 26th Feb 2004 01:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.