Topic: try / catch problem
Author: "RL" <vlad3@total.net>
Date: 1999/09/29 Raw View
// here's the code
#include <iostream>
int main()
{
char it1[10];
char it2[10];
char it3[200];
try{
it1[200]='m';
it2[200]='e';
it3[800]='r';
}
catch( ... )
{
std::cout<<"Error"<<std::endl;
return 1;
}
return 0;
}
// the question
Why the code is execute from start to line
char it2[200];
without going inside the catch block and when the code is at
char it3[800];
the code go in the catch block???????
I think when we put a value somewhere outside the array it's not legal
char it1[10];
it1[11] = '2';
is illegal but my programs work without crash ! ( without going into catch
block )
WHY ???
Did i miss something ?
Thanks.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/09/30 Raw View
In article <9OuI3.33758$Hb3.111380@news.total.net>, RL <vlad3@total.net> wrote:
>I think when we put a value somewhere outside the array it's not legal
>char it1[10];
>it1[11] = '2';
>is illegal but my programs work without crash ! ( without going into catch
>block )
>
>WHY ???
>
>Did i miss something ?
This invokes undefined behavior. Implementations are not required to
detect undefined behavior, they can have any possible result. It is well
known (but obviously not by you) that C and C++ do not require array bounds
checking, and most implementations do not do it.
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/09/30 Raw View
RL wrote:
>
> // here's the code
> #include <iostream>
>
> int main()
> {
> char it1[10];
> char it2[10];
> char it3[200];
>
> try{
> it1[200]='m';
> it2[200]='e';
> it3[800]='r';
> }
> catch( ... )
> {
> std::cout<<"Error"<<std::endl;
> return 1;
> }
>
> return 0;
> }
>
> // the question
>
> Why the code is execute from start to line
> char it2[200];
> without going inside the catch block and when the code is at
> char it3[800];
> the code go in the catch block???????
>
> I think when we put a value somewhere outside the array it's not legal
> char it1[10];
> it1[11] = '2';
> is illegal but my programs work without crash ! ( without going into catch
> block )
>
> WHY ???
>
> Did i miss something ?
I'm guessing that you're expecting an exception to be thrown when you
violate the boundaries of the arrays you've defined. That's not the way
it works. What you've done is illegal, allowing what the standard calls
"undefined behavior". There is literally no limit to what can go wrong
as a result. Running your program could format the hard disk, burn out
your monitor, send the launch codes to start WW3, or cause your program
to abort with a memory segment violation, or have no visible effect
whatsoever. You've been "lucky" - you apparently got the last of those
options.
What you want is called "bounds-checked access". In C/C++ that's not
provided by arrays. It is provided in C++ by some of the standard
library containers. Specifically, std::basic_string<>, std::vector<>,
and std::deque<> all provide member functions named at(), which
throw(std::out_of_range) if you pass them an index that is out of range.
However, they all provide faster but unsafe unchecked access through
operator[].
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]