Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dereferencing past-the-end iterators

Reply
Thread Tools

Dereferencing past-the-end iterators

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      07-22-2005
Hello,

I was wondering, does dereferencing past-the-end
iterators yield undefined behavior? Especially, is
the result of calling an STL algorithm on an empty
range undefined?

For example (pseudocode):

begin = find(...);
end = find(...);

sort(begin, find, pred);

If begin is a past-the-end iterator, is the call
to sort() an unsafe operation? Is it good practice
to check iterators for being valid before passing
them to an STL algo?

Regards,
Matthias
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      07-22-2005
Matthias Kaeppler wrote:

> I was wondering, does dereferencing past-the-end
> iterators yield undefined behavior?


Yes.

> Especially, is
> the result of calling an STL algorithm on an empty
> range undefined?


No, that's well defined. It doesn't do anything. In particular,
nothing gets dereferenced, so there's no undefined behavior.

> For example (pseudocode):
>
> begin = find(...);
> end = find(...);
>
> sort(begin, find, pred);
>
> If begin is a past-the-end iterator


By "past-the-end iterator," I assume you in fact mean an iterator equal
to end(). Given that it's name is "end()," it's confusing for you to
refer to it as "past-the-end."

> , is the call
> to sort() an unsafe operation?


No, it's safe, because nothing gets dereferenced if begin==end.

> Is it good practice
> to check iterators for being valid before passing
> them to an STL algo?


What do you mean by "valid"? An iterator equal to end() is valid, but
you can't dereference it. And end() is frequently the second iterator
passed to an algorithm. E.g.:

std::sort(seq.begin(), seq.end());

If seq is empty, then begin()==end(), there's nothing to sort, and
nothing gets dereferenced by the call to sort(). You don't have to
check for that.

Best regards,

Tom

 
Reply With Quote
 
 
 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      07-22-2005
Thomas Tutone wrote:
> If seq is empty, then begin()==end(), there's nothing to sort, and
> nothing gets dereferenced by the call to sort(). You don't have to
> check for that.


Alright, that's what I was worried about. Thanks Tom.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
Dereferencing ? Joe McGuckin Perl 1 11-28-2003 10:51 AM
Dereferencing a pointer within a function Larry Lindsey C++ 1 09-24-2003 03:42 PM
dereferencing char array as int array J. Campbell C++ 4 07-17-2003 05:56 PM



Advertisments