什么是分段故障?它在C和C++中不同吗?分段错误和悬挂指针是如何关联的?


当前回答

分段错误的简单含义是,您试图访问一些不属于您的内存。当我们尝试在只读内存位置读取和/或写入任务或尝试释放内存时,会发生分段错误。换句话说,我们可以将其解释为某种内存损坏。

下面我提到了程序员所犯的导致分段错误的常见错误。

以错误的方式使用scanf()(忘记放&)。

int num;
scanf("%d", num);// must use &num instead of num

以错误的方式使用指针。

int *num; 
printf("%d",*num); //*num should be correct as num only
//Unless You can use *num but you have to point this pointer to valid memory address before accessing it.

修改字符串文字(指针尝试写入或修改只读内存。)

char *str;  

//Stored in read only part of data segment
str = "GfG";      

//Problem:  trying to modify read only memory
*(str+1) = 'n';

尝试通过已释放的地址进行访问。

// allocating memory to num 
int* num = malloc(8); 
*num = 100; 

// de-allocated the space allocated to num 
free(num); 

// num is already freed there for it cause segmentation fault
*num = 110; 

堆栈溢出-:堆栈内存不足访问数组超出界限'使用printf()和scanf()时使用错误的格式说明符'

其他回答

分段错误的简单含义是,您试图访问一些不属于您的内存。当我们尝试在只读内存位置读取和/或写入任务或尝试释放内存时,会发生分段错误。换句话说,我们可以将其解释为某种内存损坏。

下面我提到了程序员所犯的导致分段错误的常见错误。

以错误的方式使用scanf()(忘记放&)。

int num;
scanf("%d", num);// must use &num instead of num

以错误的方式使用指针。

int *num; 
printf("%d",*num); //*num should be correct as num only
//Unless You can use *num but you have to point this pointer to valid memory address before accessing it.

修改字符串文字(指针尝试写入或修改只读内存。)

char *str;  

//Stored in read only part of data segment
str = "GfG";      

//Problem:  trying to modify read only memory
*(str+1) = 'n';

尝试通过已释放的地址进行访问。

// allocating memory to num 
int* num = malloc(8); 
*num = 100; 

// de-allocated the space allocated to num 
free(num); 

// num is already freed there for it cause segmentation fault
*num = 110; 

堆栈溢出-:堆栈内存不足访问数组超出界限'使用printf()和scanf()时使用错误的格式说明符'

当程序试图访问不存在的内存位置,或试图以不允许的方式访问内存位置时,会发生分段错误或访问冲突。

 /* "Array out of bounds" error 
   valid indices for array foo
   are 0, 1, ... 999 */
   int foo[1000];
   for (int i = 0; i <= 1000 ; i++) 
   foo[i] = i;

这里i[1000]不存在,因此出现segfault。

分段故障的原因:

it arise primarily due to errors in use of pointers for virtual memory addressing, particularly illegal access.

De-referencing NULL pointers – this is special-cased by memory management hardware.

Attempting to access a nonexistent memory address (outside process’s address space).

Attempting to access memory the program does not have rights to (such as kernel structures in process context).

Attempting to write read-only memory (such as code segment).

维基百科的Segmentation_fault页面对其进行了很好的描述,只是指出了原因和原因。查看wiki以获得详细描述。

在计算中,分段故障(通常简称为segfault)或访问违规是由具有内存保护的硬件引起的故障,通知操作系统(OS)内存访问违规。

以下是分段故障的一些典型原因:

取消引用NULL指针–这是内存管理硬件的特殊情况试图访问不存在的内存地址(在进程的地址空间之外)试图访问程序无权访问的内存(例如进程上下文中的内核结构)试图写入只读内存(如代码段)

这些通常由导致无效内存访问的编程错误引起:

取消引用或分配给未初始化的指针(指向随机内存地址的通配符指针)取消引用或分配给已释放指针(悬空指针,指向已释放/解除分配/删除的内存)缓冲区溢出。堆栈溢出。试图执行未正确编译的程序。(尽管存在编译时错误,某些编译器仍会输出可执行文件。)

当进程(正在运行的程序实例)试图访问其他进程正在使用的只读内存地址或内存范围或访问不存在的(无效的)内存地址时,会发生分段错误。悬挂引用(指针)问题意味着试图访问内容已从内存中删除的对象或变量,例如:

int *arr = new int[20];
delete arr;
cout<<arr[1];  //dangling problem occurs here

根据维基百科:

当程序试图访问内存不允许的位置访问或尝试访问内存以不允许的方式定位(例如,尝试写入只读位置,或覆盖操作系统的一部分)。