首页 > 编程语言 >c++ 段错误(核心已转储)

c++ 段错误(核心已转储)

时间:2023-07-13 20:01:45浏览次数:36  
标签:main return 错误 int c++ 转储 using include ptr

一、什么是段错误?

段错误应该就是访问了不可访问的内存,这个内存区要么是不存在的,要么是受到系统保护的,还有可能是缺少文件或者文件损坏。

二、段错误产生的原因

1、访问不存在的内存地址

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    int *ptr = NULL;
    *ptr = 0;
    return 0;
}

2、访问系统保护的内存地址

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    int *ptr = (int *)0;
    *ptr = 100;
    return 0;
}

3、访问只读的内存地址

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    char *ptr = "test";    
    strcpy (ptr, "TEST");
    return 0;
}

4、空指针废弃

因为是一个很常见的程序错误空指针废弃(读或写在一个空指针,用于C的意思是“没有对象指针”作为一个错误指示器), 大多数操作系统内存访问空指针的地址,这样它会导致段错误。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    int *ptr = NULL;
    printf("%d\n", *ptr);
    return 0;
}

非关联化一个空指针,然后分配(写一个值到一个不存在的目标)也通常会导致段错误。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    int *ptr = NULL;
    *ptr = 1;
    return 0;
}

下面的代码包含一个空指针,但当编译通常不会导致段错误,值是未使用的。因此,废弃通常会被优化掉,死代码消除。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    int *ptr = NULL;
    *ptr;
    return 0;
}

还有,比如malloc 动态分配内存,释放、置空完成后,不可再使用该指针。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    char* str = (char*)malloc(100);
    if(*str)
    {
        return ;
    }
    strcpy(str,"hello");
    printf("%s\n",str);
    free(str);
    str=NULL;
    strcpy(str,"abcdef");
    return 0;
}

5、堆栈溢出

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    main();
    return 0;
}

6、内存越界(数组越界,变量类型不一致等)

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
int main()
{
    char test[10];
    printf("%c\n", test[10000000]);
    return 0;
}

 

标签:main,return,错误,int,c++,转储,using,include,ptr
From: https://www.cnblogs.com/lyfily-p-7439305/p/17551973.html

相关文章

  • startapp时出现 CommandError错误
    startapp时出现CommandError:'xxxxx'conflictswiththenameofanexistingPythonmoduleandcannotbeusedasanappname.Pleasetryanothername.错误原因可能是在settings.py中加入了sys.path.append(os.path.join(BASE_DIR,'app'))解决办法,先将上面这行代码......
  • C++ 文件和流
     到目前为止,我们已经使用了 iostream 标准库,它提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流。本教程介绍如何从文件读取流和向文件写入流。这就需要用到C++中另一个标准库 fstream,它定义了三个新的数据类型:数据类型描述ofstream该数据类......
  • C++ STL容器之vector、list
    (1)vector连续存储的容器,动态数组,在堆上分配空间底层实现:数组扩容机制:vector增加(插入)新元素时,如果未超过当时的容量,则还有剩余空间,那么直接添加到最后(插入指定位置),然后调整迭代器。如果没有剩余空间了,则会重新配置原有元素个数的两倍空间,然后将原空间元素通过复制的方式初始......
  • 现代C++(Modern C++)基本用法实践:一、类型推导
    概述类型推导主要是依赖auto关键字和decltype关键字/运算符实现的,具体用法参考下面的例子。二者特点:auto用于声明时推导遍历decltype用于推导各种表达式,decltype(var)中var也是一种称为变量表达式的表达式二者都是在编译时进行推导。引用类型推断:decltype推断变量类型时......
  • 现代C++(Modern C++)基本用法实践:五、智能指针(Smart Pointers)
    概述c++效率较高的一个原因是我们可以自己定制策略手动申请和释放内存,当然,也伴随着开发效率降低和内存泄漏的风险。为了减少手动管理内存带来的困扰,c++提出了智能指针,可以帮助我们进行内存管理,有三种:std::unique_ptr是一种独占所有权的智能指针,它不允许多个指针指向同一个对......
  • 现代C++(Modern C++)基本用法实践:四、模板
    概述C++的模板是泛型编程思想的一种实现。C++是强类型语言,处处强调类型。同样的加法运算,int和float的加法运算需定义两个函数(重载),而使用模板则可以只用一个函数(见下面示例)。这类似我们面向对象所说的多态(定义加法运算,各个类型有不同的实现),所以是所谓静多态的一种实现方式,不同的......
  • 现代C++(Modern C++)基本用法实践:三、移动语义
    概述移动移动(move)语义C++引入了一种新的内存优化,以避免不必要的拷贝。在构造或者赋值的时候,如果实参是右值(或者左值由std::move转换成右值),便会匹配移动语义的函数调用如下述举例的Str(Str&&obj)。移动语义的本质是将资源(内存/句柄)转移给另一个对象,被转移资源的对象不应再被使......
  • 现代C++(Modern C++)基本用法实践:二、Lambda表达式
    概述lambda表达式,有时也被称为匿名函数。他提供了简短的,内联的函数对象。用法形式如:[capture](parameters)->return_type{body}具体用法如下文举例它的实现是由编译器决定的,在我的编译器上他是通过创建一个匿名类,通过重载()运算符,成为一个可调用对象,从而实现调用,类似://......
  • 现代C++(Modern C++)基本用法实践:七、范围遍历
    概述c++的for循环在语法上有些刻板,近几个版本对此进行了优化,支持了基于范围的for循环用法举例参考测试项目代码ModernCppTest/modrenc_range_for.cpp主要内容:数组遍历vector遍历字符串遍历map遍历#include"ModernCppTestHeader.h"#include<vector>#include<map>......
  • 现代C++(Modern C++)基本用法实践:六、constexpr编译时计算
    概述constexpr修饰的变量、函数、对象构造函数表示在编译时就可以确定。它经常用来计算一些编译期可以确定常数,和常数组成的表。比如编译时确定10000以内所有的素数,运行时用的时候直接查表。用法举例参考测试项目代码ModernCppTest/modrenc_constexpr.cpp主要内容:constexpr......