473,657 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of pointers/references to another array

I have an array (List<T>) containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To minimize memory
requirements, in C++, I can have arrays of pointers to the first array
(T*[]). I understand I can do this in C# as well, but the code is
unsafe. Is there a better way of doing this in C#?

Thanks,
Naveed

Feb 21 '06 #1
4 4595
na****@gmail.co m wrote:
I have an array (List<T>)
First thing: List<T> is *not* an array. They're different things.
containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To minimize memory
requirements, in C++, I can have arrays of pointers to the first array
(T*[]). I understand I can do this in C# as well, but the code is
unsafe. Is there a better way of doing this in C#?


List<T> is a reference type - just keep a reference to the object.

See http://www.pobox.com/~skeet/csharp/memory.html and
http://www.pobox.com/~skeet/csharp/parameters.html for more on value
types vs reference types.

However, if you're maintaining copies of the list sorted in different
ways, it seems to me that you're not interested in pointers to the
list. After all, the different copies *are* different lists. What's
important is whether the *contents* of the lists are references or
values themselves. That depends on whether T is a value type or a
reference type.

Jon

Feb 21 '06 #2
A List<T> is a generic ArrayList. List<car> can ONLY hold objects of car.
Now if you want to pass List<car> to a method or another place but dont want
to send the entire object, you can use reference. Kinda like pointers in
c++.

Reference is used as followed:
public void sendreference( ref Person p)
{
p.age=555;
}

static void main()
{
Person mel= new Person("Mel",30 );
sendreference(r ef mel);
}

What happens here is that you pass along the reference to the object mel of
class Person. Any changes you make in the method that recieves the reference
is done to the object itself....So you only pass the "pointer" of the
object.

Hope this helped

<na****@gmail.c om> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I have an array (List<T>) containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To minimize memory
requirements, in C++, I can have arrays of pointers to the first array
(T*[]). I understand I can do this in C# as well, but the code is
unsafe. Is there a better way of doing this in C#?

Thanks,
Naveed

Feb 21 '06 #3
>Now if you want to pass List<car> to a method or another place but dont want
to send the entire object, you can use reference. [...]Reference is used as followed:
public void sendreference( ref Person p)


Since List<T> is a reference type you will always just pass around
object references, never "the entire object". No need for the ref
keyword here if you only want to change the content of the list. For
more information see
http://www.yoda.arachsys.com/csharp/parameters.html
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 21 '06 #4

"Deleo" <De***@noemail. noemail> wrote in message
news:Om******** ******@TK2MSFTN GP15.phx.gbl...
public void sendreference( ref Person p)
{
p.age=555;
}

static void main()
{
Person mel= new Person("Mel",30 );
sendreference(r ef mel);
}


Whilst this is syntactically correct it is a bad example since the use of
reference makes no difference to the behaviour of sendreference whatsoever.

The only point in passing a reference to a reference type is to swap it with
another or make it null:

public void sendreference(r ef Person p)
{
if( p.age == 55 )
p = null;
}

This would make "mel" in main null

For the C++ coders the rough equivalents (allowing for -> rather than .)
are:

C# C++
=============== ==========
Person Person*
ref Person Person*&
out Person Person*&
Feb 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
1930
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically interested in multidimensional arrays. It is often said that arrays and pointers are virtually identical. My observations are that my (gcc) compiler knows the difference between T*, T a1, and a2. What I'm currently trying to do is to return a...
5
4492
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is getting in the way. int a; int b; int **present; int **next;
9
2299
by: Timothy Madden | last post by:
Hello If I write a function like this void Process(double Data) { ... } it is ok, but if I try class DataProcess { double (&Data); DataProcess(double Data)
41
8307
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
2
6082
by: Neelesh | last post by:
Hi all, References are (almost always) implemented as constant pointers. We can certainly have an array of constant pointers. But then why we cannot have array of references? I can think of atleast two places where such a thing 'might' be used, though I am not clear whether it makes sense to have such a usage - 1) Function call int f (int& a); // a is an array of integer references
5
1672
by: Kiran | last post by:
Hi all, another newbie question from me, but here goes. Ok, I have a double array defined as follows: int cluster; now, i know that if you say for example cluster, this will give me the address of where cluster resides. now for the program i am writing, at some point in the program i may want cluster to
11
13433
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
11
4647
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type cli:array<cli::interior_ptr<float>, 2>... So, what do I do?
31
3199
by: siddhu | last post by:
why can't we have array of references.
0
8384
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.