Arrarys of reference

Discussion in 'C++' started by Tim Clacy, Oct 31, 2003.

  1. Tim Clacy

    Tim Clacy Guest

    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
    Tim Clacy, Oct 31, 2003
    #1
    1. Advertising

  2. Tim Clacy

    Jon Bell Guest

    In article <3fa2786d$0$258$>,
    Tim Clacy <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Oct 31, 2003
    #2
    1. Advertising

  3. "Tim Clacy" <> wrote
    in message news:3fa2786d$0$258$...
    | 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
    Ivan Vecerina, Oct 31, 2003
    #3
  4. Tim Clacy

    Tim Clacy Guest

    Jon Bell wrote:
    > In article <3fa2786d$0$258$>,
    > Tim Clacy <> 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 :-(
    Tim Clacy, Oct 31, 2003
    #4
  5. > >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.
    Andrew Koenig, Oct 31, 2003
    #5
  6. Tim Clacy

    Tim Clacy Guest

    Ivan Vecerina wrote:
    > "Tim Clacy" <> wrote
    > in message news:3fa2786d$0$258$...
    >> 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,
    Tim Clacy, Oct 31, 2003
    #6
  7. Tim Clacy

    DrPizza Guest

    "Tim Clacy" <> wrote in message
    news:3fa2786d$0$258$...
    > 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;} /* brainf*** program as argv[1] */
    DrPizza, Nov 2, 2003
    #7
  8. Tim Clacy

    Troll_King Guest

    "Tim Clacy" <> wrote in message news:<3fa2786d$0$258$>...
    > 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.
    Troll_King, Nov 2, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren
    Replies:
    0
    Views:
    494
    Darren
    Oct 11, 2004
  2. Dude
    Replies:
    0
    Views:
    352
  3. ce
    Replies:
    1
    Views:
    2,985
  4. Foxy Kav
    Replies:
    1
    Views:
    466
    John Carson
    Apr 25, 2004
  5. Simon Foster
    Replies:
    3
    Views:
    422
    Lonnie Princehouse
    Dec 18, 2003
Loading...

Share This Page