Hello experts!
I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I find is wrong as I
believe.
An array index that is out of bounds in an array will not generate an
exception but will
generate the out_of_range exception if used in a vector(that is part of the
standard library)
Is this really true that out_of_range exception is generated if used in a
vector(that is part of the standard library)
I tried this but no exception was generated in this piece of code.
#inlude <vector>
#include <iostream>
int main()
{
try
{
vector<int> v(3);
cout << v[0];
cout << v[1];
cout << v[2];
cout << v[99];
}
catch (exception &e)
{
cout << exception occurred" << endl;
}
}
Many thanks
//Tony 6 3934
Tony Johansson wrote: Is this really true that out_of_range exception is generated if used in a vector(that is part of the standard library)
Nope. That's what happens if you use the member function
vector::at(size_type) instead of its operator[]. Another fundamental
mistake in that book.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Tony Johansson skrev: Hello experts!
I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I find is wrong as I believe.
Just one more reason to drop that book. It is obviously worthless. An array index that is out of bounds in an array will not generate an exception
This might be correct in practice, although the lawyer would note that
it is undefined behaviour in which case anything could happen. but will generate the out_of_range exception if used in a vector(that is part of the standard library)
If you use "at", you will get the exception. Using operator[] it is
again undefined behaviour, but this time it is more likely that the
exception will be thrown. An implementation is allowed to check if you
index outside of the vector and throw - and there are libraries that
will do so in a debug build. Is this really true that out_of_range exception is generated if used in a vector(that is part of the standard library)
I tried this but no exception was generated in this piece of code. #inlude <vector> #include <iostream> int main() { try { vector<int> v(3); cout << v[0]; cout << v[1]; cout << v[2]; cout << v[99]; } catch (exception &e) { cout << exception occurred" << endl; } }
It is okay not to throw - again: it is undefined behaviour. Many thanks
//Tony
/Peter
Tony Johansson wrote: ...
An array index that is out of bounds in an array will not generate an exception but will generate the out_of_range exception if used in a vector(that is part of the standard library)
...
#inlude <vector> #include <iostream> int main() { try { vector<int> v(3); cout << v[0]; cout << v[1]; cout << v[2]; cout << v[99]; } catch (exception &e) { cout << exception occurred" << endl; } }
Hi,
out-of range check is only performed if you use at() to access elements
of vector, not with [], however.
Steffen
Steffen skrev:
[snip] Hi,
out-of range check is only performed if you use at() to access elements of vector, not with [], however.
That is not entirely true. An implementation is allowed to do
out-of-range checks also with operator[]. It is not required to,
however.
/Peter Steffen
<pe***************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... Steffen skrev:
[snip]
Hi,
out-of range check is only performed if you use at() to access elements of vector, not with [], however. That is not entirely true. An implementation is allowed to do out-of-range checks also with operator[]. It is not required to, however.
However, safe (in the sense that it throws exceptions when needed) STL
containers are one of the main sell points of the proposed C++0x...I'd
heard.
Ben /Peter Steffen
benben wrote: <pe***************@gmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com... Steffen skrev:
out-of range check is only performed if you use at() to access elements of vector, not with [], however. That is not entirely true. An implementation is allowed to do out-of-range checks also with operator[]. It is not required to, however.
However, safe (in the sense that it throws exceptions when needed) STL containers are one of the main sell points of the proposed C++0x...I'd heard.
I'm not sure what you are saying here. STL containers throw exceptions
when they are are specified to. Hence vector::at() throws if out of
range, vector::operator[] has no defined behaviour when out of range.
Note that a "selling point" of the STL was efficiency.
--
Nick Keighley This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ricardo Jasinski |
last post by:
Hello people!!
I'm trying to improve an application I recently developed, in order to
release its version 2.0. One of my concerns is to make it more robust,
stable and with error-recovering...
|
by: Zork |
last post by:
Hi,
I am trying to stop object creation (in this case ill use a ball as the
object) via use of exceptions. In essence, if the ball does not have an
owner, I do not want the ball object created....
|
by: Miyra |
last post by:
Hi. I'm working with an app that uses exceptions for control flow.
These are code blocks where exceptions are thrown/caught regularly. A
couple hundred exceptions occur per hour and they're caught...
|
by: Digital Puer |
last post by:
Hi, I'm coming over from Java to C++, so please bear with me.
In C++, is there a way for me to use exceptions to catch
segmentation faults (e.g. when I access a location off
the end of an array)?...
|
by: Mark P |
last post by:
I'm implementing an algorithm and the computational flow is a somewhat
deep. That is, fcn A makes many calls to fcn B which makes many calls
to fcn C, and so on. The return value of the outermost...
|
by: Anonieko |
last post by:
Understanding and Using Exceptions
(this is a really long post...only read it if you (a) don't know what
try/catch is OR (b) actually write catch(Exception ex) or catch{ })
The first thing I...
|
by: arrastogi71 |
last post by:
Kindly tell me the costs and benefits of using exceptions?
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |