Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Arrarys of reference

Reply
Thread Tools

Arrarys of reference

 
 
Tim Clacy
Guest
Posts: n/a
 
      10-31-2003
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


 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      10-31-2003
In article <3fa2786d$0$258$(E-Mail Removed)>,
Tim Clacy <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-31-2003
"Tim Clacy" <(E-Mail Removed)> wrote
in message news:3fa2786d$0$258$(E-Mail Removed)...
| 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


 
Reply With Quote
 
Tim Clacy
Guest
Posts: n/a
 
      10-31-2003
Jon Bell wrote:
> In article <3fa2786d$0$258$(E-Mail Removed)>,
> Tim Clacy <(E-Mail Removed)> 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



 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      10-31-2003
> >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.


 
Reply With Quote
 
Tim Clacy
Guest
Posts: n/a
 
      10-31-2003
Ivan Vecerina wrote:
> "Tim Clacy" <(E-Mail Removed)> wrote
> in message news:3fa2786d$0$258$(E-Mail Removed)...
>> 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,



 
Reply With Quote
 
DrPizza
Guest
Posts: n/a
 
      11-02-2003

"Tim Clacy" <(E-Mail Removed)> wrote in message
news:3fa2786d$0$258$(E-Mail Removed)...
> 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;} /* http://www.velocityreviews.com/forums/(E-Mail Removed) brainf*** program as argv[1] */



 
Reply With Quote
 
Troll_King
Guest
Posts: n/a
 
      11-02-2003
"Tim Clacy" <(E-Mail Removed)> wrote in message news:<3fa2786d$0$258$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM
web reference interfering with reference to component Dude ASP .Net 0 11-09-2004 11:53 AM
How to tell if a reference is project or file reference from within the IDE? Darren ASP .Net 0 10-11-2004 12:51 AM
Passing arrarys from a function to another function. Foxy Kav C++ 1 04-25-2004 02:58 AM
Converting arrarys in Python to arrays in C Simon Foster Python 3 12-18-2003 01:33 AM



Advertisments