Advertisement

C++程序崩溃的原因

阅读量:

导致C++程序发生崩溃的主要因素归纳如下:

  1. 无效指针的使用
复制代码
 void emptyPointer()

    
 {
    
 	cout << __FUNCTION__ << " " << __LINE__ << endl;
    
 	int* p = NULL;
    
 	*p = 5;
    
 	cout << *p << endl;
    
 }
    
    
    
    

2. 数组边界越界

复制代码
 void ArrayBounds()

    
 {
    
 		cout << __FUNCTION__ << " " << __LINE__ << endl;
    
 		int array[5] = { 0 };
    
 		for (int i = 0; i < 5; ++i)
    
 		{
    
 			array[i] = i;
    
 		}
    
 		for (int i = 0; i < 6; ++i)
    
 		{
    
 			cout  << array[i] <<endl;
    
 		}
    
 }
    
    
    
    

3. 零作为除数的情况

复制代码
 void DivideByZero()

全部评论 (0)

还没有任何评论哟~