473,326 Members | 2,133 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,326 software developers and data experts.

Arrarys of reference

I know arrays of references are not allowed, but have a couple of questions:

1) Why aren't arrays of references allowed :-) ?

2) What's a good practical alternative?
As an example, Intel's PXA255 processor's DMA controller has 16 consecutive
registers for DMA channel control & status called DCSRx. It would be nice to
have a short-hand access to these registers in addition to indirect access
through structure members:

// Simple hardware model
//
struct DMAController
{
Register DCSR[16];
Register DINT;
:
} dmac;
// Short-hand aliases
//
Register& DCSR0 = dmac.DCSR[0];
Register& DCSR1 = dmac.DCSR[1];
Register& DCSR2 = dmac.DCSR[2];
Register& DCSR3 = dmac.DCSR[3];
:
// This would be handy, but isn't allowed... and how would the references be
initialised in any case?
//
Register& DCSR[16];
If arrays of references were allowed, registers could be accessed using
DCSR[n]. Does anyone have a solution that would allow such convenient
short-hand? This is largely academic, since access through a structure
members will get the job done; however, I would be interested in any
thoughts and/or solutions (it bugs me). In general, if there is an array of
objects, how can access be provided to those objects by reference still
using array operators?
Tim
Jul 19 '05 #1
7 5983
In article <3f*********************@news.dk.uu.net>,
Tim Clacy <no*******@nospamphaseone.nospamdk> wrote:
[snip]
struct DMAController
{
Register DCSR[16];
Register DINT;
:
} dmac;
// Short-hand aliases
//
Register& DCSR0 = dmac.DCSR[0];
Register& DCSR1 = dmac.DCSR[1];
Register& DCSR2 = dmac.DCSR[2];
Register& DCSR3 = dmac.DCSR[3];
:
// This would be handy, but isn't allowed... and how would the references be
initialised in any case?
//
Register& DCSR[16];
If arrays of references were allowed, registers could be accessed using
DCSR[n].


How about this?

Register* DCSR = &dmac.DCSR[0];

This takes the address of the first DCSR in the struct and puts it in the
pointer variable DCSR. You can now access any of the registers using
array notation, because DCSR[n] is exactly equivalent to *(DCSR + n).

This doesn't involve references at all, of course, but it gives you the
syntax that you want, for referring to the registers.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #2
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote
in message news:3f*********************@news.dk.uu.net...
| I know arrays of references are not allowed, but have a couple of
questions:
|
| 1) Why aren't arrays of references allowed :-) ?

Maybe because in many circumstances, it would be impossible
to initialize the references correctly.
Maybe because arrays of references often couldn't be
optimized out the way that single references can.

| 2) What's a good practical alternative?

Arrays of pointers. (or const pointers, if you wish).

| As an example, Intel's PXA255 processor's DMA controller has 16
consecutive
| registers for DMA channel control & status called DCSRx. It would be nice
to
| have a short-hand access to these registers in addition to indirect access
| through structure members:
|
| // Simple hardware model
| //
| struct DMAController
| {
| Register DCSR[16];
| Register DINT;
| :
| } dmac;
....
| // This would be handy, but isn't allowed... and how would the references
be
| initialised in any case?
| //
| Register& DCSR[16];

What you can use instead is a reference to the array:
typedef Register RegSet16[16];
RegSet16& DCSR = dmac.DCSR;

| If arrays of references were allowed, registers could be accessed using
| DCSR[n]. Does anyone have a solution that would allow such convenient
| short-hand? This is largely academic, since access through a structure
| members will get the job done; however, I would be interested in any
| thoughts and/or solutions (it bugs me). In general, if there is an array
of
| objects, how can access be provided to those objects by reference still
| using array operators?

Will the above suggestion do?
hth,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #3
Jon Bell wrote:
In article <3f*********************@news.dk.uu.net>,
Tim Clacy <no*******@nospamphaseone.nospamdk> wrote:
[snip]
struct DMAController
{
Register DCSR[16];
Register DINT;
:
} dmac;
// Short-hand aliases
//
Register& DCSR0 = dmac.DCSR[0];
Register& DCSR1 = dmac.DCSR[1];
Register& DCSR2 = dmac.DCSR[2];
Register& DCSR3 = dmac.DCSR[3];

// This would be handy, but isn't allowed... and how would the
references be initialised in any case?
//
Register& DCSR[16];
If arrays of references were allowed, registers could be accessed
using DCSR[n].


How about this?

Register* DCSR = &dmac.DCSR[0];

This takes the address of the first DCSR in the struct and puts it in
the pointer variable DCSR. You can now access any of the registers
using
array notation, because DCSR[n] is exactly equivalent to *(DCSR + n).

This doesn't involve references at all, of course, but it gives you
the syntax that you want, for referring to the registers.


Thanks Jon,

....I'm both hanging my head in shame and blushing at the same time :-(

Jul 19 '05 #4
> >struct DMAController
{
Register DCSR[16];
Register DINT;
:
} dmac;
How about this?

Register* DCSR = &dmac.DCSR[0];


Or, for that matter, this?

Register (&DSCR)[16] = dmac.DSCR;

Note that this defines a reference to an array, which is allowed, as opposed
to an array of references, which isn't.
Jul 19 '05 #5
Ivan Vecerina wrote:
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote
in message news:3f*********************@news.dk.uu.net...
I know arrays of references are not allowed, but have a couple of
questions:

1) Why aren't arrays of references allowed :-) ?


Maybe because in many circumstances, it would be impossible
to initialize the references correctly.
Maybe because arrays of references often couldn't be
optimized out the way that single references can.
2) What's a good practical alternative?


Arrays of pointers. (or const pointers, if you wish).
As an example, Intel's PXA255 processor's DMA controller has 16
consecutive registers for DMA channel control & status called DCSRx.
It would be nice to have a short-hand access to these registers in
addition to indirect access through structure members:

// Simple hardware model
//
struct DMAController
{
Register DCSR[16];
Register DINT;
:
} dmac;

...
// This would be handy, but isn't allowed... and how would the
references be initialised in any case?
//
Register& DCSR[16];


What you can use instead is a reference to the array:
typedef Register RegSet16[16];
RegSet16& DCSR = dmac.DCSR;
If arrays of references were allowed, registers could be accessed
using DCSR[n]. Does anyone have a solution that would allow such
convenient short-hand? This is largely academic, since access
through a structure members will get the job done; however, I would
be interested in any thoughts and/or solutions (it bugs me). In
general, if there is an array of objects, how can access be provided
to those objects by reference still using array operators?


Will the above suggestion do?
hth,
Ivan


Thanks Ivan,

Jul 19 '05 #6

"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message
news:3f*********************@news.dk.uu.net...
I know arrays of references are not allowed, but have a couple of questions:
1) Why aren't arrays of references allowed :-) ? 'cos references aren't objects. How do we know they're not objects? Because
you can't have arrays of them....
2) What's a good practical alternative? In general, an array of pointers. In this specific case, because you've got
an array already that you're referring to, a reference to an array or a single
pointer would both do the trick.
// This would be handy, but isn't allowed... and how would the references be
initialised in any case?
Register& DCSR[16];

Were it allowed, I don't see why normal aggregate initialization syntax
couldn't be used.
--
Now Playing: Ferry Corsten - Punk (kid vicious remix) (D I G I T A L L Y - I
M P O R T E D - European Trance, Techno, Hi-NRG... we can't define it!)
char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* dr*****@battleaxe.net brainf*** program as argv[1] */

Jul 19 '05 #7
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message news:<3f*********************@news.dk.uu.net>...
I know arrays of references are not allowed, but have a couple of questions:

1) Why aren't arrays of references allowed :-) ?

Tim


Possibly something to do with the problem that the array of references
would be volnerable. If one of the array elements were to reference a
local variable that went out of scope say when you passed the array to
a called function as an argument, than the array element referencing
that local variable would not point to anything.
Jul 19 '05 #8

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.