📜  C / C++中的核心转储(分段错误)

📅  最后修改于: 2021-05-25 23:51:49             🧑  作者: Mango

核心转储/分段错误是由访问“不属于您”的内存引起的一种特定类型的错误。

  • 当一段代码试图在内存中的只读位置或已释放的内存块中执行读取和写入操作时,称为核心转储。
  • 这是表明内存损坏的错误。

常见的分段故障场景:

  • 修改字符串字面量:
    下面的程序可能会崩溃(产生分段错误),因为*(str + 1)=’n’行试图写一个只读存储器。
C
int main()
{
   char *str;
 
   /* Stored in read only part of data segment */
   str = "GfG";    
 
   /* Problem:  trying to modify read only memory */
   *(str+1) = 'n';
   return 0;
}


CPP
// C program to illustrate
// Core Dump/Segmentation fault
#include 
#include
int main(void)
{
    // allocating memory to p
    int* p = malloc(8);
    *p = 100;
     
    // deallocated the space allocated to p
    free(p);
     
    // core dump/segmentation fault
    //  as now this statement is illegal
    *p = 110;
     
    return 0;
}


CPP
// C++ program to demonstrate segmentation
// fault when array out of bound is accessed.
#include 
using namespace std;
 
int main()
{
   int arr[2];
   arr[3] = 10;  // Accessing out of bound
   return 0;
}


C
// C program to demonstrate segmentation
// fault when value is passed to scanf
#include 
 
int main()
{
   int n = 2;
   scanf("%d",n);
   return 0;
}


C
// C program to demonstrate segmentation
// fault when uninitialized pointer is accessed.
#include 
 
int main()
{
   int *p;
   printf("%d",*p);
   return 0;
}


C++14
// C++ program to demonstrate segmentation
// fault when uninitialized pointer is accessed.
#include 
using namespace std;
 
int main()
{
    int* p;
    cout << *p;
    return 0;
}
// This code is contributed by Mayank Tyagi


Abnormal termination of program.

有关详细信息,请参见C中的字符串存储

  • 访问已释放的地址:
    在下面的代码中,释放存储块后,将指针p取消引用,这是编译器所不允许的。因此,它会在运行时产生错误段错误或程序异常终止。
    例子:

CPP

// C program to illustrate
// Core Dump/Segmentation fault
#include 
#include
int main(void)
{
    // allocating memory to p
    int* p = malloc(8);
    *p = 100;
     
    // deallocated the space allocated to p
    free(p);
     
    // core dump/segmentation fault
    //  as now this statement is illegal
    *p = 110;
     
    return 0;
}

输出:

Abnormal termination of program.
  • 访问超出数组的索引范围:

CPP

// C++ program to demonstrate segmentation
// fault when array out of bound is accessed.
#include 
using namespace std;
 
int main()
{
   int arr[2];
   arr[3] = 10;  // Accessing out of bound
   return 0;
}

输出:

Abnormal termination of program.
  • scanf()使用不当:
    scanf()函数期望将变量的地址作为输入。在此程序中n
    值2并假定其地址为1000。如果将n传递给scanf(),则从STDIN提取的输入将放置在无效内存2中,该内存应改为1000。这是导致Seg错误的内存损坏。

C

// C program to demonstrate segmentation
// fault when value is passed to scanf
#include 
 
int main()
{
   int n = 2;
   scanf("%d",n);
   return 0;
}

输出:

Abnormal termination of program.
  • 堆栈溢出
    这不是与指针相关的问题,即使代码可能没有单个指针。这是因为递归函数被重复调用,这耗尽了所有堆栈内存,从而导致堆栈溢出。堆栈上的内存不足也是一种内存损坏类型。可以通过具有从递归函数返回的基本条件来解决该问题。
  • 取消引用未初始化的指针
    指针在访问前必须指向有效内存。

C

// C program to demonstrate segmentation
// fault when uninitialized pointer is accessed.
#include 
 
int main()
{
   int *p;
   printf("%d",*p);
   return 0;
}

C++ 14

// C++ program to demonstrate segmentation
// fault when uninitialized pointer is accessed.
#include 
using namespace std;
 
int main()
{
    int* p;
    cout << *p;
    return 0;
}
// This code is contributed by Mayank Tyagi
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。