Search code examples
c++dynamic-datasegmentation-fault

Why Does This Pointer-Pointer Initialization Seg Fault?


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);

Solution

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

    Please post the constructor body.