为什么指针指针初始化段错误?

时间:2022-09-06 15:10:15

I create a pointer-to-pointer of a class object and when I try to create a new object using the pointers it seg-faults. Why does this happen?

我创建了一个类对象的指针指针,当我尝试使用指针创建一个新对象时,它会出现段错误。为什么会这样?

struct Level
{   
        int SoldierCount;
        Soldier **soldier;
        int taskCount;
        int *taskPercentage;
        int *taskBitmapX;
        int *taskBitmapY;
}level;

void createMap()
{
    //Input and Declartion of various variabls goes here

    level.soldier = new Soldier* [level.SoldierCount];

    //Seg Faults Here
        level.Soldier[i] = new Soldier(initX, initY, initDirection, steps);     

}

The Soldier Class Constructor:

士兵类构造函数:

Soldier(int, int, int, int);

4 个解决方案

#1


With empty Soldier constructor your code works fine (except for corrected typos, like lowercase level.soldier[])

使用空Soldier构造函数,您的代码工作正常(更正的错别字除外,如小写level.soldier [])

Please post the constructor body.

请发布构造函数体。

#2


I can not find any segfault related problems in your code.

我在代码中找不到任何与段错误相关的问题。

But I'm confused as to why your case sensitivity does not match:
The class is called "Soldier" and the Soldier** is called "soldier".

但我很困惑为什么你的区分大小写不匹配:该类被称为“士兵”而士兵**被称为“士兵”。

But you write:

但你写道:

level.soldier = new soldier* [level.SoldierCount];

and:

level.Soldier[i] = new Soldier(initX, initY, initDirection, steps);

If the code really compiles as you've written it, this could be the problem.

如果代码在您编写代码时确实编译,则可能是问题所在。

#3


Possibly i >= level.SoldierCount?

可能是i> = level.SoldierCount?

#4


What is the value of level.SoldierCount? What is the value of i

level.SoldierCount的价值是多少?我的价值是什么?

The only way a segfault can occur is if you access unallocated memory. In the line you highlighted, the only place that can happen is in the array (or inside the constructor, which you didn't post the code for). Most likely, you're accessing the array out of bounds.

发生段错误的唯一方法是访问未分配的内存。在您突出显示的行中,唯一可能发生的地方是数组(或构造函数内部,您没有发布代码)。最有可能的是,您正在访问数组越界。

#1


With empty Soldier constructor your code works fine (except for corrected typos, like lowercase level.soldier[])

使用空Soldier构造函数,您的代码工作正常(更正的错别字除外,如小写level.soldier [])

Please post the constructor body.

请发布构造函数体。

#2


I can not find any segfault related problems in your code.

我在代码中找不到任何与段错误相关的问题。

But I'm confused as to why your case sensitivity does not match:
The class is called "Soldier" and the Soldier** is called "soldier".

但我很困惑为什么你的区分大小写不匹配:该类被称为“士兵”而士兵**被称为“士兵”。

But you write:

但你写道:

level.soldier = new soldier* [level.SoldierCount];

and:

level.Soldier[i] = new Soldier(initX, initY, initDirection, steps);

If the code really compiles as you've written it, this could be the problem.

如果代码在您编写代码时确实编译,则可能是问题所在。

#3


Possibly i >= level.SoldierCount?

可能是i> = level.SoldierCount?

#4


What is the value of level.SoldierCount? What is the value of i

level.SoldierCount的价值是多少?我的价值是什么?

The only way a segfault can occur is if you access unallocated memory. In the line you highlighted, the only place that can happen is in the array (or inside the constructor, which you didn't post the code for). Most likely, you're accessing the array out of bounds.

发生段错误的唯一方法是访问未分配的内存。在您突出显示的行中,唯一可能发生的地方是数组(或构造函数内部,您没有发布代码)。最有可能的是,您正在访问数组越界。