C++ 分段故障C++;

C++ 分段故障C++;,c++,segmentation-fault,C++,Segmentation Fault,我得到一个“分段错误(内核转储)”运行时错误,代码如下: #include <iostream> #include "Student.h" #include "SortedList.h" using namespace std; #define BOUNDS 100 int main() { SortedList *list = new SortedList(); // points to the sorted list object Student *cr

我得到一个“分段错误(内核转储)”运行时错误,代码如下:

#include <iostream>
#include "Student.h"
#include "SortedList.h"

using namespace std;

#define BOUNDS 100

int main() {

    SortedList *list = new SortedList();  // points to the sorted list object
    Student *create[BOUNDS];  // array to hold 100 student objects
    int num = 100000;   // holds different ID numbers

    // fills an array with 100 students of various ID numbers
    for (int i = 0; i < BOUNDS; i++) {
        create[i] = new Student(num);
        num += 10;
    }

    // insert all students into the sorted list
    for (int i = 0; i < BOUNDS; i++)
    list->insert(create[i]);

    // individually deletes each student
    num = 100000;
    for (int i = 0; i < BOUNDS; i++) {
        delete list->find(num);
    num += 10;
    }

    // insert all students into the sorted list
    for (int i = 0; i < BOUNDS; i++)
    list->insert(create[i]);

    num = 100000;
    for (int i = 0; i < BOUNDS; i++) {
    list->remove(num);
    num += 10;
    }

    cout << "test2" << endl;
    delete list;
    return 0;
}
#包括
#包括“Student.h”
#包括“SortedList.h”
使用名称空间std;
#定义边界100
int main(){
SortedList*list=new SortedList();//指向已排序的列表对象
Student*创建[BOUNDS];//用于容纳100个Student对象的数组
int num=100000;//保存不同的ID号

//用100个不同ID号的学生填充数组 for(int i=0;i插入(创建[i]); //逐个删除每个学生 num=100000; for(int i=0;i查找(num); num+=10; } //将所有学生插入已排序的列表 for(int i=0;i插入(创建[i]); num=100000; for(int i=0;i删除(num); num+=10; }
cout您肯定在泄漏内存:

// fills an array with 100 students of various ID numbers
for (int i = 0; i < BOUNDS; i++) {
    x = new Student(num);
    num += 10;
}

//用100个不同ID号的学生填充数组
for(int i=0;i
x
在这段代码中被泄露,除非
Student
的ctor以某种方式神奇地插入到某个可以跟踪指针的地方


这可能与车祸有关。

我可以看出你有两个问题

首先,在这个循环中:

for (int i = 0; i < BOUNDS; i++) {
    x = new Student(num);
    num += 10;
}

在自动存储(堆栈)中的
Student
s上(因为您在
create
中用指向
Student
s的指针填充了列表,其中包含自动
Student
s),这会导致未定义的行为,可能是SEGFULT的原因。您不需要解除分配这些
学生
s,因为当数组在
main

末尾超出范围时,它们将被解除分配,而不知道
学生列表
是如何实现的,这有点像是在黑暗中拍摄的,但是

list->insert(&create[i]);
正在向列表中添加堆栈分配的对象,然后
delete list->find(num);
尝试删除此堆栈分配的对象。您不能
删除
堆栈分配的对象

除此之外,您的第一个
for
循环正在泄漏内存


我是忍者。

这句话有个问题:

list->insert(&create[i]);
此时,
create
已被分配,但其中没有任何内容。可能应该在那里分配
x=new Student(num)
的结果。

在堆栈上分配了“create”数组。您正试图删除堆栈分配的内存,这就是出现此错误的原因


delete list->find(num);

您似乎在unixoid系统上运行此操作。因此使用Valgrind;)“//用100个不同ID号的学生填充数组”该循环与此评论不符。作为参考,每次有人使用命名空间std键入
,我都会踢小狗。在你再次这样做之前,想想可怜的小狗。@cHao小狗们为我做了什么?使用命名空间std
没有问题,除非它在头文件中。
num
不用作索引到任何数组,因此
num/10>NBOUND
无关紧要。此外,
delete
不是“删除未初始化的对象”,而是
delete
ing未动态分配的对象,这会产生未定义的行为。很抱歉,您仍然有两个错误注释:
//错误:'create'not initialized
(如果数组中的对象不是内置类型,则对其进行初始化)和
错误:删除未初始化的对象(可能不是0)<代码> >因为上面所说的,你错了。你使用的是C++编译器?这不是真的。没有初始化代码的内容,代码是< >代码> />代码。而且栈变量没有自动地被初始化,因为我想知道你所指的C++标准是什么,因为这个是我第一次听到这个…是的,他们是。你可以在这里看到一个例子:。这就像你在堆栈上创建了一个变量一样;数组的每个元素都被初始化(如果不是POD类型)@Seth:gotcha。抱歉。忘记了
Student
是一个类:-)
list->insert(&create[i]);