[openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: "Bill Beebe" <wbeebe@xxxxxxxxxx>
- To: <openbeos@xxxxxxxxxxxxx>
- Date: Sat, 27 Oct 2001 21:24:29 -0700
After writing the code snippet, it just dawned on me that the code is
error-prone. The issue is size. If it's zero, then the assignment to max_val
is going to fail. Depending on the type, it will be everything from a silent
error to a core dump. Here's an example of what we all need to do to code
defensively
template<typename T>
int maxElement(const T &array[], int size)
{
int
idx = 0,
max_idx = 0;
if (not size)
{
// log a warning
}
else
{
for (idx = 1; idx < size; ++idx)
{
if (array[max_idx] < array[idx])
{
max_idx = idx;
}
}
return max_idx;
}
I've now returned the index where the array's maximum value occurs rather than
the maximum value itself. I can then test the return to see if it's zero or
not, and get the value myself accordingly. I've passed the array of type T by
const reference rather than by value, which keeps me from expensively
recreating the array of elements. Since I'm just shuffling the index around I
don't have to have an internal copy of T (causing more temporaries to be
created then disposed). This is a performance and resource usage issue.
- Follow-Ups:
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe
- References:
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Daniel Reinhold
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe
Other related posts:
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Daniel Reinhold
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe