📜  运行时错误

📅  最后修改于: 2021-05-20 06:26:23             🧑  作者: Mango

运行时错误

  • 程序中的运行时错误是在成功编译程序后运行时发生的错误。
  • 运行时错误通常称为“错误” ,通常在发布软件之前的调试过程中发现。
  • 当程序发布给公众后发生运行时错误时,开发人员通常会发布补丁或旨在修复错误的小型更新。
  • 如果是本文的初学者,任何人都可以找到他们可能会遇到的问题列表。
  • 解决在线平台上的问题时,可能会遇到许多运行时错误,这些错误在附带的消息中没有明确说明。发生各种运行时错误,例如逻辑错误输入/输出错误未定义的对象错误被零除的错误等。

运行时错误的类型

  • SIGFPE: SIGFPE是浮点错误。实际上,它总是由0除以引起的。 SIGFPE错误的主要原因可能主要有以下三个:
    1. 被零除。
    2. 零模运算。
    3. 整数溢出。

    下面是说明SIGFPE错误的程序:

    C++
    // C++ program to illustrate
    // the SIGFPE error
      
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
      
        int a = 5;
      
        // Division by Zero
        cout << a / 0;
        return 0;
    }


    C++
    // C++ program to illustrate
    // the SIGBRT error
      
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // Assigning excessive memory
        int a = 100000000000;
        int* arr = new int[a];
      
        return 0;
    }


    Python
    # Python program to illustrate
    # the NZEC Error
       
    # Driver Code
    if __name__ == "__main__":
          arr = [1, 2]
       
        # Runtime Error
        # Array Index out of Bounds
        print(arr[2])


    C++
    // C++ program to illustrate
    // the SIGSEGV error
    #include 
    using namespace std;
      
    // Function with infinite
    // Recursion
    void infiniteRecur(int a)
    {
        return infiniteRecur(a);
    }
      
    // Driver Code
    int main()
    {
      
        // Infinite Recursion
        infiniteRecur(5);
    }


    输出:

  • SIGABRT:程序本身检测到错误,然后使用调用abort()函数生成此信号。标准库也使用此信号来报告内部错误。 C++中的assert()函数还使用abort()来生成此信号。

    下面是说明SIGBRT错误的程序:

    C++

    // C++ program to illustrate
    // the SIGBRT error
      
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // Assigning excessive memory
        int a = 100000000000;
        int* arr = new int[a];
      
        return 0;
    }
    

    输出:

  • NZEC:此错误表示“非零退出代码” 。对于C用户,如果main()方法没有return 0语句,则将生成此错误。Java/ C++用户抛出异常,则可能会生成此错误。以下是出现NZEC错误的可能原因:
    1. 无限递归或堆栈内存不足。
    2. 负数组索引被访问。
    3. ArrayIndexOutOfBounds异常。
    4. StringIndexOutOfBounds异常。

    下面是说明NZEC错误的程序:

    Python

    # Python program to illustrate
    # the NZEC Error
       
    # Driver Code
    if __name__ == "__main__":
          arr = [1, 2]
       
        # Runtime Error
        # Array Index out of Bounds
        print(arr[2])
    

    输出:

  • SIGSEGV:此错误是最常见的错误,被称为“分段错误”。当程序尝试访问不允许访问的内存或尝试以不允许的方式访问内存位置时,将生成该消息。导致分段错误的一些常见原因列表如下:
    1. 超出范围访问数组。
    2. 取消引用NULL指针。
    3. 取消引用释放的内存。
    4. 取消引用未初始化的指针。
    5. 不正确使用“&” (地址)和“ *” (取消引用)运算符。
    6. printf和scanf语句中的格式说明符格式不正确。
    7. 堆栈溢出。
    8. 写入只读存储器。

    下面是说明SIGSEGV错误的程序:

    C++

    // C++ program to illustrate
    // the SIGSEGV error
    #include 
    using namespace std;
      
    // Function with infinite
    // Recursion
    void infiniteRecur(int a)
    {
        return infiniteRecur(a);
    }
      
    // Driver Code
    int main()
    {
      
        // Infinite Recursion
        infiniteRecur(5);
    }
    

    输出:

避免运行时错误的方法

  • 避免使用尚未初始化的变量。这些可能在您的系统上设置为0 ,但在编码平台上未设置。
  • 检查数组元素的每一次出现,并确保它没有超出范围。
  • 避免声明过多的内存。检查问题中指定的内存限制。
  • 避免声明过多的堆栈内存。大数组应在函数外部全局声明。
  • 使用return作为结束语句。
  • 避免引用空闲内存或空指针。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。