By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

STL safety issue

P: n/a
JDT
Hi,

In the following code, is the last line safe? Do I have to check
whether p is a non-NULL pointer? I wonder if all similar STL algorithms
such as min_element, find, etc always return a non-NULL pointer (i.e. a
pointer pointing to the one after the last element) when they fail to
find what they want to find. Please advise. Thanks for your help.

int m[6];
....
int *p = std:::max_element(m, m+6);
if (p != m+6) // found something
x = *p;

Tony
Mar 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JDT wrote:
Hi,

In the following code, is the last line safe? Do I have to check
whether p is a non-NULL pointer? I wonder if all similar STL algorithms
such as min_element, find, etc always return a non-NULL pointer (i.e. a
pointer pointing to the one after the last element) when they fail to
find what they want to find. Please advise. Thanks for your help.

int m[6];
...
int *p = std:::max_element(m, m+6);
if (p != m+6) // found something
x = *p;

Tony
All algorithms return end if they don't find what they are supposed to
find. Your p != m+6 is that check.

On a side note, I don't think it's necessary since max_element should
return something valid...it's not searching for the occurance of some
specific data point but for whatever it decides is the biggest. Only
time it could return end is if begin == end and in this case that is
impossible since end = begin + 6.
Mar 14 '07 #2

P: n/a
On Mar 14, 3:50 pm, JDT <jdt_yo...@yahoo.comwrote:
In the following code, is the last line safe?
Yes.
Do I have to check
whether p is a non-NULL pointer?
No.
I wonder if all similar STL algorithms
such as min_element, find, etc always return a non-NULL pointer (i.e. a
pointer pointing to the one after the last element) when they fail to
find what they want to find.
Since those algorithms return an iterator in the range specified, they
could never return a null pointer unless a null pointer was an
interator the specified range. Which should ordinarily be the case.
int m[6];
...
int *p = std:::max_element(m, m+6);
if (p != m+6) // found something
max_element returns an iterator to the largest element in the range.
By definition, it can never be equal to m+6, which is one past the end
of the range. You are thinking, perhaps, of something like std::find.
Best regards,

Tom
Mar 14 '07 #3

P: n/a
I wrote:
Since those algorithms return an iterator in the range specified, they
could never return a null pointer unless a null pointer was an
interator the specified range. Which should ordinarily be the case.
I meant, "Which should ordinarily _not_ be the case."
>
int m[6];
...
int *p = std:::max_element(m, m+6);
if (p != m+6) // found something

max_element returns an iterator to the largest element in the range.
By definition, it can never be equal to m+6, which is one past the end
of the range. You are thinking, perhaps, of something like std::find.

Best regards,

Tom

Mar 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.