473,789 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6013
In article <3f************ *********@news. dk.uu.net>,
Tim Clacy <no*******@nosp amphaseone.nosp amdk> 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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #2
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote
in message news:3f******** *************@n ews.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*******@nosp amphaseone.nosp amdk> 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*******@nosp amphaseone.nosp amdk> wrote
in message news:3f******** *************@n ews.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*******@nosp amphaseone.nosp amdk> wrote in message
news:3f******** *************@n ews.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+=!(4 3^c)-!(45^c),44^c||r ead(0,p,1),46^c ||putchar(*p)
,91^c||(v=*p?ma in(-1,v+1),v-1:main(0,v)),++ v);else for(;c+=!(91^*v )-!(93^*v)
;++v);return v;} /* dr*****@battlea xe.net brainf*** program as argv[1] */

Jul 19 '05 #7
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> 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
13125
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 Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
110
9966
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 must be an object instead of
9
3003
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 tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
11
2205
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
17935
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 create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
4
3409
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
19
2146
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
3687
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 pointer and reference are and how they relate to each other.
275
12408
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
9661
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
9506
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
10193
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...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
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
5414
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...
0
5546
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2904
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.