Topic: STL Container classes and member methods


Author: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Mon, 11 Sep 2000 18:25:32 GMT
Raw View
Hi,
In article <86its56zti.fsf@gabi-soft.de>,
  kanze@gabi-soft.de wrote:
> Typically, what the programmer "knows" ends up being false.

  for (std::vector<T>::size_type i = 0; i < cont.size(); ++i)
    do_something_with(cont[i]);

If the programmer's knowledge about 'cont[i]' being available ever
becomes wrong we are in real trouble...

> Having the obvious operator forgo bounds checking, and treating
> bounds checking as a special case, is simply a major design error.

I would say that there is potential for value added implementations,
not a design error. If enough users consider it a problem that the STL
containers are not range checked, they would all buy the implementation
doing the checks. Strangely enough I doubt that there are many
customers who would accept a speed reduction being forced upon them,
however.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: kanze@gabi-soft.de
Date: Sun, 10 Sep 2000 06:56:01 GMT
Raw View
Dietmar Kuehl <dietmar_kuehl@yahoo.com> writes:

|>  In article <39B58EDF.EB4D7CA6@kscable.com>,
|>    "John M. Resler" <jmresler@kscable.com> wrote:
|>  > As many of you may know, container_type<T>::operator[]() is
|>  > overloaded for many of the containers. I expected the vector class =
to
|>  > throw an exception (e.g. out_of_range ) if an index out of range va=
lue
|>  > was requested.

|>  Typical uses of the standard containers *know* that they will not
|>  access elements outside the bounds of the vector.

Typically, what the programmer "knows" ends up being false.  Having the
obvious operator forgo bounds checking, and treating bounds checking as
a special case, is simply a major design error.

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ 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: kanze@gabi-soft.de
Date: Sun, 10 Sep 2000 06:56:22 GMT
Raw View
Matthew Austern <austern@research.att.com> writes:

|>  But certainly Francis's advice is correct: if you want range
|>  checking, use at() instead of operator[]().

The problem is that you always want it, until profiling shows a problem.
Not wanting range checking is excedingly rare, especially as in the most
frequent cases the compiler can optimize it out anyway.  And always
using at() sort of defeats the idea that std::vector should replace all
uses of the C style arrays.

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ 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: Dennis Yelle <dennis51@jps.net>
Date: Sun, 10 Sep 2000 20:22:02 GMT
Raw View
kanze@gabi-soft.de wrote:
>
> Matthew Austern <austern@research.att.com> writes:
>
> |>  But certainly Francis's advice is correct: if you want range
> |>  checking, use at() instead of operator[]().
>
> The problem is that you always want it, until profiling shows a problem.

Maybe that is true for you, but it is not true for all of us.

> Not wanting range checking is excedingly rare, especially as in the most
> frequent cases the compiler can optimize it out anyway.

This has not been established, and, as of Sept 2000, is still false.
Someday it may be true, but not today.

> And always
> using at() sort of defeats the idea that std::vector should replace all
> uses of the C style arrays.

I would say that a vector that always checked the
value of the subscript would ensure that vector would
never replace all uses of C style arrays.

As Stroustrup clearly says in his book,
a safe vector can be built from a fast vector,
but a fast vector cannot be built from a safe vector.

So the proper primitive is the fast un-range-checked vector.

On page 53 of TC++PL3 Stroustrup shows how to build a
range checked vector, which he calls Vec from std::vector
with 7 non-blank lines of code.  If you don't already use
something like that, you don't want it very much.

Dennis Yelle
--
I am a computer programmer and I am looking for a job.
There is a link to my resume here:
http://table.jps.net/~vert/

---
[ 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: "John M. Resler" <jmresler@kscable.com>
Date: Wed, 6 Sep 2000 11:29:51 GMT
Raw View
This is a multi-part message in MIME format.
--------------A3D61D658782C29CC55287AD
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I have been studying the STL and it's associated container classes.  As
many of you may know,
container_type<T>::operator[]() is overloaded for many of the
containers. I expected the vector class to
throw an exception (e.g. out_of_range ) if an index out of range value
was requested.
For example, I tried the following code :

#include <iostream.h>
#include <vector.h>

int
main()
{
int a[] = {0,1,2,3,4,5};
vector<int> vec(a, (a + 6));

try {
        for (int i = 0; i < 10; i++)
            cout << vec[ i ] << endl;
    }

catch(...) {
        cout << endl <<"Exception caught." << endl;
    }
return 0;
}

And yet no exceptions are thrown on the out of bounds [] access. I do
get a segmentation fault though.
Do I need to include another file to get the exception thrown?  Is the
T
vector<t>::operator[](int)
not overloaded to throw an exception? Can anyone explain the thinking
behind this? I'd appreciate it.

Sincerely,

John Resler
jmresler@kscable.com

--
***********************************************************************
    Beware of bugs in the above code; I have only proved it
    correct, not tried it.
                -- Donald Knuth
***********************************************************************



--------------A3D61D658782C29CC55287AD
Content-Type: text/x-vcard; charset=us-ascii;
 name="jmresler.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for John M. Resler
Content-Disposition: attachment;
 filename="jmresler.vcf"

begin:vcard
n:Resler;John Michael
tel;home:(316) 788-7998
x-mozilla-html:TRUE
url:http://home.kscable.com/TheTree
org:Student;Computer Science
adr:;;901 North Westview Drive;Derby;Kansas;67037;USA
version:2.1
email;internet:jmresler@kscable.com
x-mozilla-cpt:;0
fn:John Michael Resler
end:vcard

--------------A3D61D658782C29CC55287AD--

---
[ 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 Furnanz <pfurnanz+usenet@synopsys.com>
Date: Wed, 6 Sep 2000 14:17:26 GMT
Raw View
Take a look at the at() method.  The operator[] method is faster, and
skips the range check.

>>>>> "John" == John M Resler <jmresler@kscable.com> writes:

 John> And yet no exceptions are thrown on the out of bounds [] access. I do
 John> get a segmentation fault though.
 John> Do I need to include another file to get the exception thrown?  Is the
 John> T
 John> vector<t>::operator[](int)
 John> not overloaded to throw an exception? Can anyone explain the thinking
 John> behind this? I'd appreciate it.

---
[ 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: "Sebastian Moleski" <sebmol@gmx.net>
Date: Wed, 6 Sep 2000 15:41:25 GMT
Raw View
Hi,
"John M. Resler" <jmresler@kscable.com>:
> I have been studying the STL and it's associated container classes.
That's a good thing to do.

> As many of you may know, container_type<T>::operator[]() is
>overloaded for many of the containers. I expected the vector class
> to throw an exception (e.g. out_of_range ) if an index out of range value
was requested.
It doesn't. To have an exception thrown, use at().

> For example, I tried the following code :
>
> #include <iostream.h>
> #include <vector.h>
These are not STL headers. The standard headers are <iostream> and <vector>.
What compiler are you using?

> int main()
> {
> int a[] = {0,1,2,3,4,5};
> vector<int> vec(a, (a + 6));
vector resides in the std namespace, thus "std::vector<int>"
>
> try {
>         for (int i = 0; i < 10; i++)
>             cout << vec[ i ] << endl;
>     }
>
> catch(...) {
>         cout << endl <<"Exception caught." << endl;
>     }
> return 0;
> }
...
> Do I need to include another file to get the exception thrown?  Is the
> T vector<t>::operator[](int)
> not overloaded to throw an exception? Can anyone explain the thinking
> behind this? I'd appreciate it.
It's performance reasons. Most of the time you know what are you doing so
that an extra check for valid ranges is unnecessary overhead. If you need
that check because you can't control the passed index yourself, use at().

HTH,

Sebastian Moleski


---
[ 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: firstian@nospam.bellatlantic.net (Joe Chan)
Date: Wed, 6 Sep 2000 21:15:48 GMT
Raw View
The STL containers are designed to not do range checking for performance
reasons. A lot of the times the containers are used with iterators,
which implicitly limits the range, hence lessen the needs for range
checking. You can always subclass vector to provide range checking
though.

John M. Resler <jmresler@kscable.com> wrote:

> This is a multi-part message in MIME format.
> --------------A3D61D658782C29CC55287AD
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
> I have been studying the STL and it's associated container classes.  As
> many of you may know,
> container_type<T>::operator[]() is overloaded for many of the
> containers. I expected the vector class to
> throw an exception (e.g. out_of_range ) if an index out of range value
> was requested.
> For example, I tried the following code :
>
> #include <iostream.h>
> #include <vector.h>
>
> int
> main()
> {
> int a[] = {0,1,2,3,4,5};
> vector<int> vec(a, (a + 6));
>
> try {
>         for (int i = 0; i < 10; i++)
>             cout << vec[ i ] << endl;
>     }
>
> catch(...) {
>         cout << endl <<"Exception caught." << endl;
>     }
> return 0;
> }
>
> And yet no exceptions are thrown on the out of bounds [] access. I do
> get a segmentation fault though.
> Do I need to include another file to get the exception thrown?  Is the
> T
> vector<t>::operator[](int)
> not overloaded to throw an exception? Can anyone explain the thinking
> behind this? I'd appreciate it.
>
> Sincerely,
>
> John Resler
> jmresler@kscable.com
>
> --
> ***********************************************************************
>     Beware of bugs in the above code; I have only proved it
>     correct, not tried it.
>                 -- Donald Knuth
> ***********************************************************************
>
>
>
> --------------A3D61D658782C29CC55287AD
> Content-Type: text/x-vcard; charset=us-ascii;
>  name="jmresler.vcf"
> Content-Transfer-Encoding: 7bit
> Content-Description: Card for John M. Resler
> Content-Disposition: attachment;
>  filename="jmresler.vcf"
>
> begin:vcard
> n:Resler;John Michael
> tel;home:(316) 788-7998
> x-mozilla-html:TRUE
> url:http://home.kscable.com/TheTree
> org:Student;Computer Science
> adr:;;901 North Westview Drive;Derby;Kansas;67037;USA
> version:2.1
> email;internet:jmresler@kscable.com
> x-mozilla-cpt:;0
> fn:John Michael Resler
> end:vcard
>
> --------------A3D61D658782C29CC55287AD--
>
> ---
> [ 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              ]


--
Joe Chan

Remove "nospam" to get my address.

---
[ 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: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Wed, 6 Sep 2000 21:15:36 GMT
Raw View
Hi,
In article <39B58EDF.EB4D7CA6@kscable.com>,
  "John M. Resler" <jmresler@kscable.com> wrote:
> As many of you may know, container_type<T>::operator[]() is
> overloaded for many of the containers. I expected the vector class to
> throw an exception (e.g. out_of_range ) if an index out of range value
> was requested.

Typical uses of the standard containers *know* that they will not
access elements outside the bounds of the vector. Thus, it would add
extra costs to these application if the STL containers would be
required to check for out of bounds access. Correspondingly,
'std::vector::operator[]()' is not required to throw an exception if an
out of bounds access is made. It is just undefined behavior. That is,
an implementation might choose to do the check and throw an exception.

If you want range checks when accessing 'std::vector' element, use the
method 'at()'.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 6 Sep 2000 21:16:39 GMT
Raw View
In article <39B58EDF.EB4D7CA6@kscable.com>, John M. Resler
<jmresler@kscable.com> writes
>I have been studying the STL and it's associated container classes.  As
>many of you may know, container_type<T>::operator[]() is overloaded for
>many of the containers. I expected the vector class to throw an
>exception (e.g. out_of_range ) if an index out of range value
>was requested.
Well your expectations are erroneous. the index operator is not range
checked. If you want range checking you must use at().



Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: James Kuyper <kuyper@wizard.net>
Date: Thu, 7 Sep 2000 00:44:38 GMT
Raw View
Joe Chan wrote:
>
> The STL containers are designed to not do range checking for performance
> reasons. A lot of the times the containers are used with iterators,
> which implicitly limits the range, hence lessen the needs for range
> checking. You can always subclass vector to provide range checking
> though.

I suppose, but it would be simpler to just use at().

---
[ 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: brangdon@cix.compulink.co.uk
Date: Thu, 7 Sep 2000 08:26:09 GMT
Raw View
In article <39B58EDF.EB4D7CA6@kscable.com>, jmresler@kscable.com (John M.
Resler) wrote:
> Is the T vector<t>::operator[](int) not overloaded to throw
> an exception?

Correct. According to the standard, it is undefined behaviour. There is
another function, at(), which is guaranteed to range-check.


> Can anyone explain the thinking behind this?

The range checks take time, so there is a speed/safety tradeoff here.
Different implementations may reasonably vary. For example, we might want
range checks when debugging but prefer speed when _NDEBUG is defined.

Personally I think it is the wrong way around. I'd rather Operator[]() be
the safe accessor, because it is the "default", most commonly used, and
at() be the fast but dangerous version for those rare occasions when
profiling has shown the range checks are a significant bottle-neck.

Some people justify the choice on the grounds that plain C-arrays don't
throw exceptions and "therefore" vectors shouldn't either.


> Do I need to include another file to get the exception thrown?

You need a different implementation of STL, one which emphasises safety
even at the expense of speed. Try searching for "safestl".

  Dave Harris, Nottingham, UK | "Weave a circle round him thrice,
      brangdon@cix.co.uk      |   And close your eyes with holy dread,
                              |  For he on honey dew hath fed
 http://www.bhresearch.co.uk/ |   And drunk the milk of Paradise."

---
[ 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              ]