Re: Bound Checking Cross Platform
On Sep 22, 1:23 pm, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
On Sep 22, 5:45 am, Goran <goran.pu...@gmail.com> wrote:
On Sep 22, 5:25 am, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
at() function of vector has bound checking. It does
include _SCL_SECURE and _SCL_SECURE_VALIDATE macros. They
are Microsoft specification. What about other platforms
such as Mac OS X and Linux?
On other platforms, you could use STLPort, which has support
for bounds checking (look for _STLP_DEBUG), and with gcc,
you could use _GLIBCXX_DEBUG.
I'd be wary of using bounds (and iterator) checking in
general in NDEBUG, though. I personally try to crash my code
as fast as possible upon range and iterator errors in it
;-).
static void _Xran()
{ // report an out_of_range error
_THROW(out_of_range, "invalid vector<T> subscript");
}
I copied some functions from vector header.
I wouldn't even look at the <vector> header if I were you. It's
part of the implementation, and can do all sorts of things that
you can't, or shouldn't, because it collaborates with the
compiler.
Is this function above
the Standard C++ Library to trigger an exception?
Certainly not. The very name says that it's something internal
to the implementation, neither standard nor, unless it is
explicitly documented by the implementation, for your use.
If _Xran function is called, it uses MessageBoxW(=85) in
Microsoft specification to trigger an exception. Mac OS X and
Linux may use different API function rather than
MessageBoxW(=85).
Except that there almost certainly isn't an _Xran function
anywhere to be found in Mac OS or Linux. For that matter,
I can't find it in Windows, either, if I only look in the places
I'm suppsed to be looking in (the library documentation).
reference at(size_type _Pos)
{ // subscript mutable sequence with checking
if (size() <= _Pos)
_Xran();
return (*(begin() + _Pos));
}
at() function does bound checking and throws an exception.
reference operator[](size_type _Pos)
{ // subscript mutable sequence
#if _HAS_ITERATOR_DEBUGGING
if (size() <= _Pos)
{
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
#endif /* _HAS_ITERATOR_DEBUGGING */
_SCL_SECURE_VALIDATE_RANGE(_Pos < size());
return (*(_Myfirst + _Pos));
}
The above is all part of the implementation. It's totally
irrelevant to using or understanding the function. When using
a library function, you don't read the code, you read the
documentation.
Notice _HAS_ITERATOR_DEBUGGING symbol. It is Microsoft
Specification. Do Mac OS X and Linux have different symbol
name?
Do they even have a symbol which does the same thing?
If I want to create my own operator[], it needs to be portable across
platforms. If Win32 macro is tested, then _HAS_ITERATOR_DEBUGGING
symbol is chosen, or Mac OS X or Linux is tested, then different
symbol name will be given.
You can't create your own operator[] for vector. For your own
classes, you shouldn't use anything in the implementation (which
might change from one version to the next).
--
James Kanze