STL safety issue

J

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
 
N

Noah Roberts

JDT said:
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.
 
T

Thomas Tutone

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
 
T

Thomas Tutone

I said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top