473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Dereferencing past-the-end iterators

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
Jul 23 '05 #1
2 8816
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

Jul 23 '05 #2
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.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Owen Funkhouser | last post by:
Maybe someone can help me out here: $name1 = "Owen"; $name2 = "Funkhouser"; for ($i = 1; $i < 3; $i++ ) { echo "<!-- name$i = $name$i -->\n"; } I'd like to see:
1
by: Larry Lindsey | last post by:
I have a linked list class that is giving me problems when I try to return the nth node. Here is what it looks like: class LinkedList{ public: ListNode getNode(int which); int getValue();...
4
by: Dale | last post by:
Hi Everyone, I've got a form that provides a pop-up calendar for users to select dates for requesting jobs to be completed. The calendar works great, but it unfortunately allows users to select...
2
by: Martin Zimmermann | last post by:
Hi. Is it allowed to dereference 'this' in a constructors initialiser list to initialize a reference? (for later use) Is this code legal or illegal? class Foo; class Bar
16
by: Michael Maes | last post by:
Hi, How would I handle Dereferencing in vb.Net 2003. Something like: Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox) This sadly won't work because a type of string...
7
by: Vivi | last post by:
Hello everybody, I'm writing a game program, and i have compilations errors.. :confused: typedef struct { struct lieu* cont; struct joueur* j;
1
by: Greg Smith | last post by:
Is there a way to disable past dates for a calendar control, or individual date for that matter? Any help is greatly appreciated.
1
by: akiko | last post by:
i am student and i must to do project about web app by Ruby on rails i can not do past upload file.i try looking code but i just meet upload file image. i want to code upload file that it must to...
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
6
by: hnshashi | last post by:
I have written kernel(2.4) module to communicate with user application using "Netlink Socket". I am getting compilation error "Dereferencing pointer to incomplete type" in kernel module for "nlh...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.