Arrarys of reference

T

Tim Clacy

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
 
J

Jon Bell

Tim Clacy said:
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.
 
I

Ivan Vecerina

in message | 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
 
T

Tim Clacy

Jon said:
Tim Clacy said:
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 :-(
 
A

Andrew Koenig

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.
 
T

Tim Clacy

Ivan said:
Tim Clacy said:
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,
 
D

DrPizza

Tim Clacy said:
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;} /* (e-mail address removed) brainf*** program as argv[1] */
 
T

Troll_King

Tim Clacy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top