Topic: About &v["size of v"]


Author: Salters <salters@lucent.com>
Date: 1999/10/09
Raw View
Siemel B. Naran wrote:

> On 08 Oct 99 01:21:02 GMT, Michel Michaud <Michel.Michaud@sympatico.ca> wrote:

> >Is the following legal?
> >
> >    int a[100];
> >    F(&a[0], &a[100]);

> The standard says this is undefined.  So it seems that an implementation
> could do array bounds checking on array types (which means that "&a[100]"
> would cause an runtime abort).  Besides, what if A::operator& is
> overloaded?

I think it may even emit a compile-time diagnostic - and perhaps should.

OTOH, there is no "A" involved, let alone A::operator&, and int [100]::operator&
(int::operator&[100] ? ) may not be overloaded (built-in type).

Michiel Salters
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Michel Michaud" <Michel.Michaud@sympatico.ca>
Date: 1999/10/08
Raw View
Suppose you have to use this function:

    void F(int* begin, int* end);

where begin-end is a usual open interval.

Is the following legal?

    int a[100];
    F(&a[0], &a[100]);

The problem is of course &a[100]. Stroustrup use this in
TC++PL3 and his "open issue" www page "confirm" it is
legal. However I've been told it is not legal C++, that
it will only be legal in C9X... Who's right?

Then suppose this (with a contiguous std::vector):

    vector<int> v(100);
    F(&v[0], &v[100]);

Clearly here there is a bigger problem because v[100]
will call vector::operator[]. This would return a
reference to an unexistent element. I think it is
clearly undefined behavior.

But what about this:

    vector<int> v(100);
    v.reserve(101); // reserve not resize
    F(&v[0], &v[100]);

Well, contrary to popular belief (even Stroustrup is
spreadind it), vector::operator[] is not an unchecked
access: it only *can* be unchecked, it is not said
anything about it (I thoroughly checked the standard).
So v[100] could fail because of bound checking.

Is this what was expected? I would think that the
standard should say that [] is unchecked. Without
that, people will start to use built-in arrays to
get performance...

Notes:
-It seems at least one implemention has a checked [].
-I know the proper call would be F(&v[0], &v[0]+100).

Michel Michaud (micm19@removethis.mail2.cstjean.qc.ca
http://www3.sympatico.ca/michel.michaud
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/10/08
Raw View
On 08 Oct 99 01:21:02 GMT, Michel Michaud <Michel.Michaud@sympatico.ca> wrote:

>Is the following legal?
>
>    int a[100];
>    F(&a[0], &a[100]);

The standard says this is undefined.  So it seems that an implementation
could do array bounds checking on array types (which means that "&a[100]"
would cause an runtime abort).  Besides, what if A::operator& is
overloaded?

--
--------------
siemel b naran
--------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/10/08
Raw View
"Michel Michaud" <Michel.Michaud@sympatico.ca> writes:

| Suppose you have to use this function:
|
|     void F(int* begin, int* end);
|
| where begin-end is a usual open interval.
|
| Is the following legal?
|
|     int a[100];
|     F(&a[0], &a[100]);
|
| The problem is of course &a[100]. Stroustrup use this in
| TC++PL3 and his "open issue" www page "confirm" it is
| legal. However I've been told it is not legal C++, that
| it will only be legal in C9X... Who's right?

C++ Standard says it is undefined.  No matter other books or
irrelevant standards might say.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1999/10/08
Raw View
Michel Michaud <Michel.Michaud@sympatico.ca> wrote

> Suppose you have to use this function:
>
>     void F(int* begin, int* end);
>
> where begin-end is a usual open interval.
>
> Is the following legal?
>
>     int a[100];
>     F(&a[0], &a[100]);
>
> The problem is of course &a[100]. Stroustrup use this in
> TC++PL3 and his "open issue" www page "confirm" it is
> legal. However I've been told it is not legal C++, that
> it will only be legal in C9X... Who's right?

This is an irritating aspect of the standard, in my view. A compiler is
allowed to do range-checking on an array index, even if that array element
isn't going to be read or written. I wish that such range-checking was
prohibited, for the case of taking the address, since it is important to be
able to take a one-past-the-end address.

I usually get around this merely by using pointer addition:

    F(a, a + 100);

or

    F(a, a + lengthof(a));

where lengthof(a) is a handy macro for (sizeof(a)/sizeof(*(a))).




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]