[C++] Core Dump若干原因分析-英文版

Core Dump (Segmentation fault) in C/C++

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you”.

  • When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump.
  • It is an error indicating memory corruption.

Common segmentation fault scenarios:

  • Modifying a string literal :
    The below program may crash (gives segmentation fault error) because the line *(str+1) = ‘n’ tries to write a read only memory.
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; 
} 
  • Accessing an address that is freed :
    Here in the below code, the pointer p is dereferenced after freeing the memory block, which is not allowed by the compiler. So it produces the error segment fault or abnormal program termination at runtime.
    Example:
// C program to illustrate 
// Core Dump/Segmentation fault 
#include <stdio.h> 
#include<alloc.h> 
int main(void) 
{ 
	// allocating memory to p 
	int* p = (int *)malloc(sizeof(int)); 
	*p = 100; 
	
	// deallocated the space allocated to p 
	free(p); 
	
	// core dump/segmentation fault 
	// as now this statement is illegal 
	*p = 110; 
	
	return 0; 
} 
  • Accessing out of array index bounds :
// C++ program to demonstrate segmentation 
// fault when array out of bound is accessed. 
#include <iostream> 
using namespace std; 

int main() 
{ 
  int arr[2]; 
  arr[3] = 10; // Accessing out of bound 
  return 0; 
} 
  • Improper use of scanf() :

    scanf() function expects address of a variable as an input.Here in this program n takes
    value of 2 and assume it’s address as 1000. If we pass n to scanf(), input fetched from STDIN is placed in invalid memory 2 which should be 1000 instead.It’s a memory corruption leading to Seg fault.

    // C program to demonstrate segmentation 
    // fault when value is passed to scanf 
    #include <stdio.h> 
    
    int main() { 
    int n = 2; 
    scanf("%d",n); 
    return 0; 
    } 
    
  • Stack Overflow

    It’s not a pointer related problem even code may not have single pointer. It’s because of recursive function gets called repeatedly which eats up all the stack memory resulting in stack overflow. Running out of memory on the stack is also a type of memory corruption. It can be resolved by having a base condition to return from the recursive function.

  • Dereferencing uninitialized pointer
    A pointer must point to valid memory before accessing it.

    // C program to demonstrate segmentation 
    // fault when uninitialized pointer is accessed. 
    #include <stdio.h> 
    
    int main() 
    { 
    int *p; 
    printf("%d",*p); 
    return 0; 
    } 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值