RE: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-06-01 01:08:41
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]=20
> Sent: Sunday, June 01, 2003 2:24 AM
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: RE: [Dev-C++] #DEFINE versus cont variables in array=20
> inicializations
>=20
>=20
> You do realise that a reference is just a slightly modified=20
> pointer, with
> required initialization and without any pointer manipulation allowed.
Yes.
=20
> While you could do:
> const int &ref =3D 7;
> the majority of code using references does:
> int var =3D 7;
> const int &ref =3D var;
Not only. Consider the numerous functions and methods getting const T &. =
If
you mean by functionality, then yes.
=20
> Since 'const int &ref' allows for aliasing, i.e. that you have other
> access methods to change the value that the reference points to, it is
> hard - and often impossible - for the compiler to consider=20
> optimization
> from a reference.
No. Consider A string literal like "string". It has an address (behaves =
as a
const char * const array), but it can be optimised away.
=20
> Since a reference - like a pointer - is an indirect access=20
> method, they
> are not optimal for object access, unless you do a lot of localized
> accesses using the same reference, in which case the compiler=20
> may cache
> the reference in a register and thereby getting the same=20
> access times as
> for a variable with a fixed address.=20
When talking about optimizations let's not mention possible run-time
optimisations, only compile time optimisations. Not to mention the fact =
that
registers are controlled by the OS in a non-dedicated system.
In any case we are talking about compiler (compile time) optimisations =
and
in particular about inlining.
> Pointers and references are
> at their best when you have a large object that you don't=20
> want to send as
> a value, i.e. instead of a function taking a large struct as=20
> parameter, it
> can take a reference to that struct.
Yes.
> However, that reference is - internally to the machine -=20
> identical to a
> pointer, and when the compiler tries to access anything through the
> reference, it have to do an indirection.
Yes and no. It depends on what the compiler produces (and in our =
discussion
what the compiler can do in theory).
> A reference isn't sometihng of zero size that doesn't exist.=20
References do not have an address because they were invented exactly so =
as
in many cases to not occupy space by themselves.
> Except for
> rare cases when the compiler is 100% sure about what it points to, the
> reference have the same size - and will generate identical code - as a
> pointer.
In many cases yes. But the rest are not rare cases. Example:
template <class T>
void something(const T &x)
{
// ...
}
something("literal");
The literal would be easy to be inlined.
=20
> The whole id=E9a with a pointer or a reference is to either=20
> allow a variable
> to point at different entities of something (such as when a=20
> constructor or
> function takes a reference without knowing at compile time=20
> what it will be
> initialized to) or when you have an object too large to send as a
> parameter.
Yes.
> In both these cases, it is impossible for the compiler to=20
> optimize away
> the address value stored by the reference, since that would=20
> either make
> the constructor or function only supporting a hardcoded parameter,
In the first case yes.
> or
> would in the second case force the large object to be sent on=20
> the stack
> instead of just the address of the object.
No. Why?
class whatever
{
int x;
public:
whatever(int y):x(y) {}
=20
operator int() const { return x; }
// ...
};
template <class T>
inline const T &something(const T &x)
{
return x+1;
}
int x;
whatever v(7);
x=3Dsomething(v);
In the above case the compiler can optimise out the passing of whatever =
to
something().
=20
> So:
> const int &ref =3D 7;
> would normally consume (sizeof(void*) + sizeof(int))
Something similar to sizeof(int *) + sizeof(int), if it creates a =
temporary
(which will have lifetime till the end of the scope). But nothing =
prohibits
it to inline it as it can also inline a const int var=3D7; I am not =
talking
what the current version of MINGW does here.
The bottom line is do you agree that a compiler can inline a const =
reference
as it can inline a const variable? If you agree it's ok, if you do not
agree, we cannot agree. :)
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|