
在这里,我们将看到一些C和C++代码,并尝试猜测结果。这些代码将生成一些运行时错误。
1. 除以零的错误是未定义的。
示例代码
#includeusing namespace std; int main() { int x = 10, y = 0; int z = x / y; cout << "Done" << endl; }
Output
Runtime error for divide by zero operation
2. Trying to use uninitialized variable.
Example Code
#includeusing namespace std; int main() { bool x; if(x == true) cout << "true value"; else cout << "false value"; }
Output
false value (This may differ in different compilers)
3. Trying to access null pointer values.
立即学习“C++免费学习笔记(深入)”;
95Shop可以免费下载使用,是一款仿醉品商城网店系统,内置SEO优化,具有模块丰富、管理简洁直观,操作易用等特点,系统功能完整,运行速度较快,采用ASP.NET(C#)技术开发,配合SQL Serve2000数据库存储数据,运行环境为微软ASP.NET 2.0。95Shop官方网站定期开发新功能和维护升级。可以放心使用! 安装运行方法 1、下载软件压缩包; 2、将下载的软件压缩包解压缩,得到we
Example Code
#includeusing namespace std; int main() { int *ptr = NULL; cout << "The pointer value is: " << *ptr; }
Output
Runtime error for accessing null pointer values
4. Trying to access null pointer values.
Example Code
#includeusing namespace std; int main() { int array[10]; for(int i = 0; i<=10; i++) { cout << array[i] << endl; } }
Output
Runtime error for accessing item out of bound. Some compiler may return some arbitrary value, not return any error
5. 超出有符号整数的限制。
示例代码
#includeusing namespace std; int main() { int x = INT_MAX; cout << "x + 1: " << x + 1; }
Output
x + 1: -2147483648 circulate to the minimum number of signed int
6. 尝试更改字符串字面值中的某些字符。
示例代码
#includeusing namespace std; int main() { char *str = "Hello World"; str[2] = 'x'; cout << str; }
Output
Runtime error because we are trying to change the value of some constant variables.










