exception error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main(){
	char _str[] = "one world,one dream";
	cout << "The length of the string _str is:" << strlen(_str) << endl;
	try
	{
		_str[strlen(_str) + 1] = 'c';	
                }catch(...)
	{
		cout << "an exception has occured!" << endl;
	}
	cout << _str << endl;
	return 0;
} 

the code border has accessed invalid memory,and i think it should throw an exception,but the sentence

 
cout << "an exception has occured!" << endl;


didn't act actually,it works very well,by the way,i compile this code by vc++ 6.0, any help is seriously appreciated!
Last edited on
Yes, you accessed beyond the end of the array. What you did is stomp memory on your program stack. On most architectures the stack grows downwards in memory, which means that you stomped one byte of allocated stack space.

Regardless, you won't get an exception on a memory stomp. Depending on your OS you'll either get an access violation or a segmentation fault, but not an exception.
It varies.
You're depending on non-standard behavior. The compiler, the system, and the execution environment play a very big role in controlling what will and what won't throw an exception. Generally, an exception is only thrown if the program tries to read or write outside of its segment (a segmentation fault). This is of course not always the case.
thank you friends,I know,i will hava a think.
Topic archived. No new replies allowed.