[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: while( *it == value && it != v.end() )
From: |
mathieu |
Subject: |
Re: while( *it == value && it != v.end() ) |
Date: |
14 Feb 2007 10:00:50 -0800 |
User-agent: |
G2/1.0 |
On Feb 13, 10:07 pm, Paul Pluzhnikov <ppluzhnikov-...@charter.net>
wrote:
> "mathieu" <mathieu.malate...@gmail.com> writes:
> > Just to be sure I am double-checking here.
> > Is there a way to make gcc produce a warning when a code is
> > dereferencing an end iterator ? Or does this involve too much static
> > analysis ?
>
> The latter.
> You can however ask newer versions of g++ to catch this at runtime:
>
> $ cat junk.cc
> #include <vector>
>
> typedef std::vector<int> VI;
>
> int main()
> {
> VI v(10);
> VI::iterator it = v.begin();
> while (*it == 0 && it != v.end()) ++it;
> return 0;
>
> }
>
> $ /usr/local/gcc-3.4.6/bin/g++ -g junk.cc && ./a.out && echo ok
> ok
> $ /usr/local/gcc-3.4.6/bin/g++ -g junk.cc -D_GLIBCXX_DEBUG && ./a.out
> /usr/local/gcc-3.4.6/bin/../lib/gcc/i686-pc-linux-gnu/3.4.6/../../../../include/c++/3.4.6/debug/safe_iterator.h178:
> error: attempt to dereference a past-the-end iterator.
>
> Objects involved in the operation:
> iterator "this" @ 0x0xbfffdc60 {
> type =
> N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPiN10__gnu_norm6vectorIiSaIiEEEEEN15__gnu_debug_def6vectorIiS6_EEEE
> (mutable iterator);
> state = past-the-end;
> references sequence with type `N15__gnu_debug_def6vectorIiSaIiEEE' @
> 0x0xbfffdc60}
>
> Aborted (core dumped)
sweet !
Thanks,
-M